ReadElementContentAsBase64() public method

public ReadElementContentAsBase64 ( byte buffer, int index, int count ) : int
buffer byte
index int
count int
return int
Example #1
0
 [MonoTODO]         // FIXME: Check how expanded entity is handled here.
 public override int ReadElementContentAsBase64(byte [] buffer, int index, int count)
 {
     if (validatingReader != null)
     {
         return(validatingReader.ReadElementContentAsBase64(buffer, index, count));
     }
     else
     {
         return(sourceReader.ReadElementContentAsBase64(buffer, index, count));
     }
 }
Example #2
0
 [MonoTODO]         // FIXME: Check how expanded entity is handled here.
 public override int ReadElementContentAsBase64(byte [] buffer, int offset, int length)
 {
     if (validatingReader != null)
     {
         return(validatingReader.ReadElementContentAsBase64(buffer, offset, length));
     }
     else
     {
         return(sourceReader.ReadElementContentAsBase64(buffer, offset, length));
     }
 }
        public void StoreImageFromXml(Image image, XmlReader reader)
        {
            MemoryStream memoryStream = new MemoryStream();

            byte[] readBuffer = new byte[128 * 1024];
            int read;
            while ((read = reader.ReadElementContentAsBase64(readBuffer, 0, readBuffer.Length)) > 0)
            {
                memoryStream.Write(readBuffer, 0, read);
            }

            this.data[image] = memoryStream.ToArray();
        }
 public void StoreImageFromXml(Image image, XmlReader reader)
 {
     string remoteFilename = GetImageFilename(image);
     using (Stream fileStream = this.Database.GridFS.Open(remoteFilename, FileMode.Create))
     {
         byte[] readBuffer = new byte[128 * 1024];
         int read;
         while ((read = reader.ReadElementContentAsBase64(readBuffer, 0, readBuffer.Length)) > 0)
         {
             fileStream.Write(readBuffer, 0, read);
         }
     }
 }
Example #5
0
        public override int ReadElementContentAsBase64(byte [] buffer, int index, int count)
        {
//			return base.ReadElementContentAsBase64 (
//				buffer, index, count);
            // FIXME: This is problematic wrt end of entity.
            if (entity != null)
            {
                return(entity.ReadElementContentAsBase64(
                           buffer, index, count));
            }
            else
            {
                return(source.ReadElementContentAsBase64(
                           buffer, index, count));
            }
        }
Example #6
0
        public override int ReadElementContentAsBase64(
            byte [] buffer, int offset, int length)
        {
//			return base.ReadElementContentAsBase64 (
//				buffer, offset, length);
            // FIXME: This is problematic wrt end of entity.
            if (entity != null)
            {
                return(entity.ReadElementContentAsBase64(
                           buffer, offset, length));
            }
            else
            {
                return(source.ReadElementContentAsBase64(
                           buffer, offset, length));
            }
        }
        /// <summary>
        /// Initializes a new instance of the Message class from data
        /// read from an XML stream.
        /// </summary>
        /// <param name="reader">The XmlReader for the XML data.</param>
        public Message(XmlReader reader)
        {
            MemoryStream growableBuffer = null;

            try
            {
                reader.ReadToFollowing("From");
                this.From = reader.ReadElementContentAsString();

                reader.ReadToFollowing("To");
                this.To = reader.ReadElementContentAsString();

                reader.ReadToFollowing("Body");
                int got = 0;
                byte[] buffer = new byte[100];
                growableBuffer = new MemoryStream();
                while ((got = reader.ReadElementContentAsBase64(buffer, 0, buffer.Length)) > 0)
                {
                    growableBuffer.Write(buffer, 0, got);
                }

                this.Body = growableBuffer.ToArray();

                this.Valid = true;
            }
            catch
            {
                return;
            }
            finally
            {
                if (growableBuffer != null)
                {
                    growableBuffer.Dispose();
                }
            }
        }
        protected void ParseTerrainElement(XmlReader r)
        {
            // save the name of the element
            string name = r.Name;

            String mapFormat = null;
            string val;

            if (name == "seedMap")
            { // parse seedMap attributes
                for (int i = 0; i < r.AttributeCount; i++)
                {
                    r.MoveToAttribute(i);

                    // set the field in this object based on the element we just read
                    switch (r.Name)
                    {
                        case "width":
                            SeedMapWidth = int.Parse(r.Value);
                            break;
                        case "height":
                            SeedMapHeight = int.Parse(r.Value);
                            break;
                        case "mapFormat":
                            mapFormat = r.Value;
                            break;
                        case "bitsPerSample":
                            bitsPerSample = int.Parse(r.Value);
                            // deal with old terrain xml, which set bitsPerSample to 256 for 8-bit samples
                            if (bitsPerSample == 256)
                            {
                                bitsPerSample = 8;
                            }
                            break;
                    }
                }
                r.MoveToElement(); //Moves the reader back to the element node.

                SeedMap = new float[SeedMapWidth * SeedMapHeight];

                switch (mapFormat)
                {
                    case "digitString":
                        // read the value
                        r.Read();
                        if (r.NodeType != XmlNodeType.Text)
                        {
                            return;
                        }
                        val = r.Value;

                        Debug.Assert(val.Length == (SeedMapWidth * SeedMapHeight), "ParseSeedMap: value is wrong length");

                        for (int i = 0; i < (SeedMapWidth * SeedMapHeight); i++)
                        {
                            SeedMap[i] = (val[i] - '0') / 9f;
                        }

                        // error out if we dont see an end element here
                        r.Read();
                        if (r.NodeType != XmlNodeType.EndElement)
                        {
                            return;
                        }
                        break;
                    case "base64":

                        int bytesPerSample = bitsPerSample >> 3;
                        int byteCount = bytesPerSample * SeedMapWidth * SeedMapHeight;
                        float divisor = 1.0f / ((float)Math.Pow(256d, bytesPerSample) - 1f);
                        byte [] byteMap = new byte[byteCount];
                        int bytesRead = r.ReadElementContentAsBase64(byteMap, 0, byteCount);

                        Debug.Assert(bytesRead == (byteCount));

                        for (int i = 0; i < (SeedMapWidth * SeedMapHeight); i++)
                        {
                            int value = 0;
                            int byteOffset = i * bytesPerSample;
                            for (int j=0; j<bytesPerSample; j++)
                                value |= (byteMap[byteOffset + j] << (j * 8));
                            SeedMap[i] = value * divisor;
                        }

                        // dont read the end element, because the ReadBase64 appears to consume it
                        break;
                }

            }
            else
            {

                // read the value
                r.Read();
                if (r.NodeType != XmlNodeType.Text)
                {
                    return;
                }
                val = r.Value;

                // error out if we dont see an end element here
                r.Read();
                if (r.NodeType != XmlNodeType.EndElement)
                {
                    return;
                }

                // set the field in this object based on the element we just read
                switch (name)
                {
                    case "algorithm":
                        for (int i = 0; i < AlgorithmNames.Length; i++)
                        {
                            if (AlgorithmNames[i] == val)
                            {
                                Algorithm = (GeneratorAlgorithm)i;
                                break;
                            }
                        }
                        break;
                    case "xOffset":
                        XOff = float.Parse(val);
                        break;
                    case "yOffset":
                        YOff = float.Parse(val);
                        break;
                    case "zOffset":
                        ZOff = float.Parse(val);
                        break;
                    case "h":
                        H = float.Parse(val);
                        break;
                    case "lacunarity":
                        Lacunarity = float.Parse(val);
                        break;
                    case "octaves":
                        Octaves = float.Parse(val);
                        break;
                    case "metersPerPerlinUnit":
                        MetersPerPerlinUnit = float.Parse(val);
                        break;
                    case "heightScale":
                        HeightScale = float.Parse(val);
                        break;
                    case "heightOffset":
                        HeightOffset = float.Parse(val);
                        break;
                    case "fractalOffset":
                        FractalOffset = float.Parse(val);
                        break;
                    case "heightFloor":
                        HeightFloor = float.Parse(val);
                        break;
                    case "seedMapOriginX":
                        SeedMapOriginX = float.Parse(val);
                        break;
                    case "seedMapOriginY":
                        SeedMapOriginY = float.Parse(val);
                        break;
                    case "seedMapOriginZ":
                        SeedMapOriginZ = float.Parse(val);
                        break;
                    case "seedMapMetersPerSample":
                        SeedMapMetersPerSample = float.Parse(val);
                        break;
                    case "outsideSeedMapHeight":
                        OutsideMapSeedHeight = float.Parse(val);
                        break;
                }
            }
        }
 public override int ReadElementContentAsBase64(byte[] buffer, int index, int count)
 {
     CheckAsync();
     return(_coreReader.ReadElementContentAsBase64(buffer, index, count));
 }
        protected virtual object ReadValueFromXmlReader(XmlReader xmlReader)
        {
            xmlReader.MoveToContent();
            using (var memoryStream = new MemoryStream())
            {
                const int BUFFER_SIZE = 4096;
                byte[] buffer = new byte[BUFFER_SIZE];
                int readByteCount = 0;

                while ((readByteCount = xmlReader.ReadElementContentAsBase64(buffer, 0, BUFFER_SIZE)) > 0)
                {
                    memoryStream.Write(buffer, 0, readByteCount);
                }
                memoryStream.Seek(0, SeekOrigin.Begin);
                var deserializedObject = _binarySer.Deserialize(memoryStream);
                return deserializedObject;
            }
        }
        private void ReadAdditionalFiles(XmlReader reader, Release release)
        {
            reader.AssertElementStart(Keys.AdditionalFiles);

            if (reader.IsEmptyElement)
            {
                return;
            }

            bool skipRead = false;
            while (skipRead || reader.Read())
            {
                skipRead = false;

                if (reader.IsElementEnd(Keys.AdditionalFiles))
                {
                    break;
                }

                reader.AssertElementStart(Keys.AdditionalFile);
                ReleaseAdditionalFile file = new ReleaseAdditionalFile()
                {
                    Type = Utility.ParseEnum<ReleaseAdditionalFileType>(reader.GetAttributeOrNull(Keys.Type)),
                    Description = reader.GetAttributeOrNull(Keys.Description),
                    OriginalFilename = reader.GetAttributeOrNull(Keys.OriginalFilename)
                };

                byte[] resultBuffer = new byte[0];
                int read;
                while ((read = reader.ReadElementContentAsBase64(this.readBuffer, 0, this.readBuffer.Length)) > 0)
                {
                    byte[] newResultBuffer = new byte[resultBuffer.Length + read];
                    Array.Copy(resultBuffer, newResultBuffer, resultBuffer.Length);
                    Array.Copy(this.readBuffer, 0, newResultBuffer, resultBuffer.Length, read);
                    resultBuffer = newResultBuffer;
                }
                file.File = resultBuffer;

                release.AdditionalFiles.Add(file);

                skipRead = true;
            }
        }
Example #12
0
        /// <summary>
        /// Constructor. Parses the LAYER part of the TMX map file. There can be many layers.
        /// </summary>
        /// <param name="xml">Assumes the LAYER part of the tmx map file</param>
        /// <param name="engine"></param>
        /// <param name="tileSet"></param>
        public Layer(XmlReader xml, Engine engine, TileSet tileSet)
        {
            // Move xml ahead
            xml.Read();
            // Read in layer name
            _name = xml.GetAttribute("name");
            // Read in layer width and height
            _width = int.Parse(xml.GetAttribute("width"));
            _height = int.Parse(xml.GetAttribute("height"));

            // Parse the layer block
            while (xml.Read())
            {
                switch (xml.Name)
                {
                    // We currently only support the data block for tile layers, properties are thus ignored
                    case "data":
                        // We create a byte buffer to read in the base64 encoded data, which may or may not be compressed
                        int dataSize = (_width * _height * 4) + 1024;
                        var buffer = new byte[dataSize];
                        if (xml.CanReadBinaryContent)
                        {
                            // Read base64 content info buffer
                            xml.ReadElementContentAsBase64(buffer, 0, dataSize);
                            // We create a memory stream of the buffer
                            Stream stream = new MemoryStream(buffer, false);

                            // At this point it would be natural to check for gzip, zlib or uncompressed xml data since these are all options in Tiled
                            // However, due to time constraints I've only implemented the default setting, zlib compressed base64 encoded data.
                            // The first two bytes are Zlib specific identifiers, we skip those before decompressing
                            stream.ReadByte();
                            stream.ReadByte();
                            stream = new DeflateStream(stream, CompressionMode.Decompress);

                            // At this point we have a decompressed base64 stream which we need to "parse".
                            // For that we use a binary reader to read out the tile ids within the layer.
                            // In pure XML, layer data is just a long list of tile ids which represent the tile within the tileset used.
                            // So we need to loop through the layer width by height, as each tile is listed in this order.
                            using (stream)
                            using (var br = new BinaryReader(stream))
                            {
                                // Loop layer height
                                for (int y = 0; y < _height; y++)
                                {
                                    // Loop layer width
                                    for (int x = 0; x < _width; x++)
                                    {
                                        // tileId represent the tile in the tileset
                                        int tileId = br.ReadInt32();
                                        // Tile id 0 means nothing was "painted" in this tile so we skip that.
                                        if (tileId > 0)
                                        {
                                            // Get the TileSetTile using the id we extracted
                                            TileSetTile t = tileSet.getTileById(tileId);
                                            if (t is TileSetTile)
                                            {
                                                // At this point we check if this tile has a collidable property, if so, we create a new collidable and add to the engine collidable list.
                                                if (t.getProperty("Collidable") is string && t.getProperty("Collidable").Equals("True"))
                                                {
                                                    engine.AddCollidable(
                                                        new Collidable(
                                                            new Rectangle(
                                                                x * tileSet.tileWidth,
                                                                y * tileSet.tileHeight,
                                                                tileSet.tileWidth,
                                                                tileSet.tileHeight
                                                            )
                                                        )
                                                    );
                                                }
                                                // Add the new layertile to the layer list of tiles
                                                _layerTiles.Add(new LayerTile(t, x, y));
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        break;
                }
            }
        }