using System; using System.IO; namespace Unplugged.IbmBits { public static class BinaryReaderExtensionMethods { /// /// Reads the requested number of 8-bit EBCDIC encoded characters from the stream and converts them to a /// Unicode string. /// /// The number of bytes to read /// /// Unicode encoded string converted from bytes read from the stream. /// The length of the string might be less than the number of bytes requested if the end of the stream is reached. /// public static string ReadStringEbcdic(this BinaryReader reader, int count) { if (ReferenceEquals(null, reader)) throw new ArgumentNullException("reader"); var bytes = reader.ReadBytes(count); return IbmConverter.ToString(bytes); } /// /// Reads a 16-bit integer from the stream that has been encoded as big endian. /// public static Int16 ReadInt16BigEndian(this BinaryReader reader) { if (ReferenceEquals(null, reader)) throw new ArgumentNullException("reader"); var bytes = ReadBytes(reader, 2); return IbmConverter.ToInt16(bytes); } /// /// Reads a 32-bit integer from the stream that has been encoded as big endian. /// public static Int32 ReadInt32BigEndian(this BinaryReader reader) { if (ReferenceEquals(null, reader)) throw new ArgumentNullException("reader"); var bytes = ReadBytes(reader, 4); return IbmConverter.ToInt32(bytes); } /// /// Reads a single precision 32-bit floating point number from the stream /// that has been encoded in IBM System/360 Floating Point format /// /// IEEE formatted single precision floating point public static float ReadSingleIbm(this BinaryReader reader) { if (ReferenceEquals(null, reader)) throw new ArgumentNullException("reader"); var bytes = ReadBytes(reader, 4); return IbmConverter.ToSingle(bytes); } /// /// Reads a pack decimal from the stream /// /// The reader from which the bytes will be read /// The total storage length of the packed decimal /// The scale of the decimal (number of number after the .) /// The decimal read from the stream public static decimal ReadPackedDecimalIbm(this BinaryReader reader, byte storageLength, byte scale) { if (ReferenceEquals(null, reader)) throw new ArgumentNullException("reader"); var bytes = ReadBytes(reader, storageLength); return IbmConverter.ToUnpackedDecimal(bytes, scale); } static byte[] ReadBytes(BinaryReader reader, int count) { var bytes = reader.ReadBytes(count); if (bytes.Length < count) throw new EndOfStreamException(); return bytes; } } }