public void Initialize() { _resourceReleaser = new MockResourceReleaser <byte[]>(); byte[] bytes = new byte[256]; for (int i = 0; i < 256; ++i) { bytes[i] = (byte)i; } Stream unbufferedStream = new MemoryStream(bytes); _buffer = new byte[10]; _pooledByteArrayBufferedInputStream = new PooledByteArrayBufferedInputStream( unbufferedStream, _buffer, _resourceReleaser); }
/// <summary> /// If this is the first time calling this method, the buffer will /// be checked to make sure it starts with SOI marker (0xffd8). /// If the image has been identified as a non-JPEG, data will be /// ignored and false will be returned immediately on all /// subsequent calls. /// /// This object maintains state of the position of the last read /// byte. On repeated calls to this method, it will continue from /// where it left off. /// </summary> /// <param name="encodedImage"> /// Next set of bytes received by the caller. /// </param> /// <returns>true if a new full scan has been found.</returns> public bool ParseMoreData(EncodedImage encodedImage) { if (_parserState == NOT_A_JPEG) { return(false); } int dataBufferSize = encodedImage.Size; // Is there any new data to parse? // _bytesParsed might be greater than size of dataBuffer - that // happens when we skip more data than is available to read // inside DoParseMoreData method. if (dataBufferSize <= _bytesParsed) { return(false); } Stream bufferedDataStream = new PooledByteArrayBufferedInputStream( encodedImage.GetInputStream(), _byteArrayPool.Get(BUFFER_SIZE), _byteArrayPool); try { StreamUtil.Skip(bufferedDataStream, _bytesParsed); return(DoParseMoreData(encodedImage, bufferedDataStream)); } catch (IOException) { // Does not happen - streams returned by IPooledByteBuffers // do not throw IOExceptions. throw; } finally { Closeables.CloseQuietly(bufferedDataStream); } }