/// <summary> /// Copy all bytes from the input stream to the output stream. /// </summary> /// <param name="from">The intput stream.</param> /// <param name="to">The output stream.</param> /// <returns>Number of copied bytes.</returns> /// <exception cref="IOException"> /// An I/O error occurs. /// </exception> public long Copy(Stream from, Stream to) { long count = 0; byte[] tmp = _byteArrayPool.Get(_tempBufSize); try { while (true) { int read = from.Read(tmp, 0, _tempBufSize); if (read == 0) { return(count); } to.Write(tmp, 0, read); count += read; } } finally { _byteArrayPool.Release(tmp); } }
/// <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); } }
private void OnResponse( FetchState fetchState, Stream responseData, int responseContentLength) { PooledByteBufferOutputStream pooledOutputStream; if (responseContentLength > 0) { pooledOutputStream = _pooledByteBufferFactory.NewOutputStream(responseContentLength); } else { pooledOutputStream = _pooledByteBufferFactory.NewOutputStream(); } byte[] ioArray = _byteArrayPool.Get(READ_SIZE); try { int length; while ((length = responseData.Read(ioArray, 0, ioArray.Length)) > 0) { pooledOutputStream.Write(ioArray, 0, length); MaybeHandleIntermediateResult(pooledOutputStream, fetchState); float progress = CalculateProgress(pooledOutputStream.Size, responseContentLength); fetchState.Consumer.OnProgressUpdate(progress); } _networkFetcher.OnFetchCompletion(fetchState, pooledOutputStream.Size); HandleFinalResult(pooledOutputStream, fetchState); } finally { _byteArrayPool.Release(ioArray); pooledOutputStream.Dispose(); } }