/// <summary> /// Creates <see cref="JpegImage"/> from stream with an arbitrary image data /// </summary> /// <param name="imageData">Stream containing bytes of image in /// arbitrary format (BMP, Jpeg, GIF, PNG, TIFF, e.t.c)</param> public JpegImage(Stream imageData) { if (imageData is null) { throw new ArgumentNullException(nameof(imageData)); } m_compressedData = new MemoryStream(); imageData.CopyTo(m_compressedData); if (CompressedData.Length <= 2) { throw new ArgumentException("There must be at least two bytes in the image stream", nameof(imageData)); } CompressedData.Seek(0, SeekOrigin.Begin); var first = CompressedData.ReadByte(); var second = CompressedData.ReadByte(); if (!(first == 0xFF) && (second == (int)JpegMarker.SOI)) { throw new ArgumentException("This is not a JPEG stream", nameof(imageData)); } Decompress(); }
public void Read(string fileName, MyCancellationToken cancellationToken) { using (var compressedFile = new FileStream(fileName, FileMode.Open)) { while (compressedFile.Position < compressedFile.Length && !cancellationToken.IsCancelled) { var lengthBuffer = new byte[8]; compressedFile.Read(lengthBuffer, 0, lengthBuffer.Length); var blockLength = BitConverter.ToInt32(lengthBuffer, 4); var compressedData = new byte[blockLength]; lengthBuffer.CopyTo(compressedData, 0); compressedFile.Read(compressedData, 8, blockLength - 8); int dataSize = BitConverter.ToInt32(compressedData, blockLength - 4); byte[] lastBuffer = new byte[dataSize]; var _block = new CompressedData { Buffer = compressedData, UncompressedSize = dataSize }; ConsoleIndicator.ShowProgress(compressedFile.Position, compressedFile.Length); _inputStorage.Add(_block); } _inputStorage.Close(); } }
protected override EncapsulatedData ParseNonNullNode(ParseContext context, XmlNode node, BareANY bareAny, Type expectedReturnType , XmlToModelResult result) { XmlElement element = (XmlElement)node; EncapsulatedData ed = new EncapsulatedData(); if (!this.isR2 && element.HasAttribute("compression")) { ed = new CompressedData(); } HandleRepresentation(ed, element, context, result); HandleMediaType(ed, element, result); HandleLanguage(ed, element, result); HandleCompression(ed, element, result); HandleIntegrityCheck(ed, element, context, result); HandleIntegrityCheckAlgorithm(ed, element, context, result); ValidateInnerNodes(element, result); HandleContent(ed, element, result, context); HandleReference(ed, element, result, context); HandleThumbnail(ed, element, result, context); HandleConstraints(ed, context.GetConstraints(), element, result); if (!this.isR2) { Validate(ed, element, context, result); } if (ed.IsEmpty()) { ed = null; } return(ed); }
private void WritingThread() { try { while (true) { CompressedData compressedBlock = _writeQueue.Take(); // Wait for the block to be compressed. compressedBlock._autoEvent.WaitOne(); for (int i = 0; i < compressedBlock._buffers.Count; ++i) { _writer.Write(compressedBlock._buffers[i], 0, compressedBlock._bufferSizes[i]); } } } catch (IOException e) { throw new IOException( string.Format("ERROR: An IO exception occurred when flushing the BGZF block: {0}", e.Message)); } catch (InvalidOperationException) { // We're done. There are no more blocks to process. } }
public override int GetHashCode() { int hash = 1; hash ^= shape_.GetHashCode(); if (CompressionType != 0) { hash ^= CompressionType.GetHashCode(); } if (observationDataCase_ == ObservationDataOneofCase.CompressedData) { hash ^= CompressedData.GetHashCode(); } if (observationDataCase_ == ObservationDataOneofCase.FloatData) { hash ^= FloatData.GetHashCode(); } hash ^= compressedChannelMapping_.GetHashCode(); hash ^= dimensionProperties_.GetHashCode(); if (ObservationType != 0) { hash ^= ObservationType.GetHashCode(); } hash ^= (int)observationDataCase_; if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } return(hash); }
public string Decompress(byte[] input) { var data = new CompressedData(); data.Load(input); _huffmanTree.Build(data.Frequencies); var originalLength = data.OriginalLength; var output = new char[originalLength]; var bits = new BitReader(data.Data); var node = _huffmanTree.Root; var position = 0; while (position < originalLength) { if (node.Character != '\0') { output[position] = node.Character; node = _huffmanTree.Root; position++; } node = bits.Read() ? node.Right : node.Left; } return(new string(output)); }
public byte[] GetContent() { CompressedData comData = CompressedData.GetInstance(contentInfo.Content); ContentInfo content = comData.EncapContentInfo; Asn1OctetString bytes = (Asn1OctetString)content.Content; //InflaterInputStream zIn = new InflaterInputStream(new MemoryStream(bytes.GetOctets(), false)); MemoryStream bOut = new MemoryStream(); byte[] buf = new byte[1024]; //int len; try { //while ((len = zIn.Read(buf, 0, buf.Length)) > 0) //{ // bOut.Write(buf, 0, len); //} } catch (IOException e) { throw new CmsException("exception reading compressed stream.", e); } return(bOut.ToArray()); }
public override void Write() { _worldPacket.WritePackedGuid(Player); _worldPacket.WriteUInt32(Time); _worldPacket.WriteUInt32(Size); _worldPacket.WriteBits(DataType, 3); var bytes = CompressedData.GetData(); _worldPacket.WriteUInt32(bytes.Length); _worldPacket.WriteBytes(bytes); }
public virtual void TestFormatValueCompressedXmlDataEmptyContent() { byte[] content = System.Text.ASCIIEncoding.ASCII.GetBytes(string.Empty); string expectedResult = "<name compression=\"GZ\" language=\"en-CA\" mediaType=\"text/xml\"/>" + SystemUtils.LINE_SEPARATOR; EncapsulatedData data = new CompressedData(Ca.Infoway.Messagebuilder.Domainvalue.Basic.X_DocumentMediaType.XML_TEXT, null , content, Compression.GZIP, "en-CA"); string result = new EdPropertyFormatter().Format(GetContext("name", "ED.DOC"), new EDImpl <EncapsulatedData>(data)); Assert.IsFalse(this.result.IsValid()); Assert.AreEqual(1, this.result.GetHl7Errors().Count); Assert.AreEqual(ClearPayload(expectedResult), ClearPayload(result), "element"); }
public virtual void TestParseValueCompressedTextData() { XmlNode node = CreateNode("<name compression=\"GZ\" language=\"en-CA\" mediaType=\"text/plain\" representation=\"B64\">" + "H4sIAAAAAAAAALOpyM2xS8vPt9EHMQATOK6nDgAAAA==</name>"); CompressedData data = (CompressedData) new EdElementParser().Parse(CreateContext("ED.DOC", SpecificationVersion.V02R02), node , this.xmlResult).BareValue; Assert.IsTrue(this.xmlResult.IsValid()); Assert.AreEqual(Ca.Infoway.Messagebuilder.Domainvalue.Basic.X_DocumentMediaType.PLAIN_TEXT, data.MediaType, "media type"); Assert.AreEqual(Compression.GZIP, data.Compression, "Compression type"); Assert.AreEqual("en-CA", data.Language, "language"); Assert.AreEqual("<xml>foo</xml>", BytesUtil.AsString(data.UncompressedContent), "content"); }
public virtual void TestFormatValueCompressedXmlDataNullContent() { string expectedResult = "<name compression=\"GZ\" language=\"en-CA\" mediaType=\"text/xml\"><reference value=\"http://www.i-proving.ca\"/></name>"; EncapsulatedData data = new CompressedData(Ca.Infoway.Messagebuilder.Domainvalue.Basic.X_DocumentMediaType.XML_TEXT, null , null, Compression.GZIP, "en-CA"); data.ReferenceObj = new TelecommunicationAddress(Ca.Infoway.Messagebuilder.Domainvalue.Basic.URLScheme.HTTP, "www.i-proving.ca" ); string result = new EdPropertyFormatter().Format(GetContext("name", "ED.DOC"), new EDImpl <EncapsulatedData>(data)); Assert.IsFalse(this.result.IsValid()); Assert.AreEqual(1, this.result.GetHl7Errors().Count); Assert.AreEqual(expectedResult, ClearPayload(result), "something in text node"); }
public virtual void TestParseValueCompressedXmlNullData() { XmlNode node = CreateNode("<name compression=\"GZ\" language=\"en-CA\" mediaType=\"text/xml\" representation=\"B64\"></name>" ); CompressedData data = (CompressedData) new EdElementParser().Parse(CreateContext("ED.DOC", SpecificationVersion.V02R02), node , this.xmlResult).BareValue; Assert.IsFalse(this.xmlResult.IsValid()); Assert.AreEqual(1, this.xmlResult.GetHl7Errors().Count); // content missing Assert.AreEqual(Ca.Infoway.Messagebuilder.Domainvalue.Basic.X_DocumentMediaType.XML_TEXT, data.MediaType, "media type"); Assert.AreEqual(Compression.GZIP, data.Compression, "Compression type"); Assert.AreEqual("en-CA", data.Language, "language"); Assert.IsNull(data.UncompressedContent, "content"); }
public byte[] Decode() { var compressedData = new CompressedData(this.Bytes.ToArray()); var huffmanResult = new HuffmanResult( bytesCounts: compressedData.HufBytesCounts, bits: compressedData.HufBits); var hufResult = Huffman(huffmanResult); var rleResult1 = RLE(hufResult); var mtfResult = MTF(rleResult1); var bwtResult = BWT(mtfResult, compressedData.BwtInitialStringIndex); var rleResult2 = RLE(bwtResult); return(rleResult2.ToArray()); }
public byte[] GetContent(int limit) { CompressedData instance = CompressedData.GetInstance(contentInfo.Content); ContentInfo encapContentInfo = instance.EncapContentInfo; Asn1OctetString asn1OctetString = (Asn1OctetString)encapContentInfo.Content; ZInputStream inStream = new ZInputStream(new MemoryStream(asn1OctetString.GetOctets(), writable: false)); try { return(CmsUtilities.StreamToByteArray(inStream, limit)); } catch (IOException e) { throw new CmsException("exception reading compressed stream.", e); } }
public byte[] Encode() { var rleResult1 = RLE(this.Bytes); var bwtResult = BWT(rleResult1); var mtfResult = MTF(bwtResult.Bytes); var rleResult2 = RLE(mtfResult); var hufResult = Huffman(rleResult2); CompressedData compressedData = new CompressedData( bwtInitialStringIndex: bwtResult.InitialStringIndex, hufSymbolsCount: hufResult.BytesCounts.Count, hufBytesCounts: hufResult.BytesCounts, hufBits: hufResult.Bits); byte[] result = compressedData.ToByteArray(); return(result); }
public virtual void TestFormatValueCompressedTextData() { string contentAsString = "<xml>foo</xml>"; byte[] content = System.Text.ASCIIEncoding.ASCII.GetBytes(contentAsString); string finalContent = Base64.EncodeBase64String(Compression.Gzip(content)); string expectedResult = "<name compression=\"GZ\" language=\"en-CA\" mediaType=\"text/plain\" representation=\"B64\">" + finalContent + "</name>" + SystemUtils.LINE_SEPARATOR; EncapsulatedData data = new CompressedData(Ca.Infoway.Messagebuilder.Domainvalue.Basic.X_DocumentMediaType.PLAIN_TEXT, null , System.Text.ASCIIEncoding.ASCII.GetBytes(finalContent), Compression.GZIP, "en-CA"); data.Representation = EdRepresentation.B64; string result = new EdPropertyFormatter().Format(GetContext("name", "ED.DOC"), new EDImpl <EncapsulatedData>(data)); Assert.IsTrue(this.result.IsValid()); Assert.AreEqual(ClearPayload(expectedResult), ClearPayload(result), "element"); Assert.AreEqual(DecodeAndUnzip(ExtractPayload(result)), contentAsString, "element payload"); }
public override void Write() { _worldPacket.WritePackedGuid(Player); _worldPacket.WriteInt64(Time); _worldPacket.WriteUInt32(Size); _worldPacket.WriteBits(DataType, 4); if (CompressedData == null) { _worldPacket.WriteUInt32(0); } else { var bytes = CompressedData.GetData(); _worldPacket.WriteInt32(bytes.Length); _worldPacket.WriteBytes(bytes); } }
/** * Return the uncompressed content, throwing an exception if the data size * is greater than the passed in limit. If the content is exceeded getCause() * on the CMSException will contain a StreamOverflowException * * @param limit maximum number of bytes to read * @return the content read * @throws CMSException if there is an exception uncompressing the data. */ public byte[] GetContent(int limit) { CompressedData comData = CompressedData.GetInstance(contentInfo.Content); ContentInfo content = comData.EncapContentInfo; Asn1OctetString bytes = (Asn1OctetString)content.Content; ZInflaterInputStream zIn = new ZInflaterInputStream(new MemoryStream(bytes.GetOctets(), false)); try { return(CmsUtilities.StreamToByteArray(zIn, limit)); } catch (IOException e) { throw new CmsException("exception reading compressed stream.", e); } }
private static void ExtractMetadata(List <object> row, CompressedData data, BrotliGlobalState state) { row.Add(data.InsertCopyCommands.Count); int literalBytes = 0; int backRefBytes = 0; int backRefCount = 0; int dictRefBytes = 0; int dictRefCount = 0; foreach (var command in data.InsertCopyCommands) { var literals = command.Literals; var copyLength = command.CopyLength; var copyDistance = command.CopyDistance; state.OutputLiterals(literals); literalBytes += literals.Count; if (copyDistance == DistanceInfo.EndsAfterLiterals) { break; } var copy = state.OutputCopy(copyLength, copyDistance); if (copy.IsBackReference) { backRefBytes += copy.BytesWritten; ++backRefCount; } else { dictRefBytes += copy.BytesWritten; ++dictRefCount; } } row.Add(literalBytes); row.Add(backRefBytes); row.Add(dictRefBytes); row.Add(backRefCount); row.Add(dictRefCount); }
// flushes the data in the BGZF block protected void FlushBlock() { if (_compressQueue != null) { // Save the block to the queue to be processed on multiple threads. CompressedData compressedData = new CompressedData(); _writeQueue.Add(compressedData); _compressQueue.Add(new Data((byte[])UncompressedBlock.Clone(), BlockOffset, compressedData)); BlockOffset = 0; } else { // flush all of the remaining blocks while (BlockOffset > 0) { BlockAddress += FlushSingleBlock(); } } }
/** * Return the uncompressed content. * * @return the uncompressed content * @throws CmsException if there is an exception uncompressing the data. */ public byte[] GetContent() { CompressedData comData = CompressedData.GetInstance(ContentInfoInternal.Content); ContentInfo content = comData.EncapContentInfo; var bytes = (Asn1OctetString)content.Content; using (var zIn = new ZInputStream(bytes.GetOctetStream())) { try { return(CmsUtilities.StreamToByteArray(zIn)); } catch (IOException e) { throw new CmsException("exception reading compressed stream.", e); } } }
public byte[] GetContent(int limit) { //IL_002b: Unknown result type (might be due to invalid IL or missing references) //IL_0035: Expected O, but got Unknown //IL_0043: Expected O, but got Unknown CompressedData instance = CompressedData.GetInstance(contentInfo.Content); ContentInfo encapContentInfo = instance.EncapContentInfo; Asn1OctetString asn1OctetString = (Asn1OctetString)encapContentInfo.Content; ZInputStream inStream = new ZInputStream((Stream) new MemoryStream(asn1OctetString.GetOctets(), false)); try { return(CmsUtilities.StreamToByteArray((Stream)(object)inStream, limit)); } catch (IOException val) { IOException e = val; throw new CmsException("exception reading compressed stream.", (global::System.Exception)(object) e); } }
public byte[] GetContent() { CompressedData instance = CompressedData.GetInstance(contentInfo.Content); ContentInfo encapContentInfo = instance.EncapContentInfo; Asn1OctetString asn1OctetString = (Asn1OctetString)encapContentInfo.Content; ZInputStream zInputStream = new ZInputStream(asn1OctetString.GetOctetStream()); try { return(CmsUtilities.StreamToByteArray(zInputStream)); } catch (IOException e) { throw new CmsException("exception reading compressed stream.", e); } finally { Platform.Dispose(zInputStream); } }
/** * Return the uncompressed content. * * @return the uncompressed content * @throws CmsException if there is an exception uncompressing the data. */ public byte[] GetContent() { CompressedData comData = CompressedData.GetInstance(contentInfo.Content); ContentInfo content = comData.EncapContentInfo; Asn1OctetString bytes = (Asn1OctetString)content.Content; ZInflaterInputStream zIn = new ZInflaterInputStream(bytes.GetOctetStream()); try { return(CmsUtilities.StreamToByteArray(zIn)); } catch (IOException e) { throw new CmsException("exception reading compressed stream.", e); } finally { zIn.Close(); } }
/** * Return the uncompressed content. * * @return the uncompressed content * @throws CmsException if there is an exception uncompressing the data. */ public byte[] GetContent() { CompressedData comData = CompressedData.GetInstance(contentInfo.Content); ContentInfo content = comData.EncapContentInfo; Asn1OctetString bytes = (Asn1OctetString)content.Content; ZInputStream zIn = new ZInputStream(bytes.GetOctetStream()); try { return(CmsUtilities.StreamToByteArray(zIn)); } catch (IOException e) { throw new CmsException("exception reading compressed stream.", e); } finally { BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(zIn); } }
void DecompressAndStore(object data) { Debug.Log("1"); CompressedData compressedData = (CompressedData)data; Debug.Log("2"); byte[] decompressedPayload = DeCompress(compressedData.getCompressedData(), compressedData.getOriginalLength()); Debug.Log("3"); if (decompressedPayload.Length > 0) { float[] samples = new float[decompressedPayload.Length / 4]; Buffer.BlockCopy(decompressedPayload, 0, samples, 0, decompressedPayload.Length); Debug.Log("4"); AddClipToQueue(samples); Debug.Log("5"); } else { Debug.Log("Attempted to decompress invalid data, Audio Chunk Lost"); } }
public byte[] GetContent() { //IL_003c: Expected O, but got Unknown CompressedData instance = CompressedData.GetInstance(contentInfo.Content); ContentInfo encapContentInfo = instance.EncapContentInfo; Asn1OctetString asn1OctetString = (Asn1OctetString)encapContentInfo.Content; ZInputStream zInputStream = new ZInputStream(asn1OctetString.GetOctetStream()); try { return(CmsUtilities.StreamToByteArray((Stream)(object)zInputStream)); } catch (IOException val) { IOException e = val; throw new CmsException("exception reading compressed stream.", (global::System.Exception)(object) e); } finally { Platform.Dispose((Stream)(object)zInputStream); } }
private ITestResult CompressionTest() { try { ContentInfo info = ContentInfo.GetInstance( Asn1Object.FromByteArray(compData)); CompressedData data = CompressedData.GetInstance(info.Content); data = new CompressedData(data.CompressionAlgorithmIdentifier, data.EncapContentInfo); info = new ContentInfo(CmsObjectIdentifiers.CompressedData, data); if (!Arrays.AreEqual(info.GetEncoded(), compData)) { return(new SimpleTestResult(false, Name + ": CMS compression failed to re-encode")); } return(new SimpleTestResult(true, Name + ": Okay")); } catch (Exception e) { return(new SimpleTestResult(false, Name + ": CMS compression failed - " + e.ToString(), e)); } }
// writes data to the BGZF block protected int Write(byte[] data, uint dataLength) { // initialize const int blockLength = MaxBlockSize; int numBytesWritten = 0; int inputIndex = 0; // copy the data to the buffer while (numBytesWritten < dataLength) { int copyLength = Math.Min(blockLength - BlockOffset, (int)dataLength - numBytesWritten); Buffer.BlockCopy(data, inputIndex, UncompressedBlock, BlockOffset, copyLength); BlockOffset += copyLength; inputIndex += copyLength; numBytesWritten += copyLength; if (BlockOffset == blockLength) { if (_compressQueue != null) { // Save the block to the queue to be processed on multiple threads. CompressedData compressedData = new CompressedData(); _writeQueue.Add(compressedData); _compressQueue.Add(new Data((byte[])UncompressedBlock.Clone(), blockLength, compressedData)); BlockOffset = 0; } else { FlushBlock(); } } } return(numBytesWritten); }
public override int GetHashCode() { int hash = 1; hash ^= shape_.GetHashCode(); if (CompressionType != global::MLAgents.CommunicatorObjects.CompressionTypeProto.None) { hash ^= CompressionType.GetHashCode(); } if (observationDataCase_ == ObservationDataOneofCase.CompressedData) { hash ^= CompressedData.GetHashCode(); } if (observationDataCase_ == ObservationDataOneofCase.FloatData) { hash ^= FloatData.GetHashCode(); } hash ^= (int)observationDataCase_; if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } return(hash); }
public Data(byte[] buffer, int bufferSize, CompressedData compressedData) { _buffer = buffer; _bufferSize = bufferSize; _compressedData = compressedData; }