/** * Returns the response as a byte array. * * <p>This method loads entire response body into memory. If the response body is very large this * may trigger an {@link OutOfMemoryError}. Prefer to stream the response body if this is a * possibility for your response. */ public final byte[] bytes() throws IOException { long contentLength = contentLength(); if (contentLength > Integer.MAX_VALUE) { throw new IOException("Cannot buffer entire body for content length: " + contentLength); } byte[] bytes; try (BufferedSource source = source()) { bytes = source.readByteArray(); } if (contentLength != -1 && contentLength != bytes.length) { throw new IOException("Content-Length (" + contentLength + ") and stream length (" + bytes.length + ") disagree"); } return bytes; }
@RequiresApi(28) @SuppressLint("Override") private static Bitmap decodeStreamP(Request request, BufferedSource bufferedSource) throws IOException { ImageDecoder.Source imageSource = ImageDecoder.createSource(ByteBuffer.wrap(bufferedSource.readByteArray())); return decodeImageSource(imageSource, request); }
static EncryptionResult load(File f) throws Exception { BufferedSource source=Okio.buffer(Okio.source(f)); byte[] iv=source.readByteArray(BLOCK_SIZE); byte[] encrypted=source.readByteArray(); source.close(); return new EncryptionResult(iv, encrypted); }
private void load(KeyStore ks, ObservableEmitter<char[]> emitter) throws Exception { BufferedSource source=Okio.buffer(Okio.source(encryptedFile)); byte[] iv=source.readByteArray(BLOCK_SIZE); byte[] encrypted=source.readByteArray(); source.close(); SecretKey secretKey=(SecretKey)ks.getKey(keyName, null); Cipher cipher=Cipher.getInstance("AES/CBC/PKCS7Padding"); cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv)); byte[] decrypted=cipher.doFinal(encrypted); char[] passphrase=toChars(decrypted); emitter.onNext(passphrase); } }
/** * Returns the response as a byte array. * * <p>This method loads entire response body into memory. If the response body is very large this * may trigger an {@link OutOfMemoryError}. Prefer to stream the response body if this is a * possibility for your response. */ public final byte[] bytes() throws IOException { long contentLength = contentLength(); if (contentLength > Integer.MAX_VALUE) { throw new IOException("Cannot buffer entire body for content length: " + contentLength); } byte[] bytes; try (BufferedSource source = source()) { bytes = source.readByteArray(); } if (contentLength != -1 && contentLength != bytes.length) { throw new IOException("Content-Length (" + contentLength + ") and stream length (" + bytes.length + ") disagree"); } return bytes; }
@Nullable byte[] load(String id) throws IOException { final File file = toFile(id); if (file.exists() && file.canRead()) { BufferedSource source = Okio.buffer(Okio.source(file)); try { return source.readByteArray(); } finally { closeQuitely(source); } } return null; }
/** Reads a potentially Huffman encoded byte string. */ ByteString readByteString() throws IOException { int firstByte = readByte(); boolean huffmanDecode = (firstByte & 0x80) == 0x80; // 1NNNNNNN int length = readInt(firstByte, PREFIX_7_BITS); if (huffmanDecode) { return ByteString.of(Huffman.get().decode(source.readByteArray(length))); } else { return source.readByteString(length); } } }
byte[] data = new byte[0]; try { data = Okio.buffer(response.body().source()).readByteArray(); } catch (IOException e) { e.printStackTrace();
byte[] bytes = bufferedSource.readByteArray(); if (calculateSize) { BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
@Test public void testPdfAndroidJavascriptBridgeGetChunk() throws IOException { final String path = this.getClass().getClassLoader().getResource("file.txt").getPath(); final File file = new File(path); final long size = file.length(); final String expected = Base64.encodeToString(Okio.buffer(Okio.source(file)).readByteArray(), Base64.DEFAULT); final WebFragment.PdfAndroidJavascriptBridge bridge = new WebFragment.PdfAndroidJavascriptBridge(path, null); assertEquals(expected, bridge.getChunk(0, size)); }
/** Reads a potentially Huffman encoded byte string. */ ByteString readByteString() throws IOException { int firstByte = readByte(); boolean huffmanDecode = (firstByte & 0x80) == 0x80; // 1NNNNNNN int length = readInt(firstByte, PREFIX_7_BITS); if (huffmanDecode) { return ByteString.of(Huffman.get().decode(source.readByteArray(length))); } else { return source.readByteString(length); } } }
@Test public void readByteArray() throws IOException { String string = "abcd" + repeat('e', SEGMENT_SIZE); sink.writeUtf8(string); sink.emit(); assertByteArraysEquals(string.getBytes(UTF_8), source.readByteArray()); }
@Test public void readByteArrayPartial() throws IOException { sink.writeUtf8("abcd"); sink.emit(); assertEquals("[97, 98, 99]", Arrays.toString(source.readByteArray(3))); assertEquals("d", source.readUtf8(1)); }
InetAddress inetAddress; if (addressType == ADDRESS_TYPE_IPV4) { inetAddress = InetAddress.getByAddress(fromSource.readByteArray(4L)); } else if (addressType == ADDRESS_TYPE_DOMAIN_NAME){ int domainNameLength = fromSource.readByte() & 0xff;
@Override public void accept(BufferedSource source) { try { while (!source.exhausted()) { // See https://docs.docker.com/engine/api/v1.37/#operation/ContainerAttach if(!source.request(HEADER_SIZE)) { return; } StreamType streamType = streamType(source.readByte()); source.skip(3); int payloadSize = source.readInt(); if (streamType != StreamType.RAW) { if (!source.request(payloadSize)) { return; } byte[] payload = source.readByteArray(payloadSize); resultCallback.onNext(new Frame(streamType, payload)); } else { resultCallback.onNext(new Frame(streamType, source.readByteArray())); } } } catch (Exception e) { resultCallback.onError(e); } }
@Test public void readByteArrayTooShortThrows() throws IOException { sink.writeUtf8("abc"); sink.emit(); try { source.readByteArray(4); fail(); } catch (EOFException expected) { } assertEquals("abc", source.readUtf8()); // The read shouldn't consume any data. }
protected SmashResponse<byte[]> parseResponse(SmashNetworkData data) { try { return SmashResponse.success(Okio.buffer(data.source).readByteArray()); } catch (IOException ioe) { return SmashResponse.failed(new SmashError(ioe)); } }
@WorkerThread public void flashBinaryFile(InputStream stream) throws ParticleCloudException, IOException { final byte[] bytes = Okio.buffer(Okio.source(stream)).readByteArray(); performFlashingChange(() -> mainApi.flashFile(deviceState.deviceId, new TypedFakeFile(bytes))); }
@Test public void proxyHandlesExceptions() throws IOException { ResponseBody body = ResponseBody.create(MediaType.parse("application/json"), -1, mockSource); when(chain.proceed(request)).thenReturn(response.newBuilder().body(body).build()); IOException exception = new IOException(); when(mockSource.readByteArray()).thenThrow(exception); Response erroneousResponse = interceptor.intercept(chain); assertThatThrownBy(() -> erroneousResponse.body().source().readByteArray()).isEqualTo(exception); }
/** Reads a potentially Huffman encoded byte string. */ ByteString readByteString() throws IOException { int firstByte = readByte(); boolean huffmanDecode = (firstByte & 0x80) == 0x80; // 1NNNNNNN int length = readInt(firstByte, PREFIX_7_BITS); if (huffmanDecode) { return ByteString.of(Huffman.get().decode(source.readByteArray(length))); } else { return source.readByteString(length); } } }