public bool ReadStringFromInputStream(IInputStream inputStream, List<string> messages, out string s) { var bytesCollection = new List<byte[]>(); var bytes = new byte[Environment.SystemPageSize]; while (true) { var bytesRead = inputStream.Read(bytes, _timeout); if (bytesRead <= 0) break; var bytesCopy = new byte[bytesRead]; Buffer.BlockCopy(bytes, 0, bytesCopy, 0, bytesRead); bytesCollection.Add(bytesCopy); } var bytesCount = bytesCollection.Sum(bytesChunk => bytesChunk.Length); bytes = new byte[bytesCount]; var offset = 0; foreach (var bytesChunk in bytesCollection) { Buffer.BlockCopy(bytesChunk, 0, bytes, offset, bytesChunk.Length); offset += bytesChunk.Length; } try { s = _encoding.GetString(bytes); } catch (Exception e) { messages.Add(e.ToString()); s = null; return false; } return true; }
/// <exception cref="System.IO.IOException"></exception> public virtual int Read(byte[] a_bytes, int a_offset, int a_length) { int ret = _in.Read(a_bytes, a_offset, a_length); CheckEOF(ret); return(ret); }
// ---------- These methods aid in reading state from the stream ---------- /// <summary> /// Checks to see if there is a complete command waiting on the input stream. /// </summary> /// <param name="maxSize">The maximum number of bytes we are allowing before an /// <see cref="IOException"/> is thrown.</param> /// <remarks> /// If this method returns true then it is safe to go ahead and process a single /// command from this stream. This will return true only once while there is a /// command pending until that command is completely read in. /// </remarks> /// <returns> /// Returns true if there is a complete command. /// </returns> public bool PollForCommand(int maxSize) { lock (this) { if (markedLength == -1) { int available = input.Available; if (count > 0 || available > 0) { if ((count + available) > maxSize) { throw new IOException("Marked length is greater than max size ( " + (count + available) + " > " + maxSize + " )"); } EnsureCapacity(count + available); int readIn = input.Read(buf, count, available); if (readIn == 0) { //TODO: Check this format... // throw new EndOfStreamException(); // zero bytes read means that the stream is finished... return(false); } count = count + readIn; // Check: Is a complete command available? if (count >= 4) { int lengthMarker = ByteBuffer.ReadInt4(buf, 0); if (count >= lengthMarker + 4) { // Yes, complete command available. // mark this area up. markedLength = lengthMarker + 4; markedIndex = 4; return(true); } } } } return(false); } }
public void Write( IInputStream data ) { // TODO:IMPL bufferize!!! byte[] baseData = new byte[data.Length]; // Force the source pointer to the BOF (as we must copy the entire content)! data.Position = 0; // Read source content! data.Read(baseData, 0, baseData.Length); // Write target content! Write(baseData); }
/// <exception cref="System.IO.IOException"></exception> protected virtual void Copy(IInputStream rawin, Socket4Adapter sock, bool update) { BufferedInputStream @in = new BufferedInputStream(rawin); byte[] buffer = new byte[BlobImpl.CopybufferLength]; int bytesread = -1; while ((bytesread = rawin.Read(buffer)) >= 0) { sock.Write(buffer, 0, bytesread); if (update) { _currentByte += bytesread; } } @in.Close(); }
/// <exception cref="System.IO.IOException"></exception> protected virtual void Copy(IInputStream rawin, Socket4Adapter sock, bool update) { var @in = new BufferedInputStream(rawin); var buffer = new byte[BlobImpl.CopybufferLength]; var bytesread = -1; while ((bytesread = rawin.Read(buffer)) >= 0) { sock.Write(buffer, 0, bytesread); if (update) { _currentByte += bytesread; } } @in.Close(); }
public static Index Parse(IInputStream stream) { byte[][] data = new byte[stream.ReadUnsignedShort()][]; { int[] offsets = new int[data.Length + 1]; int offSize = stream.ReadByte(); for (int index = 0, count = offsets.Length; index < count; index++) { offsets[index] = stream.ReadInt(offSize); } for (int index = 0, count = data.Length; index < count; index++) { stream.Read(data[index] = new byte[offsets[index + 1] - offsets[index]]); } } return(new Index(data)); }
public Object VisitReadInputNode(IReadInputASTNode readNode) { if (readNode == null) { throw new ArgumentNullException("readNode", "The argument cannot equal to null"); } if (mInputStream == null) { throw new CRuntimeError("The data pointer is out of range"); } int[] readOpParameter = (int[])readNode.Index.Accept(this); int readDataIndex = readOpParameter.Length >= 1 ? readOpParameter[0] : 0; return(mInputStream.Read(readDataIndex)); }
/// <summary> /// Loads data from the input stream. /// </summary> /// <param name="count">The count of bytes to load into the intermediate buffer.</param> /// <returns>The operation.</returns> //public DataReaderLoadOperation LoadAsync(UInt32 count) public uint Load(UInt32 count) { if (stream == null) { throw new InvalidOperationException(); } if (bufferData.Length < count) { int size = 128; while (size < (count + UnconsumedBufferLength)) { size *= 2; } byte[] byteArray = new byte[size]; Array.Copy(bufferData, currentPosition, byteArray, 0, (int)UnconsumedBufferLength); currentPosition = 0; bufferData = byteArray; } IBuffer buffer = stream.Read(SyncBuffer(), count, InputStreamOptions); return((uint)bufferData.Length); }
public bool ReadStringFromInputStream(IInputStream inputStream, List <string> messages, out string s) { var bytesCollection = new List <byte[]>(); var bytes = new byte[Environment.SystemPageSize]; while (true) { var bytesRead = inputStream.Read(bytes, _timeout); if (bytesRead <= 0) { break; } var bytesCopy = new byte[bytesRead]; Buffer.BlockCopy(bytes, 0, bytesCopy, 0, bytesRead); bytesCollection.Add(bytesCopy); } var bytesCount = bytesCollection.Sum(bytesChunk => bytesChunk.Length); bytes = new byte[bytesCount]; var offset = 0; foreach (var bytesChunk in bytesCollection) { Buffer.BlockCopy(bytesChunk, 0, bytes, offset, bytesChunk.Length); offset += bytesChunk.Length; } try { s = _encoding.GetString(bytes); } catch (Exception e) { messages.Add(e.ToString()); s = null; return(false); } return(true); }
private void Fill() { if (_eos) { throw new ParserException(); } if (_length == 0) { _position = 0; } else if (_position != 0) { Relocate(); } var difference = _size - _length; var read = _source.Read(_buffer, _length, difference); if (read != difference) { _eos = true; } _length += read; }
private void Fill() { if (_eos) { throw Exception(LexerExceptionCodes.UnexpectedEndOfStream); } if (_length == 0) { _position = 0; } else if (_position != 0) { Relocate(); } var difference = _size - _length; var read = _source.Read(_buffer, _length, difference); if (read != difference) { _eos = true; } _length += read; }
public int Read() { return(_stream.Read()); }
public override PdfDataObject ParsePdfObject( ) { switch (TokenType) { case TokenTypeEnum.Keyword: if (Token is Reference) { Reference reference = (Reference)Token; return(new PdfReference(reference.ObjectNumber, reference.GenerationNumber, file)); } break; } PdfDataObject pdfObject = base.ParsePdfObject(); if (pdfObject is PdfDictionary) { IInputStream stream = Stream; int oldOffset = (int)stream.Position; MoveNext(); // Is this dictionary the header of a stream object [PDF:1.6:3.2.7]? if ((TokenType == TokenTypeEnum.Keyword) && Token.Equals(Keyword.BeginStream)) { PdfDictionary streamHeader = (PdfDictionary)pdfObject; // Keep track of current position! /* * NOTE: Indirect reference resolution is an outbound call which affects the stream pointer position, * so we need to recover our current position after it returns. */ long position = stream.Position; // Get the stream length! int length = ((PdfInteger)streamHeader.Resolve(PdfName.Length)).IntValue; // Move to the stream data beginning! stream.Seek(position); SkipEOL(); // Copy the stream data to the instance! byte[] data = new byte[length]; stream.Read(data); MoveNext(); // Postcondition (last token should be 'endstream' keyword). Object streamType = streamHeader[PdfName.Type]; if (PdfName.ObjStm.Equals(streamType)) // Object stream [PDF:1.6:3.4.6]. { return(new ObjectStream( streamHeader, new bytes.Buffer(data) )); } else if (PdfName.XRef.Equals(streamType)) // Cross-reference stream [PDF:1.6:3.4.7]. { return(new XRefStream( streamHeader, new bytes.Buffer(data) )); } else // Generic stream. { return(new PdfStream( streamHeader, new bytes.Buffer(data) )); } } else // Stand-alone dictionary. { stream.Seek(oldOffset); } // Restores postcondition (last token should be the dictionary end). } return(pdfObject); }
ScanResult ScanSegment(IInputStream stream, long offset) { stream.ScanTo(buffer, offset); stream.Read(buffer, blockSize); return(client.ScanBuffer(buffer, (uint)blockSize, $"{filePath}@{offset}")); }
/** * <summary>Parses the current PDF object [PDF:1.6:3.2].</summary> */ public PdfDataObject ParsePdfObject( ) { /* * NOTE: Object parsing is intrinsically a sequential operation tied to the stream pointer. * Calls bound towards other classes are potentially disruptive for the predictability of * the position of the stream pointer, so we are forced to carefully keep track of our * current position in order to recover its proper state after any outbound call. */ do { // Which token type? switch (tokenType) { case TokenTypeEnum.Integer: return(new PdfInteger((int)token)); case TokenTypeEnum.Name: return(new PdfName((string)token, true)); case TokenTypeEnum.Reference: /* * NOTE: Curiously, PDF references are the only primitive objects that require * a file reference. That's because they deal with indirect objects, which are strongly * coupled with the current state of the file: so, PDF references are the fundamental * bridge between the token layer and the file layer. */ return(new PdfReference( (Reference)token, file )); case TokenTypeEnum.Literal: return(new PdfTextString( Encoding.Encode((string)token) )); case TokenTypeEnum.DictionaryBegin: PdfDictionary dictionary = new PdfDictionary(); while (true) { // Key. MoveNext(); if (tokenType == TokenTypeEnum.DictionaryEnd) { break; } PdfName key = (PdfName)ParsePdfObject(); // Value. MoveNext(); PdfDirectObject value = (PdfDirectObject)ParsePdfObject(); // Add the current entry to the dictionary! dictionary[key] = value; } int oldOffset = (int)stream.Position; MoveNext(); // Is this dictionary the header of a stream object [PDF:1.6:3.2.7]? if ((tokenType == TokenTypeEnum.Keyword) && token.Equals(Keyword.BeginStream)) { // Keep track of current position! long position = stream.Position; // Get the stream length! /* * NOTE: Indirect reference resolution is an outbound call (stream pointer hazard!), * so we need to recover our current position after it returns. */ int length = ((PdfInteger)files.File.Resolve(dictionary[PdfName.Length])).RawValue; // Move to the stream data beginning! stream.Seek(position); SkipEOL(); // Copy the stream data to the instance! byte[] data = new byte[length]; stream.Read(data); MoveNext(); // Postcondition (last token should be 'endstream' keyword). Object streamType = dictionary[PdfName.Type]; if (PdfName.ObjStm.Equals(streamType)) // Object stream [PDF:1.6:3.4.6]. { return(new ObjectStream( dictionary, new bytes.Buffer(data), file )); } else if (PdfName.XRef.Equals(streamType)) // Cross-reference stream [PDF:1.6:3.4.7]. { return(new XRefStream( dictionary, new bytes.Buffer(data), file )); } else // Generic stream. { return(new PdfStream( dictionary, new bytes.Buffer(data) )); } } else // Stand-alone dictionary. { stream.Seek(oldOffset); // Restores postcondition (last token should be the dictionary end). return(dictionary); } case TokenTypeEnum.ArrayBegin: PdfArray array = new PdfArray(); while (true) { // Value. MoveNext(); if (tokenType == TokenTypeEnum.ArrayEnd) { break; } // Add the current item to the array! array.Add((PdfDirectObject)ParsePdfObject()); } return(array); case TokenTypeEnum.Real: return(new PdfReal((float)token)); case TokenTypeEnum.Boolean: return(PdfBoolean.Get((bool)token)); case TokenTypeEnum.Date: return(new PdfDate((DateTime)token)); case TokenTypeEnum.Hex: return(new PdfTextString( (string)token, PdfString.SerializationModeEnum.Hex )); case TokenTypeEnum.Null: return(null); case TokenTypeEnum.Comment: // NOOP: Comments are simply ignored and skipped. break; default: throw new Exception("Unknown type: " + tokenType); } } while(MoveNext()); return(null); }