public void WriteRecord(bool binary, byte[] mrecord) { TapeConverter.ToTape(binary, mrecord, out byte[] trecord); if (p7bflag) { trecord[0] |= 0x80; f.Write(trecord, 0, trecord.Length); } else { byte[] len = BitConverter.GetBytes(trecord.Length); if (!BitConverter.IsLittleEndian) { Array.Reverse(len); } f.Write(len, 0, 4); f.Write(trecord, 0, (int)trecord.Length); f.Write(len, 0, 4); } }
public int ReadRecord(out bool binary, out byte[] mrecord) /* reads a record as array of 6-bit value from tape-file, retun -1:end of input file 0: EOF marker read, 1: record read; binary=true: binary record false: bcd record */ { /* reset output values */ mrecord = null; binary = false; if (p7bflag) /* read p7b record */ { int b; /* current character */ if (stored) /* fist byte of record already read? */ { b = last; /* use it */ stored = false; } else { b = f.ReadByte(); /* read byte */ } if (b < 0) /* end of media ? */ { return(-1); /* EOM */ } if ((b & 128) == 0) /* The first byte of a records must have bit 7 set */ { throw new InvalidDataException("TapeReader:Bit 7 not set at record start"); } List <byte> trecord = new List <byte>(160) { (byte)(b & 127) }; /* remove record start marker, store character */ do { b = f.ReadByte(); if (b < 0 || (b & 128) != 0) /* next record or EOF */ { stored = true; /* set flag */ last = b; /* store value for next call*/ break; } trecord.Add((byte)b); }while (true); TapeConverter.FromTape(trecord.ToArray(), out binary, out mrecord); if (!binary && mrecord.Length == 1 && mrecord[0] == 15) { return(0); /* EOF */ } return(1); /* no EOF */ } else /* read tp record */ { const int maxbuffer = 32000; byte[] len = new byte[4]; if (f.Read(len, 0, 4) != 4) /* read length, check if readable */ { return(-1); } if (!BitConverter.IsLittleEndian) { Array.Reverse(len); } uint l = BitConverter.ToUInt32(len, 0); if (l == 0) { return(0); /* length of 0 means EOF */ } else if (l == 0xFFFFFFFF) { return(-1);/* length of -1 means EOM */ } else { if (l > maxbuffer) { return(-1); } byte[] trecord = new byte[l]; if (l != f.Read(trecord, 0, (int)l)) { throw new Exception("read error"); } byte[] elen = new byte[4]; if (4 != f.Read(elen, 0, 4)) { throw new Exception("read error"); } if (!BitConverter.IsLittleEndian) { Array.Reverse(elen); } if (l != BitConverter.ToUInt32(elen, 0)) { throw new Exception("wrong elen"); } TapeConverter.FromTape(trecord, out binary, out mrecord); return(1); /* record was read */ } } }