Esempio n. 1
0
        /// <summary>
        /// Moves to the next primitive block, returns null at the end.
        /// </summary>
        /// <returns></returns>
        public PrimitiveBlock MoveNext()
        {
            PrimitiveBlock block = null;
            int            length;
            bool           not_found_but = true;

            while (not_found_but)
            {                          // continue if there is still data but not a primitiveblock.
                not_found_but = false; // not found.
                if (Serializer.TryReadLengthPrefix(_stream, PrefixStyle.Fixed32, out length))
                {
                    // TODO: remove some of the v1 specific code.
                    // TODO: this means also to use the built-in capped streams.

                    // code borrowed from: http://stackoverflow.com/questions/4663298/protobuf-net-deserialize-open-street-maps

                    // I'm just being lazy and re-using something "close enough" here
                    // note that v2 has a big-endian option, but Fixed32 assumes little-endian - we
                    // actually need the other way around (network byte order):
                    length = IntLittleEndianToBigEndian((uint)length);

                    BlockHeader header;
                    // again, v2 has capped-streams built in, but I'm deliberately
                    // limiting myself to v1 features
                    using (var tmp = new LimitedStream(_stream, length))
                    {
                        header = Serializer.Deserialize <BlockHeader>(tmp);
                    }
                    Blob blob;
                    using (var tmp = new LimitedStream(_stream, header.datasize))
                    {
                        blob = Serializer.Deserialize <Blob>(tmp);
                    }

                    // construct the source stream, compressed or not.
                    Stream source_stream = null;
                    if (blob.zlib_data == null)
                    { // use a regular uncompressed stream.
                        source_stream = new MemoryStream(blob.raw);
                    }
                    else
                    { // construct a compressed stream.
                        var ms = new MemoryStream(blob.zlib_data);
                        source_stream = new ZLibStreamWrapper(ms);
                    }

                    // use the stream to read the block.
                    HeaderBlock headerBlock;
                    using (source_stream)
                    {
                        if (header.type == "OSMHeader")
                        {
                            headerBlock   = Serializer.Deserialize <HeaderBlock>(source_stream);
                            not_found_but = true;
                        }

                        if (header.type == "OSMData")
                        {
                            block = Serializer.Deserialize <PrimitiveBlock>(source_stream);
                        }
                    }
                }
            }
            return(block);
        }
Esempio n. 2
0
        /// <summary>
        /// Moves to the next primitive block, returns null at the end.
        /// </summary>
        /// <returns></returns>
        public PrimitiveBlock MoveNext()
        {
            PrimitiveBlock block = null;
            int length;
            bool not_found_but = true;
            while (not_found_but)
            { // continue if there is still data but not a primitiveblock.
                not_found_but = false; // not found.
                if (Serializer.TryReadLengthPrefix(_stream, PrefixStyle.Fixed32, out length))
                {
                    // TODO: remove some of the v1 specific code.
                    // TODO: this means also to use the built-in capped streams.

                    // code borrowed from: http://stackoverflow.com/questions/4663298/protobuf-net-deserialize-open-street-maps

                    // I'm just being lazy and re-using something "close enough" here
                    // note that v2 has a big-endian option, but Fixed32 assumes little-endian - we
                    // actually need the other way around (network byte order):
                    length = IntLittleEndianToBigEndian((uint)length);

                    BlockHeader header;
                    // again, v2 has capped-streams built in, but I'm deliberately
                    // limiting myself to v1 features
                    using (var tmp = new LimitedStream(_stream, length))
                    {
                        header = Serializer.Deserialize<BlockHeader>(tmp);
                    }
                    Blob blob;
                    using (var tmp = new LimitedStream(_stream, header.datasize))
                    {
                        blob = Serializer.Deserialize<Blob>(tmp);
                    }

                    // construct the source stream, compressed or not.
                    Stream source_stream = null;
                    if (blob.zlib_data == null)
                    { // use a regular uncompressed stream.
                        source_stream = new MemoryStream(blob.raw);
                    }
                    else
                    { // construct a compressed stream.
                        var ms = new MemoryStream(blob.zlib_data);
                        source_stream = new ZLibStreamWrapper(ms);
                    }

                    // use the stream to read the block.
                    HeaderBlock headerBlock;
                    using (source_stream)
                    {
                        if (header.type == "OSMHeader")
                        {
                            headerBlock = Serializer.Deserialize<HeaderBlock>(source_stream);
                            not_found_but = true;
                        }

                        if (header.type == "OSMData")
                        {
                            block = Serializer.Deserialize<PrimitiveBlock>(source_stream);
                        }
                    }
                }
            }
            return block;
        }