本文整理了Java中okio.BufferedSource.readByteArray()
方法的一些代码示例,展示了BufferedSource.readByteArray()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。BufferedSource.readByteArray()
方法的具体详情如下:
包路径:okio.BufferedSource
类名称:BufferedSource
方法名:readByteArray
[英]Removes all bytes from this and returns them as a byte array.
[中]从中删除所有字节并将其作为字节数组返回。
代码示例来源:origin: square/okhttp
/**
* 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;
}
代码示例来源:origin: square/picasso
@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);
}
代码示例来源:origin: commonsguy/cw-omnibus
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);
}
代码示例来源:origin: commonsguy/cw-omnibus
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);
}
}
代码示例来源:origin: com.squareup.okhttp3/okhttp
/**
* 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;
}
代码示例来源:origin: yigit/android-priority-jobqueue
@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;
}
代码示例来源:origin: square/okhttp
/** 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);
}
}
}
代码示例来源:origin: amitshekhariitbhu/Fast-Android-Networking
byte[] data = new byte[0];
try {
data = Okio.buffer(response.body().source()).readByteArray();
} catch (IOException e) {
e.printStackTrace();
代码示例来源:origin: square/picasso
byte[] bytes = bufferedSource.readByteArray();
if (calculateSize) {
BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
代码示例来源:origin: hidroh/materialistic
@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));
}
代码示例来源:origin: com.squareup.okhttp3/okhttp
/** 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);
}
}
}
代码示例来源:origin: square/okio
@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());
}
代码示例来源:origin: square/okio
@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));
}
代码示例来源:origin: square/okio
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;
代码示例来源:origin: testcontainers/testcontainers-java
@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);
}
}
代码示例来源:origin: square/okio
@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.
}
代码示例来源:origin: appformation/smash
protected SmashResponse<byte[]> parseResponse(SmashNetworkData data)
{
try
{
return SmashResponse.success(Okio.buffer(data.source).readByteArray());
}
catch (IOException ioe)
{
return SmashResponse.failed(new SmashError(ioe));
}
}
代码示例来源:origin: particle-iot/spark-sdk-android
@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)));
}
代码示例来源:origin: palantir/conjure-java-runtime
@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);
}
代码示例来源:origin: huxq17/SwipeCardsView
/** 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);
}
}
}
内容来源于网络,如有侵权,请联系作者删除!