@Override public synchronized long skip(long n) throws IOException { byte[] buffer = getSkipBufferLocked(); long total = 0; while (total < n) { long bytesDiff = n - total; int bytesToRead = (int) Math.min((long) buffer.length, bytesDiff); int result = this.read(buffer, 0, bytesToRead); if (result == -1) { break; } total += result; } return total; }
@Override public int read() throws IOException { try { int result = checkEOF(in.read()); if (result != -1) { mResponseHandler.onRead(1); writeToOutputStream(result); } return result; } catch (IOException ex) { throw handleIOException(ex); } }
@Override public void close() throws IOException { try { long bytesRead = 0; if (!mEofSeen) { byte[] buffer = new byte[BUFFER_SIZE]; int count; while ((count = this.read(buffer)) != -1) { bytesRead += count; } } if (bytesRead > 0) { CLog.writeToConsole( mNetworkPeerManager, Console.MessageLevel.ERROR, Console.MessageSource.NETWORK, "There were " + String.valueOf(bytesRead) + " bytes that were not consumed while " + "processing request " + mRequestId); } } finally { super.close(); closeOutputStreamQuietly(); } }
/** * Same as {@link #writeToOutputStream(int)}, but we write a buffer instead. */ private synchronized void writeToOutputStream(byte[] b, int offset, int count) { if (mClosed) { return; } try { mOutputStream.write(b, offset, count); reportDecodedSizeIfApplicable(); } catch (IOException e) { handleIOExceptionWritingToStream(e); } }
@Test public void testReadFully() throws IOException { byte[] tempReadingBuffer = new byte[TEST_RESPONSE_BODY.length]; int result = mResponseHandlingInputStream.read(tempReadingBuffer); assertEquals(TEST_RESPONSE_BODY.length, result); assertBufferMatchesResponseBody(tempReadingBuffer, TEST_RESPONSE_BODY.length); assertBufferMatchesResponseBody(mTestOutputStream.toByteArray(), TEST_RESPONSE_BODY.length); PowerMockito.mockStatic(CLog.class); PowerMockito.verifyZeroInteractions(CLog.class); mResponseHandlingInputStream.close(); PowerMockito.verifyStatic(); }
@Test public void testSwallowException() throws IOException { OutputStream exceptionOutputStream = new OutputStream() { @Override public void write(int oneByte) throws IOException { throw new TestIOException(); } }; ResponseHandlingInputStream responseHandlingInputStream = new ResponseHandlingInputStream( new ByteArrayInputStream(TEST_RESPONSE_BODY), TEST_REQUEST_ID, exceptionOutputStream, null /* decompressedCounter */, mNetworkPeerManager, new DefaultResponseHandler(mNetworkEventReporter, TEST_REQUEST_ID)); PowerMockito.mockStatic(CLog.class); responseHandlingInputStream.read(); PowerMockito.verifyStatic(); }
@Test public void testSkipMany() throws IOException { long numBytesToSkip = TEST_RESPONSE_BODY.length * 2; long result = mResponseHandlingInputStream.skip(numBytesToSkip); assertEquals((long) TEST_RESPONSE_BODY.length, result); assertBufferMatchesResponseBody(mTestOutputStream.toByteArray(), TEST_RESPONSE_BODY.length); PowerMockito.verifyZeroInteractions(CLog.class); mResponseHandlingInputStream.close(); }
return new ResponseHandlingInputStream( availableInputStream, requestId,
@Override public int read(byte[] b) throws IOException { return this.read(b, 0, b.length); }
private void handleIOExceptionWritingToStream(IOException e) { CLog.writeToConsole( mNetworkPeerManager, Console.MessageLevel.ERROR, Console.MessageSource.NETWORK, "Could not write response body to the stream " + e); closeOutputStreamQuietly(); }
/** * Attempts to close all the output stream, and swallows any exceptions. */ private synchronized void closeOutputStreamQuietly() { if (!mClosed) { try { mOutputStream.close(); reportDecodedSizeIfApplicable(); } catch (IOException e) { CLog.writeToConsole( mNetworkPeerManager, Console.MessageLevel.ERROR, Console.MessageSource.NETWORK, "Could not close the output stream" + e); } finally { mClosed = true; } } }
@Test public void testReadOneByte() throws IOException { int result = mResponseHandlingInputStream.read(); assertEquals(TEST_RESPONSE_BODY[0], positionToByte(result)); assertBufferMatchesResponseBody(mTestOutputStream.toByteArray(), 1); PowerMockito.mockStatic(CLog.class); PowerMockito.doNothing().when(CLog.class); CLog.writeToConsole( Mockito.any(ChromePeerManager.class), Mockito.any(Console.MessageLevel.class), Mockito.any(Console.MessageSource.class), Mockito.anyString()); mResponseHandlingInputStream.close(); PowerMockito.verifyStatic(); }
@Test public void testSkipFew() throws IOException { long numBytesToSkip = TEST_RESPONSE_BODY.length / 2; long result = mResponseHandlingInputStream.skip(numBytesToSkip); assertEquals(numBytesToSkip, result); assertBufferMatchesResponseBody(mTestOutputStream.toByteArray(), (int) numBytesToSkip); PowerMockito.mockStatic(CLog.class); PowerMockito.doNothing().when(CLog.class); CLog.writeToConsole( Mockito.any(ChromePeerManager.class), Mockito.any(Console.MessageLevel.class), Mockito.any(Console.MessageSource.class), Mockito.anyString()); mResponseHandlingInputStream.close(); PowerMockito.verifyStatic(); }
/** * Writes the byte to all the output streams. If we get an exception when writing to any * of the streams, we close all the streams, and then propagate the first exception that * occurred when writing. */ private synchronized void writeToOutputStream(int oneByte) { if (mClosed) { return; } try { mOutputStream.write(oneByte); reportDecodedSizeIfApplicable(); } catch (IOException e) { handleIOExceptionWritingToStream(e); } }
@Before public void setup() { mTestOutputStream = new ByteArrayOutputStream(); // The only place this is used is when trying to write to the console. Since we are going to // mock ResponseHandlingInputStream#writeToConsole passing null is fine. mNetworkPeerManager = null; mNetworkEventReporter = mock(NetworkEventReporterImpl.class); mResponseHandlingInputStream = new ResponseHandlingInputStream( new ByteArrayInputStream(TEST_RESPONSE_BODY), TEST_REQUEST_ID, mTestOutputStream, null /* decompressedCounter */, mNetworkPeerManager, new DefaultResponseHandler(mNetworkEventReporter, TEST_REQUEST_ID)); }
private synchronized int checkEOF(int n) { if (n == -1) { closeOutputStreamQuietly(); mResponseHandler.onEOF(); mEofSeen = true; } return n; }
@Test public void testReadPartial() throws IOException { int numBytesToRead = TEST_RESPONSE_BODY.length / 2; byte[] tempReadingBuffer = new byte[numBytesToRead]; int result = mResponseHandlingInputStream.read(tempReadingBuffer, 0, numBytesToRead); assertEquals(numBytesToRead, result); assertBufferMatchesResponseBody(tempReadingBuffer, numBytesToRead); assertBufferMatchesResponseBody(mTestOutputStream.toByteArray(), numBytesToRead); PowerMockito.mockStatic(CLog.class); PowerMockito.doNothing().when(CLog.class); CLog.writeToConsole( Mockito.any(ChromePeerManager.class), Mockito.any(Console.MessageLevel.class), Mockito.any(Console.MessageSource.class), Mockito.anyString()); mResponseHandlingInputStream.close(); PowerMockito.verifyStatic(); }
@Override public int read(byte[] b, int off, int len) throws IOException { try { int result = checkEOF(in.read(b, off, len)); if (result != -1) { mResponseHandler.onRead(result); writeToOutputStream(b, off, result); } return result; } catch (IOException ex) { throw handleIOException(ex); } }