/// <summary> /// Returns this DBF header in it's original raw binary data form. /// </summary> /// <returns>An Array of type System.Byte[]</returns> public byte[] GetDbfHeader() { try { ByteCollection bytes = new ByteCollection(32 + this._flds.Count * 32); // Keep the same file type ID. bytes.Add(this._id); // Then we right the last time the DBF was updated. byte lstUpYr = (byte)(this._lastUpd.Year - 1900), lstUpMn = (byte)this._lastUpd.Month, lstUpDy = (byte)this._lastUpd.Day; bytes.AddRange(lstUpYr, lstUpMn, lstUpDy); // Next comes the number of records in the table. byte[] recCnt = Hex.GetBytes(Hex.ToHex(this._recCount)); Array.Reverse(recCnt); bytes.AddRange(recCnt); // Now, the offset to the actual data. This is the header data (32 bytes), plus // the field headers (each field header is 32 bytes) plus 1 byte for the // header termination character. byte[] datOfst = Hex.GetBytes(Hex.ToHex(this._dataOffset)); Array.Reverse(datOfst); bytes.AddRange(datOfst); // Then, the size of each record in the table. byte[] recSz = Hex.GetBytes(Hex.ToHex(this._recSize)); Array.Reverse(recSz); bytes.AddRange(recSz); // Next, comes 16 "empty" bytes. byte[] empBts = new byte[16]; empBts.Initialize(); bytes.AddRange(empBts); // Then the CDX structure flag. bytes.Add(Convert.ToByte(this._cdxStruct)); // Then three (3) more "empty" bytes. empBts = new byte[3]; empBts.Initialize(); bytes.AddRange(empBts); // Finally, we write the file headers. for (int i = 0; i < this._flds.Count; i++) { bytes.AddRange(this._flds[0].GetFieldHeader()); } // Don't forget the header terminator character. bytes.Add(Convert.ToByte("0x0D")); // And... we're done ;) return(bytes.ToArray()); } catch { throw; } }
public byte[] GetRawHeader() { ByteCollection hdr = new ByteCollection(); foreach (DbfField fld in this.List) { hdr.AddRange(fld.GetFieldHeader()); } hdr.Add(0x0D); return(hdr.ToArray()); }
static void Main(string[] args) { ByteCollection list1 = new ByteCollection(); for (int i = 0; i < 9; i++) { // list1.Add(i); // This isn't compiled list1.Add((byte)i); } foreach (byte i in list1) { Console.WriteLine(" {0} ", i); } Console.WriteLine(); CharCollection list2 = new CharCollection(); // list2.Add(5); // This isn't compiled list2.Add('a'); list2.Add('b'); list2.Add('c'); list2.Add('d'); list2.Add('e'); list2.Add('f'); foreach (char i in list2) { Console.WriteLine(" {0} ", i); } Console.ReadKey(); }
public byte[] GetRawHeader() { ByteCollection hdr = new ByteCollection(); foreach (DbfField fld in this.List) hdr.AddRange(fld.GetFieldHeader()); hdr.Add(0x0D); return hdr.ToArray(); }
/// <summary> /// Reads a byte array from the stream. /// </summary> public ByteCollection ReadByteArray(string fieldName) { bool isNil = false; ByteCollection values = new ByteCollection(); if (BeginField(fieldName, true, out isNil)) { PushNamespace(Namespaces.OpcUaXsd); while (MoveToElement("Byte")) { values.Add(ReadByte("Byte")); } // check the length. if (m_context.MaxArrayLength > 0 && m_context.MaxArrayLength < values.Count) { throw new ServiceResultException(StatusCodes.BadEncodingLimitsExceeded); } PopNamespace(); EndField(fieldName); return values; } if (isNil) { return null; } return values; }
public byte[] GetMemoField(long row, int col) { // Make sure the user didn't select a field outside the bounds of the table. if (row < 0 || row > _hdr.RecordCount) { throw new ArgumentOutOfRangeException("Requested row was greater than the length of the table.", "row"); } if (col < 0 || col > _hdr.Fields.Count) { throw new ArgumentOutOfRangeException("Requested field is outside the bounds of the record.", "col"); } if (_hdr.Fields[col].FieldDataType != DbfHeader.dBaseFieldType.Memo) { throw new ArgumentException("The selected field is not of type 'Memo'.", "col"); } // We also want to check & make sure the file exists. if (!File.Exists(this.MemoFile)) { throw new FileNotFoundException("The table's DBT file could not be found.", this.MemoFile); } byte[] retData = null; using (FileStream fs = new FileStream(this.MemoFile, FileMode.Open, FileAccess.Read)) using (BinaryReader br = new BinaryReader(fs)) { // First thing we have to do is determine the offset of the block // we want to read. // dBASEIV files can have custom-sized blocks, so we get that from // the header first. int blockSize = 0; if (_hdr.TypeID == DbfHeader.dBaseType.Dbf3WithMemo) { br.ReadBytes(20); byte[] bl = br.ReadBytes(2); Array.Reverse(bl); blockSize = Convert.ToInt32(Hex.ToHex(bl), 16) * 512; br.ReadBytes(490); } else { blockSize = 512; br.ReadBytes(512); } // Now we can find the actual offset. We've already skipped the header, // so we don't have to include that in the offset. int offset = (Convert.ToInt32(this.GetValue(row, col)) - 1) * blockSize; br.ReadBytes(offset); // The first 8 bytes of data are header info. // The first 4 are reserved and always FF FF 08 00. br.ReadBytes(4); // The next 4 indicate the size of the memo field // with the usuall reversed bit array. byte[] btLen = br.ReadBytes(4); Array.Reverse(btLen); int fLen = Convert.ToInt32(Hex.ToHex(btLen), 16); // The stored length includes the 8 bytes for the header, so we // remove those from the total data length. retData = br.ReadBytes(fLen - 8); // Now we need to replace all the 0x08 carriage return bytes with the // standard Windows carriage return/line feed bytes, and make sure // the byte array terminates when the 'End of File' byte (0x0A) is // reached. ByteCollection bts = new ByteCollection(); for (int i = 0; i < retData.Length; i++) { if (retData[i] == 0x0A && (i < retData.Length - 1 && retData[i + 1] == 0x0A)) { break; } //else if (retData[i] == 0x08) //{ // bts.Add(Convert.ToByte('\r')); // bts.Add(Convert.ToByte('\n')); //} else { bts.Add(retData[i]); } } // And then we pump are 'corrected' byte array back into our variable // to return. retData = bts.ToArray(); //retData = new byte[bts.Count]; //for (int i = 0; i < retData.Length; i++) // retData[i] = (byte)bts[i]; } return(retData); }
/// <summary> /// Reads a byte array from the stream. /// </summary> public ByteCollection ReadByteArray(string fieldName) { int length = ReadArrayLength(); if (length == -1) { return null; } ByteCollection values = new ByteCollection(length); for (int ii = 0; ii < length; ii++) { values.Add(ReadByte(null)); } return values; }
//*************************************************************************** // Public Methods // public byte[] GetValue() { ByteCollection retVal = new ByteCollection(); if (this.ValueIsPointer && this._owner.Owner.GetType().Name == typeof(TiffReader).Name) { // The tag value contains a pointer to a byte address in the // original file where the literal data is stored. #region Read data from file FileStream fs = null; BinaryReader br = null; try { // Get a stream and binary reader for the file this tag // belongs to. fs = new FileStream(((TiffReader)this._owner.Owner).LoadFile, FileMode.Open, FileAccess.Read); br = new BinaryReader(fs); // The Value property will return an integer value containing // the byte offset of the actual data in the file, so the // first thing we need to do is jump to that byte address. br.BaseStream.Seek(this.Value, SeekOrigin.Begin); // Now the read cursor's in the right spot, so we just have to // read however many fields of whatever byte-length. for (int i = 0; i < this.FieldCount; i++) { byte[] val; switch (this.FieldType) { case TiffFieldType.ByteType: val = br.ReadBytes(1); break; case TiffFieldType.AsciiType: val = br.ReadBytes(1); break; case TiffFieldType.ShortType: val = br.ReadBytes(2); break; case TiffFieldType.LongType: val = br.ReadBytes(4); break; case TiffFieldType.Rational: // The Rational field type takes a little extra work, // since it's actually two different long values // per field entry. The first value represents the // Numerator, while the second value is the // Denominator. // NOTE: These values are always in sequencial byte- // order, so we don't use Array.Reverse(). byte[] num = br.ReadBytes(4); byte[] den = br.ReadBytes(4); // We have to initialized the 'val' variable to an // empty array before we can use the // Array.Copy() function on it. val = new byte[8]; // Now, we just copy the two byte arrays into the // output array. Array.Copy(num, 0, val, 0, 4); Array.Copy(den, 0, val, 4, 4); break; default: // If we somehow have a different value for the // Fieldtype (which *shouldn't* be possible), // populate the 'val' variable with an empty // array to ensure we don't needlessly throw a // NullReference exception when we try to check // the length of the array. val = new byte[0]; break; } // End Switch(this.FieldType) // Don't forget to swap the byte orders if we're working with // a little-endian file (which will be 90% of the time). // NOTE: We don't want to swap the byte-order of Rational // field types. if (val.Length > 1 && this.FieldType != TiffFieldType.Rational && this._owner.Owner.LittleEndian) { Array.Reverse(val); } // And, finally, add the value to the collection. if (val.Length > 0) { retVal.AddRange(val); } } // Next i } // End Try catch { // Just throw the exception back up the call stack so that the // calling function can deal with it. throw; } finally { // Make sure we release the BinaryReader and FileStream objects. if (br != null) { br.Close(); } if (fs != null) { fs.Dispose(); } } #endregion } else if (this.ValueIsPointer) { // This tag is part of an IDF belonging to a TiffWriter object, so // we need to look for the real data in the TiffWriter's // _metaData field. int metaOffset = this.Value; for (int i = 0; i < this.FieldCount; i++) { // We still need to be concious of what type of data we're // getting from the binary stream. switch (this.FieldType) { case TiffFieldType.AsciiType: retVal.Add(((TiffWriter)this._owner.Owner)._metaData[metaOffset + i]); break; case TiffFieldType.ByteType: retVal.Add(((TiffWriter)this._owner.Owner)._metaData[metaOffset + i]); break; case TiffFieldType.ShortType: retVal.Add(((TiffWriter)this._owner.Owner)._metaData[metaOffset + (i * 2)]); retVal.Add(((TiffWriter)this._owner.Owner)._metaData[metaOffset + (i * 2) + 1]); break; case TiffFieldType.LongType: retVal.Add(((TiffWriter)this._owner.Owner)._metaData[metaOffset + (i * 4)]); retVal.Add(((TiffWriter)this._owner.Owner)._metaData[metaOffset + (i * 4) + 1]); retVal.Add(((TiffWriter)this._owner.Owner)._metaData[metaOffset + (i * 4) + 2]); retVal.Add(((TiffWriter)this._owner.Owner)._metaData[metaOffset + (i * 4) + 3]); break; case TiffFieldType.Rational: retVal.Add(((TiffWriter)this._owner.Owner)._metaData[metaOffset + (i * 8)]); retVal.Add(((TiffWriter)this._owner.Owner)._metaData[metaOffset + (i * 8) + 1]); retVal.Add(((TiffWriter)this._owner.Owner)._metaData[metaOffset + (i * 8) + 2]); retVal.Add(((TiffWriter)this._owner.Owner)._metaData[metaOffset + (i * 8) + 3]); retVal.Add(((TiffWriter)this._owner.Owner)._metaData[metaOffset + (i * 8) + 4]); retVal.Add(((TiffWriter)this._owner.Owner)._metaData[metaOffset + (i * 8) + 5]); retVal.Add(((TiffWriter)this._owner.Owner)._metaData[metaOffset + (i * 8) + 6]); retVal.Add(((TiffWriter)this._owner.Owner)._metaData[metaOffset + (i * 8) + 7]); break; } } } else { // The tag value contains the literal data, not a pointer, so we // just need to pull the data straight from the binary data // we're already holding. #region Read data from tag if (this.FieldType == TiffFieldType.AsciiType || this.FieldType == TiffFieldType.ByteType) { // We're just retrieving single byte values, so just dump them // into the ByteCollection in order. for (int i = 0; i < this.FieldCount; i++) { retVal.Add(this._raw[i + 8]); } } else if (this.FieldType == TiffFieldType.ShortType) { // Here, we read either one or two 2-byte integer values. for (int i = 0; i < this.FieldCount; i++) { byte[] val = new byte[] { this._raw[(i * 2) + 8], this._raw[(i * 2) + 9] }; if (this._owner.Owner.LittleEndian) { Array.Reverse(val); } retVal.AddRange(val); } } else { // If we hit here, we must be pulling a Long value, which means // we there can only be a single field value. byte[] val = new byte[4]; for (int i = 0; i < val.Length; i++) { val[i] = this._raw[i + 8]; } if (this._owner.Owner.LittleEndian) { Array.Reverse(val); } retVal.AddRange(val); } #endregion } return(retVal.ToArray()); }
public void SetTag(int idfNum, TiffTagType tagType, params object[] value) { if (tagType == TiffTagType.StripOffsets || tagType == TiffTagType.StripByteCounts) { throw new Exception("The 'StripOffsets' and 'StripByteCounts' tags will be constructed automatically and may not be manually configured."); } ByteCollection tagBts = new ByteCollection(); tagBts.AddRange(Hex.GetBinary((short)tagType, this._littleEndian)); TiffFieldType fldType = (TiffFieldType)Enum.Parse(typeof(TiffTagIDFieldType), tagType.ToString()); tagBts.AddRange(Hex.GetBinary((short)fldType, this._littleEndian)); tagBts.AddRange(Hex.GetBinary(value.Length, this._littleEndian)); if (TiffTag.MustBePointer(fldType, value.Length) || fldType == TiffFieldType.AsciiType) { // We'll have to add the data to the _metaData ByteCollection, // since it won't fit into the tag itself. We'll also need // to make a pointer to the data in the tag. int metaPointer = this._metaData.Count; ByteCollection tmpMetaData = new ByteCollection(); // We need to process each passed value. Often there may be only // one, but we still have to be prepared for multiple values. for (int i = 0; i < value.Length; i++) { try { // First, we need to figure out what kind of data we're deeling with, // since it will determine what we do with it. switch (fldType) { case TiffFieldType.AsciiType: // ASCII type is easy... we just convert the text to ASCII // bytes and dump them to the metaData collection. tmpMetaData.AddRange(System.Text.ASCIIEncoding.ASCII.GetBytes(value[i].ToString())); break; case TiffFieldType.ByteType: tmpMetaData.Add((byte)value[i]); break; case TiffFieldType.ShortType: tmpMetaData.AddRange(Hex.GetBinary((short)value[i], this._littleEndian)); break; case TiffFieldType.LongType: tmpMetaData.AddRange(Hex.GetBinary((int)value[i], this._littleEndian)); break; case TiffFieldType.Rational: string[] vals = ((string)value[i]).Split('/', '|'); tmpMetaData.AddRange(Hex.GetBinary(int.Parse(vals[0]))); tmpMetaData.AddRange(Hex.GetBinary(int.Parse(vals[1]))); break; } } catch { // The most likely reson for ending up here is that we got // passed some data that didn't match the field type for // the specified field. Just chunk the error back up the // stack and let the caller deal with it. throw; } // We don't want to add the values to the actuall metaData // collection until we're sure we've got it all processes // and converted. Otherwise, we might end up wtih 'orphaned' // meta data. this._metaData.AddRange(tmpMetaData.ToArray()); tagBts.AddRange(Hex.GetBinary(metaPointer, this._littleEndian)); } } else { // The tag value is small enough to reside inside the tag itself. // This automatically rules out certain data types such as // Rational and ASCII, so we should only be dealing with // Byte, Short, and Long here. for (int i = 0; i < value.Length; i++) { if (value[i].GetType().Name.ToLower() == "int32") { tagBts.AddRange(Hex.GetBinary((short)value[i], this._littleEndian)); } else if (value[i].GetType().Name.ToLower() == "int64") { tagBts.AddRange(Hex.GetBinary((int)value[i], this._littleEndian)); } } } // If we ended up with less than 12 bytes for the tag data, it means // we're storing a short or byte value that doesn't 'fill' the tag's // value space, so we just need to add 'padding'. int btShort = 12 - tagBts.Count; for (int t = 0; t < btShort; t++) { tagBts.Add(byte.Parse("00")); } // Now, we're read to add the tag to the IDF. this._idfs[idfNum].Tags.Add(new TiffTag(this._idfs[idfNum], tagBts.ToArray())); }
/// <summary> /// Reads a byte array from the stream. /// </summary> public ByteCollection ReadByteArray(string fieldName) { var values = new ByteCollection(); List<object> token = null; if (!ReadArrayField(fieldName, out token)) { return values; } for (int ii = 0; ii < token.Count; ii++) { try { m_stack.Push(token[ii]); values.Add(ReadByte(null)); } finally { m_stack.Pop(); } } return values; }
public byte[] GetMemoField(long row, int col) { // Make sure the user didn't select a field outside the bounds of the table. if (row < 0 || row > _hdr.RecordCount) throw new ArgumentOutOfRangeException("Requested row was greater than the length of the table.", "row"); if (col < 0 || col > _hdr.Fields.Count) throw new ArgumentOutOfRangeException("Requested field is outside the bounds of the record.", "col"); if (_hdr.Fields[col].FieldDataType != DbfHeader.dBaseFieldType.Memo) throw new ArgumentException("The selected field is not of type 'Memo'.", "col"); // We also want to check & make sure the file exists. if (!File.Exists(this.MemoFile)) throw new FileNotFoundException("The table's DBT file could not be found.", this.MemoFile); byte[] retData = null; using (FileStream fs = new FileStream(this.MemoFile, FileMode.Open, FileAccess.Read)) using (BinaryReader br = new BinaryReader(fs)) { // First thing we have to do is determine the offset of the block // we want to read. // dBASEIV files can have custom-sized blocks, so we get that from // the header first. int blockSize = 0; if (_hdr.TypeID == DbfHeader.dBaseType.Dbf3WithMemo) { br.ReadBytes(20); byte[] bl = br.ReadBytes(2); Array.Reverse(bl); blockSize = Convert.ToInt32(Hex.ToHex(bl), 16) * 512; br.ReadBytes(490); } else { blockSize = 512; br.ReadBytes(512); } // Now we can find the actual offset. We've already skipped the header, // so we don't have to include that in the offset. int offset = (Convert.ToInt32(this.GetValue(row, col)) - 1) * blockSize; br.ReadBytes(offset); // The first 8 bytes of data are header info. // The first 4 are reserved and always FF FF 08 00. br.ReadBytes(4); // The next 4 indicate the size of the memo field // with the usuall reversed bit array. byte[] btLen = br.ReadBytes(4); Array.Reverse(btLen); int fLen = Convert.ToInt32(Hex.ToHex(btLen), 16); // The stored length includes the 8 bytes for the header, so we // remove those from the total data length. retData = br.ReadBytes(fLen - 8); // Now we need to replace all the 0x08 carriage return bytes with the // standard Windows carriage return/line feed bytes, and make sure // the byte array terminates when the 'End of File' byte (0x0A) is // reached. ByteCollection bts = new ByteCollection(); for (int i = 0; i < retData.Length; i++) if (retData[i] == 0x0A && (i < retData.Length - 1 && retData[i + 1] == 0x0A)) break; //else if (retData[i] == 0x08) //{ // bts.Add(Convert.ToByte('\r')); // bts.Add(Convert.ToByte('\n')); //} else bts.Add(retData[i]); // And then we pump are 'corrected' byte array back into our variable // to return. retData = bts.ToArray(); //retData = new byte[bts.Count]; //for (int i = 0; i < retData.Length; i++) // retData[i] = (byte)bts[i]; } return retData; }
/// <summary> /// Returns this DBF header in it's original raw binary data form. /// </summary> /// <returns>An Array of type System.Byte[]</returns> public byte[] GetDbfHeader() { try { ByteCollection bytes = new ByteCollection(32 + this._flds.Count * 32); // Keep the same file type ID. bytes.Add(this._id); // Then we right the last time the DBF was updated. byte lstUpYr = (byte)(this._lastUpd.Year - 1900), lstUpMn = (byte)this._lastUpd.Month, lstUpDy = (byte)this._lastUpd.Day; bytes.AddRange(lstUpYr, lstUpMn, lstUpDy); // Next comes the number of records in the table. byte[] recCnt = Hex.GetBytes(Hex.ToHex(this._recCount)); Array.Reverse(recCnt); bytes.AddRange(recCnt); // Now, the offset to the actual data. This is the header data (32 bytes), plus // the field headers (each field header is 32 bytes) plus 1 byte for the // header termination character. byte[] datOfst = Hex.GetBytes(Hex.ToHex(this._dataOffset)); Array.Reverse(datOfst); bytes.AddRange(datOfst); // Then, the size of each record in the table. byte[] recSz = Hex.GetBytes(Hex.ToHex(this._recSize)); Array.Reverse(recSz); bytes.AddRange(recSz); // Next, comes 16 "empty" bytes. byte[] empBts = new byte[16]; empBts.Initialize(); bytes.AddRange(empBts); // Then the CDX structure flag. bytes.Add(Convert.ToByte(this._cdxStruct)); // Then three (3) more "empty" bytes. empBts = new byte[3]; empBts.Initialize(); bytes.AddRange(empBts); // Finally, we write the file headers. for (int i = 0; i < this._flds.Count; i++) bytes.AddRange(this._flds[0].GetFieldHeader()); // Don't forget the header terminator character. bytes.Add(Convert.ToByte("0x0D")); // And... we're done ;) return bytes.ToArray(); } catch { throw; } }