public void GetBigEndianUInt32() { _log.Debug("Running: GetBigEndianUInt32"); const UInt32 anInt = 12345; UInt32 aNewInt = ByteEncoder.GetBigEndian(anInt); Assert.IsTrue(anInt == ByteEncoder.GetBigEndian(aNewInt)); }
protected override void EndSize32(int pos) { int cur = (int)_out.Position; _out.Seek(pos, SeekOrigin.Begin); _writer.Write(ByteEncoder.GetBigEndian((Int32)cur - pos - 4)); _out.Seek(cur, SeekOrigin.Begin); }
public void GetBigEndianlong() { _log.Debug("Running: GetBigEndianlong"); const long anInt = 123456660700770; long aNewInt = ByteEncoder.GetBigEndian(anInt); Assert.IsTrue(anInt == ByteEncoder.GetBigEndian(aNewInt)); }
public void WriteHeader(BinaryWriter writer) { writer.Seek(0, SeekOrigin.Begin); writer.Write(ByteEncoder.GetBigEndian(ShapeFileConstants.HeaderStartCode)); writer.Write(new Byte[20]); writer.Write(ByteEncoder.GetBigEndian(FileLengthInWords)); writer.Write(ByteEncoder.GetLittleEndian(ShapeFileConstants.VersionCode)); writer.Write(ByteEncoder.GetLittleEndian((Int32)ShapeType)); writer.Write(ByteEncoder.GetLittleEndian(Extents.GetMin(Ordinates.X))); writer.Write(ByteEncoder.GetLittleEndian(Extents.GetMin(Ordinates.Y))); writer.Write(ByteEncoder.GetLittleEndian(Extents.GetMax(Ordinates.X))); writer.Write(ByteEncoder.GetLittleEndian(Extents.GetMax(Ordinates.Y))); writer.Write(new Byte[32]); // Z-values and M-values }
/// <summary> /// Parses the .shx shapefile index file /// </summary> /// <remarks> /// The index file is organized to give a matching offset and content length for each entry in the .shp file. /// /// From ESRI ShapeFile Technical Description document /// /// http://www.esri.com/library/whitepapers/pdfs/shapefile.pdf /// /// Byte /// Position Field Value Type Order /// --------------------------------------------------------- /// Byte 0 Offset Offset Integer Big /// Byte 4 Content Length Content Length Integer Big /// /// The Integer type corresponds to the CLS Int32 type. /// </remarks> private void parseIndex() { using (FileStream indexStream = _indexFile.Open(FileMode.Open, FileAccess.Read, FileShare.Read)) using (BinaryReader indexReader = new BinaryReader(indexStream, Encoding.Unicode)) { indexStream.Seek(ShapeFileConstants.HeaderSizeBytes, SeekOrigin.Begin); UInt32 recordNumber = 0; while (indexStream.Position < indexStream.Length) { Int32 offset = ByteEncoder.GetBigEndian(indexReader.ReadInt32()); Int32 length = ByteEncoder.GetBigEndian(indexReader.ReadInt32()); IndexEntry entry = new IndexEntry(length, offset); _shapeIndex[recordNumber++] = entry; } } }
public void Save() { using (FileStream indexStream = _indexFile.Open(FileMode.Create, FileAccess.Write, FileShare.None)) using (BinaryWriter indexWriter = new BinaryWriter(indexStream)) { _header.Extents = ShapeFile.GetExtents(); _header.FileLengthInWords = computeIndexLengthInWords(); _header.WriteHeader(indexWriter); foreach (IndexEntry entry in _shapeIndex.Values) { indexWriter.Write(ByteEncoder.GetBigEndian(entry.Offset)); indexWriter.Write(ByteEncoder.GetBigEndian(entry.Length)); } indexWriter.Flush(); } }
/// <summary> /// Reads and parses the header of the .shp index file /// </summary> /// <remarks> /// From ESRI ShapeFile Technical Description document /// /// http://www.esri.com/library/whitepapers/pdfs/shapefile.pdf /// /// Byte /// Position Field Value Type Order /// ----------------------------------------------------- /// Byte 0 File Code 9994 Integer Big /// Byte 4 Unused 0 Integer Big /// Byte 8 Unused 0 Integer Big /// Byte 12 Unused 0 Integer Big /// Byte 16 Unused 0 Integer Big /// Byte 20 Unused 0 Integer Big /// Byte 24 File Length File Length Integer Big /// Byte 28 Version 1000 Integer Little /// Byte 32 Shape Type Shape Type Integer Little /// Byte 36 Bounding Box Xmin Double Little /// Byte 44 Bounding Box Ymin Double Little /// Byte 52 Bounding Box Xmax Double Little /// Byte 60 Bounding Box Ymax Double Little /// Byte 68* Bounding Box Zmin Double Little /// Byte 76* Bounding Box Zmax Double Little /// Byte 84* Bounding Box Mmin Double Little /// Byte 92* Bounding Box Mmax Double Little /// /// * Unused, with value 0.0, if not Measured or Z type /// /// The "Integer" type corresponds to the CLS Int32 type, and "Double" to CLS Double (IEEE 754). /// </remarks> private void parseHeader(BinaryReader reader) { reader.BaseStream.Seek(0, SeekOrigin.Begin); // Check file header if (ByteEncoder.GetBigEndian(reader.ReadInt32()) != ShapeFileConstants.HeaderStartCode) { throw new ShapeFileIsInvalidException("Invalid ShapeFile (.shp)"); } // Seek to File Length reader.BaseStream.Seek(24, 0); // Read filelength as big-endian. The length is number of 16-bit words in file FileLengthInWords = ByteEncoder.GetBigEndian(reader.ReadInt32()); // Seek to ShapeType reader.BaseStream.Seek(32, 0); ShapeType = (ShapeType)reader.ReadInt32(); // Seek to bounding box of shapefile reader.BaseStream.Seek(36, 0); // Read the spatial bounding box of the contents Double xMin = ByteEncoder.GetLittleEndian(reader.ReadDouble()); Double yMin = ByteEncoder.GetLittleEndian(reader.ReadDouble()); Double xMax = ByteEncoder.GetLittleEndian(reader.ReadDouble()); Double yMax = ByteEncoder.GetLittleEndian(reader.ReadDouble()); ICoordinate min = _geoFactory.CoordinateFactory.Create(xMin, yMin); ICoordinate max = _geoFactory.CoordinateFactory.Create(xMax, yMax); Extents = min.Equals(max) && min.Equals(_geoFactory.CoordinateFactory.Create(0, 0)) //jd: if the shapefile has just been created the box wil be 0,0,0,0 in this case create an empty extents ? _geoFactory.CreateExtents() : _geoFactory.CreateExtents(min, max); //jd:allow exmpty extents //if (Extents.IsEmpty) //{ // Extents = null; //} }
private void Frame(byte flags, byte type, byte track, int channel, int size, MemoryStream buf) { lock (_sendlock) { _writer.Write(flags); _writer.Write(type); _writer.Write(ByteEncoder.GetBigEndian((UInt16)(size + network.Frame.HEADER_SIZE))); _writer.Write((byte)0); _writer.Write(track); _writer.Write(ByteEncoder.GetBigEndian((UInt16)(channel))); _writer.Write((byte)0); _writer.Write((byte)0); _writer.Write((byte)0); _writer.Write((byte)0); _sender.Send(_header); _header.Seek(0, SeekOrigin.Begin); _sender.Send(buf, size); } }
public override void WriteUint16(int s) { Debug.Assert(s < 0x10000); _writer.Write(ByteEncoder.GetBigEndian((UInt16)s)); }
public override void WriteDouble(double d) { _writer.Write(ByteEncoder.GetBigEndian(d)); }
private ShapeFileIndex(ShapeFileProvider shapeFile, FileInfo file) { if (shapeFile == null) { throw new ArgumentNullException("shapeFile"); } if (file == null) { throw new ArgumentNullException("file"); } _shapeFile = shapeFile; if (String.Compare(file.Extension, ".shx", StringComparison.InvariantCultureIgnoreCase) != 0) { throw new ShapeFileIsInvalidException("Shapefile index must end in '.shx'."); } _indexFile = file; if (File.Exists(_indexFile.FullName)) { using (FileStream indexStream = _indexFile.Open(FileMode.Open, FileAccess.Read, FileShare.Read)) using (BinaryReader reader = new BinaryReader(indexStream)) { _header = new ShapeFileHeader(reader, _shapeFile.GeometryFactory); indexStream.Seek(ShapeFileConstants.HeaderSizeBytes, SeekOrigin.Begin); Int32 featureCount = (Int32)((_indexFile.Length - ShapeFileConstants.HeaderSizeBytes) / ShapeFileConstants.IndexRecordByteLength); for (Int32 id = 0; id < featureCount; id++) { Int32 offset = ByteEncoder.GetBigEndian(reader.ReadInt32()); Int32 length = ByteEncoder.GetBigEndian(reader.ReadInt32()); IndexEntry entry = new IndexEntry(length, offset); // Record numbers begin at 1. (Shapefile: p. 5) _shapeIndex.Add((UInt32)id + 1, entry); } } } else { // We need to create the index data from the raw data file FileStream dataStream = new FileStream(Path.ChangeExtension(_indexFile.FullName, ".shp"), FileMode.Open, FileAccess.Read); BinaryReader reader = new BinaryReader(dataStream); { _header = new ShapeFileHeader(reader, _shapeFile.GeometryFactory); reader.BaseStream.Seek(ShapeFileConstants.HeaderSizeBytes, SeekOrigin.Begin); long offset = ShapeFileConstants.HeaderSizeBytes; long fileSize = _header.FileLengthInWords * 2; Int32 id = 0; while (offset < fileSize) { reader.BaseStream.Seek(offset, 0); //Skip content length uint oid_id = (uint)ByteEncoder.GetBigEndian(reader.ReadInt32()); int data_length = 2 * ByteEncoder.GetBigEndian(reader.ReadInt32()); if (_shapeIndex.ContainsKey(oid_id) == false) { IndexEntry entry = new IndexEntry(data_length / 2, (int)(offset / 2)); // Record numbers begin at 1. (Shapefile: p. 5) _shapeIndex.Add((UInt32)oid_id, entry); offset += data_length; // Add Record data length offset += 8; // Plus add the record header size ++id; } else { offset = fileSize; } } --id; // Correct the header size _header.FileLengthInWords = (id * 4) + ShapeFileConstants.HeaderSizeBytes; } reader.Close(); dataStream.Close(); } }
public override int ReadInt16() { return(ByteEncoder.GetBigEndian((Int16)_reader.ReadInt16())); }
public override double ReadDouble() { return((double)ByteEncoder.GetBigEndian(_reader.ReadDouble())); }
public override long ReadInt64() { return((long)ByteEncoder.GetBigEndian(_reader.ReadInt64())); }
public override long ReadInt32() { return(ByteEncoder.GetBigEndian((Int32)_reader.ReadInt32())); }
public override void WriteUint32(long i) { Debug.Assert(i < 0x100000000L); _writer.Write(ByteEncoder.GetBigEndian((UInt32)i)); }
public override void WriteUint64(long l) { _writer.Write(ByteEncoder.GetBigEndian(l)); }
private State Next(MemoryStream buf) { BinaryReader reader = new BinaryReader(buf); switch (state) { case State.PROTO_HDR: char a = reader.ReadChar(); char m = reader.ReadChar(); char q = reader.ReadChar(); char p = reader.ReadChar(); if (a != 'A' && m != 'M' && q != 'Q' && p != 'P') { Error("bad protocol header: {0}", buf.ToString()); return(State.ERROR); } reader.ReadByte(); byte instance = reader.ReadByte(); byte major = reader.ReadByte(); byte minor = reader.ReadByte(); Fire_NetworkEvent(new ProtocolHeader(instance, major, minor)); needed = Frame.HEADER_SIZE; return(State.FRAME_HDR); case State.FRAME_HDR: reader = new BinaryReader(buf, Encoding.BigEndianUnicode); flags = reader.ReadByte(); type = SegmentTypeGetter.Get(reader.ReadByte()); // generated code int size = reader.ReadUInt16(); size = ByteEncoder.GetBigEndian((UInt16)size); size -= Frame.HEADER_SIZE; if (size < 0 || size > (64 * 1024 - 12)) { Error("bad frame size: {0:d}", size); return(State.ERROR); } reader.ReadByte(); byte b = reader.ReadByte(); if ((b & 0xF0) != 0) { Error("non-zero reserved bits in upper nibble of " + "frame header byte 5: {0}", b); return(State.ERROR); } track = (byte)(b & 0xF); channel = reader.ReadUInt16(); channel = ByteEncoder.GetBigEndian((UInt16)channel); if (size == 0) { Fire_NetworkEvent(new Frame(flags, type, track, channel, 0, new MemoryStream())); needed = Frame.HEADER_SIZE; return(State.FRAME_HDR); } needed = size; return(State.FRAME_BODY); case State.FRAME_BODY: Fire_NetworkEvent(new Frame(flags, type, track, channel, needed, buf)); needed = Frame.HEADER_SIZE; return(State.FRAME_HDR); default: if (ExceptionProcessing != null) { ExceptionProcessing(this, new ExceptionArgs(new Exception("Error creating frame"))); } throw new Exception("Error creating frame"); } }