public static synchronized NetworkPeerManager getOrCreateInstance(Context context) { if (sInstance == null) { sInstance = new NetworkPeerManager( new ResponseBodyFileManager( context.getApplicationContext())); } return sInstance; }
@Override protected void onLastPeerUnregistered() { mResponseBodyFileManager.cleanupFiles(); AsyncPrettyPrinterExecutorHolder.shutdown(); } };
public ResponseBodyData readFile(String requestId) throws IOException { InputStream in = mContext.openFileInput(getFilename(requestId)); try { int firstByte = in.read(); if (firstByte == -1) { throw new EOFException("Failed to read base64Encode byte"); } ResponseBodyData bodyData = new ResponseBodyData(); bodyData.base64Encoded = firstByte != 0; AsyncPrettyPrinter asyncPrettyPrinter = mRequestIdMap.get(requestId); if (asyncPrettyPrinter != null) { // TODO: this line blocks for up to 10 seconds and create problems as described // in issue #243 allow asynchronous dispatch for MethodDispatcher bodyData.data = prettyPrintContentWithTimeOut(asyncPrettyPrinter, in); } else { bodyData.data = Util.readAsUTF8(in); } return bodyData; } finally { in.close(); } }
public OutputStream openResponseBodyFile(String requestId, boolean base64Encode) throws IOException { OutputStream out = mContext.openFileOutput(getFilename(requestId), Context.MODE_PRIVATE); out.write(base64Encode ? 1 : 0); if (base64Encode) { return new Base64OutputStream(out, Base64.DEFAULT); } else { return out; } }
@Nullable private static AsyncPrettyPrinter initAsyncPrettyPrinterForResponse( InspectorResponse response, NetworkPeerManager peerManager) { AsyncPrettyPrinterRegistry registry = peerManager.getAsyncPrettyPrinterRegistry(); AsyncPrettyPrinter asyncPrettyPrinter = createPrettyPrinterForResponse(response, registry); if (asyncPrettyPrinter != null) { peerManager.getResponseBodyFileManager().associateAsyncPrettyPrinterWithId( response.requestId(), asyncPrettyPrinter); } return asyncPrettyPrinter; }
private GetResponseBodyResponse readResponseBody(String requestId) throws IOException, JsonRpcException { GetResponseBodyResponse response = new GetResponseBodyResponse(); ResponseBodyData bodyData; try { bodyData = mResponseBodyFileManager.readFile(requestId); } catch (OutOfMemoryError e) { throw new JsonRpcException(new JsonRpcError(JsonRpcError.ErrorCode.INTERNAL_ERROR, e.toString(), null /* data */)); } response.body = bodyData.data; response.base64Encoded = bodyData.base64Encoded; return response; }
peerManager.getResponseBodyFileManager().openResponseBodyFile( requestId, base64Encode);
@Override protected void onFirstPeerRegistered() { AsyncPrettyPrinterExecutorHolder.ensureInitialized(); if (mAsyncPrettyPrinterRegistry == null && mPrettyPrinterInitializer != null) { mAsyncPrettyPrinterRegistry = new AsyncPrettyPrinterRegistry(); mPrettyPrinterInitializer.populatePrettyPrinters(mAsyncPrettyPrinterRegistry); } mResponseBodyFileManager.cleanupFiles(); }