@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(); } }
/** * 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; } } }
@Nullable private static String readBodyAsString( NetworkPeerManager peerManager, InspectorRequest request) { try { byte[] body = request.body(); if (body != null) { return new String(body, Utf8Charset.INSTANCE); } } catch (IOException | OutOfMemoryError e) { CLog.writeToConsole( peerManager, Console.MessageLevel.WARNING, Console.MessageSource.NETWORK, "Could not reproduce POST body: " + e); } return null; }
@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 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(); }
@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(); }
@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(); }
private @NonNull ScriptableObject initJsScope(@NonNull Context jsContext) { // Set the main Rhino goodies ImporterTopLevel importerTopLevel = new ImporterTopLevel(jsContext); ScriptableObject scope = jsContext.initStandardObjects(importerTopLevel, false); ScriptableObject.putProperty(scope, "context", Context.javaToJS(mContext, scope)); try { importClasses(jsContext, scope); importPackages(jsContext, scope); importConsole(scope); importVariables(scope); importFunctions(scope); } catch (StethoJsException e) { String message = String.format("%s\n%s", e.getMessage(), Log.getStackTraceString(e)); LogUtil.e(e, message); CLog.writeToConsole(Console.MessageLevel.ERROR, Console.MessageSource.JAVASCRIPT, message); } return scope; }
Database.ExecuteSQLResponse executeSQL(String databaseId, String query) throws JSONException { Timber.d("executeSQL: %s, %s", databaseId, query); Database.ExecuteSQLResponse response = new Database.ExecuteSQLResponse(); Matcher matcher = mPattern.matcher(query); if (!matcher.find()) { return response; } String docId = matcher.group(1); Timber.d("Parsed doc ID: %s", docId); Map<String, String> map = getDocument(databaseId, docId); response.columnNames = COLUMN_NAMES; response.values = new ArrayList<>(); for (Map.Entry<String, String> entry : map.entrySet()) { final String key = entry.getKey(); if (!mShowMetadata && key.substring(0,1).equals("_")) { continue; } response.values.add(key); response.values.add(entry.getValue()); } // Log to console CLog.writeToConsole(Console.MessageLevel.DEBUG, Console.MessageSource.JAVASCRIPT, new JSONObject(map).toString(4)); return response; }
private void handleIOExceptionWritingToStream(IOException e) { CLog.writeToConsole( mNetworkPeerManager, Console.MessageLevel.ERROR, Console.MessageSource.NETWORK, "Could not write response body to the stream " + e); closeOutputStreamQuietly(); }
@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 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(); }
@Override protected void log(int priority, String tag, String message, Throwable t) { ConsolePeerManager peerManager = ConsolePeerManager.getInstanceOrNull(); if (peerManager == null) { return; } Console.MessageLevel logLevel; switch (priority) { case Log.VERBOSE: case Log.DEBUG: logLevel = Console.MessageLevel.DEBUG; break; case Log.INFO: logLevel = Console.MessageLevel.LOG; break; case Log.WARN: logLevel = Console.MessageLevel.WARNING; break; case Log.ERROR: case Log.ASSERT: logLevel = Console.MessageLevel.ERROR; break; default: logLevel = Console.MessageLevel.LOG; } CLog.writeToConsole( logLevel, Console.MessageSource.OTHER, message ); }
public static void init(Gson gsonInstance) { CLog.writeToConsole(Console.MessageLevel.LOG, Console.MessageSource.OTHER, "TaskLibStetho.init"); TaskLibStetho.gsonInstance = gsonInstance; TaskEventListener.setInstance(new StethoEventListener()); }
@Override protected void onExecuteCalled(BaseTask task, Object sourceAndTarget) { CLog.writeToConsole(Console.MessageLevel.LOG, Console.MessageSource.OTHER, "onExecuteCalled"); bridge.reportTaskExecuteCalled(task, sourceAndTarget); }
@Override protected void onResult(BaseTask task) { CLog.writeToConsole(Console.MessageLevel.LOG, Console.MessageSource.OTHER, "onResult"); bridge.reportTaskFinished(task); }
@Override public InputStream interpretResponseStream( String requestId, @Nullable String contentType, @Nullable String contentEncoding, @Nullable InputStream availableInputStream, ResponseHandler responseHandler) { NetworkPeerManager peerManager = getPeerManagerIfEnabled(); if (peerManager != null) { if (availableInputStream == null) { responseHandler.onEOF(); return null; } Page.ResourceType resourceType = contentType != null ? getResourceTypeHelper().determineResourceType(contentType) : null; // There's this weird logic at play that only knows how to base64 decode certain kinds of // resources. boolean base64Encode = false; if (resourceType != null && resourceType == Page.ResourceType.IMAGE) { base64Encode = true; } try { OutputStream fileOutputStream = peerManager.getResponseBodyFileManager().openResponseBodyFile( requestId, base64Encode); return DecompressionHelper.teeInputWithDecompression( peerManager, requestId, availableInputStream, fileOutputStream, contentEncoding, responseHandler); } catch (IOException e) { CLog.writeToConsole( peerManager, Console.MessageLevel.ERROR, Console.MessageSource.NETWORK, "Error writing response body data for request #" + requestId); } } return availableInputStream; }
private static void log(Object [] rawArgs) { String message = JsFormat.parse(rawArgs); CLog.writeToConsole(MessageLevel.LOG, MessageSource.JAVASCRIPT, message); }