@Override public void writeTo(BufferedSink sink) throws IOException { buffer.copyTo(sink.buffer(), 0, buffer.size()); } }
/** * Returns true if the body in question probably contains human readable text. Uses a small sample * of code points to detect unicode control characters commonly used in binary file signatures. */ static boolean isPlaintext(Buffer buffer) { try { Buffer prefix = new Buffer(); long byteCount = buffer.size() < 64 ? buffer.size() : 64; buffer.copyTo(prefix, 0, byteCount); for (int i = 0; i < 16; i++) { if (prefix.exhausted()) { break; } int codePoint = prefix.readUtf8CodePoint(); if (Character.isISOControl(codePoint) && !Character.isWhitespace(codePoint)) { return false; } } return true; } catch (EOFException e) { return false; // Truncated UTF-8 sequence. } }
/** * Returns true if the body in question probably contains human readable text. Uses a small sample * of code points to detect unicode control characters commonly used in binary file signatures. */ private boolean isPlaintext(Buffer buffer) { try { Buffer prefix = new Buffer(); long byteCount = buffer.size() < 64 ? buffer.size() : 64; buffer.copyTo(prefix, 0, byteCount); for (int i = 0; i < 16; i++) { if (prefix.exhausted()) { break; } int codePoint = prefix.readUtf8CodePoint(); if (Character.isISOControl(codePoint) && !Character.isWhitespace(codePoint)) { return false; } } return true; } catch (EOFException e) { return false; // Truncated UTF-8 sequence. } }
/** * Returns true if the body in question probably contains human readable text. Uses a small sample * of code points to detect unicode control characters commonly used in binary file signatures. */ static boolean isPlaintext(Buffer buffer) { try { Buffer prefix = new Buffer(); long byteCount = buffer.size() < 64 ? buffer.size() : 64; buffer.copyTo(prefix, 0, byteCount); for (int i = 0; i < 16; i++) { if (prefix.exhausted()) { break; } int codePoint = prefix.readUtf8CodePoint(); if (Character.isISOControl(codePoint) && !Character.isWhitespace(codePoint)) { return false; } } return true; } catch (EOFException e) { return false; // Truncated UTF-8 sequence. } }
public static ByteString encodeQuery(String host, int type) { Buffer buf = new Buffer(); buf.writeShort(0); // query id buf.writeShort(256); // flags with recursion buf.writeShort(1); // question count buf.writeShort(0); // answerCount buf.writeShort(0); // authorityResourceCount buf.writeShort(0); // additional Buffer nameBuf = new Buffer(); final String[] labels = host.split("\\."); for (String label : labels) { long utf8ByteCount = Utf8.size(label); if (utf8ByteCount != label.length()) { throw new IllegalArgumentException("non-ascii hostname: " + host); } nameBuf.writeByte((byte) utf8ByteCount); nameBuf.writeUtf8(label); } nameBuf.writeByte(0); // end nameBuf.copyTo(buf, 0, nameBuf.size()); buf.writeShort(type); buf.writeShort(1); // CLASS_IN return buf.readByteString(); }
long posInBuffer = offset - mark; long result = Math.min(byteCount, markBuffer.size() - posInBuffer); markBuffer.copyTo(sink, posInBuffer, result); offset += result; return result; long result = super.read(markBuffer, Math.min(byteCount, byteCountBeforeLimit)); if (result == -1L) return -1L; markBuffer.copyTo(sink, markBuffer.size() - result, result); offset += result; return result;
void copyFrom(Buffer buffer, long offset, long bytesCount) { if (failed) return; try { BufferedSink outSink = (BufferedSink) delegate(); buffer.copyTo(outSink.buffer(), offset, bytesCount); outSink.emitCompleteSegments(); } catch (Exception e) { failed = true; onException(e); } }
@Override public long read(Buffer sink, long byteCount) throws IOException { long bytesRead; try { bytesRead = source.read(sink, byteCount); } catch (IOException e) { if (!cacheRequestClosed) { cacheRequestClosed = true; cacheRequest.abort(); // Failed to write a complete cache response. } throw e; } if (bytesRead == -1) { if (!cacheRequestClosed) { cacheRequestClosed = true; cacheBody.close(); // The cache response is complete! } return -1; } sink.copyTo(cacheBody.buffer(), sink.size() - bytesRead, bytesRead); cacheBody.emitCompleteSegments(); return bytesRead; }
@Test public void copyToStream() throws Exception { Buffer buffer = new Buffer().writeUtf8("hello, world!"); ByteArrayOutputStream out = new ByteArrayOutputStream(); buffer.copyTo(out); String outString = new String(out.toByteArray(), UTF_8); assertEquals("hello, world!", outString); assertEquals("hello, world!", buffer.readUtf8()); }
@Test // SPR-16246 public void shouldSendLargeTextFile() throws IOException { prepareResponse(response -> {}); Resource resource = new ClassPathResource("largeTextFile.txt", getClass()); byte[] expected = Files.readAllBytes(resource.getFile().toPath()); Flux<DataBuffer> body = DataBufferUtils.read(resource, new DefaultDataBufferFactory(), 4096); this.webClient.post() .uri("/") .body(body, DataBuffer.class) .retrieve() .bodyToMono(Void.class) .block(Duration.ofSeconds(5)); expectRequest(request -> { ByteArrayOutputStream actual = new ByteArrayOutputStream(); try { request.getBody().copyTo(actual); } catch (IOException ex) { throw new IllegalStateException(ex); } assertEquals(expected.length, actual.size()); assertEquals(hash(expected), hash(actual.toByteArray())); }); }
@Test public void copyTo() { Buffer source = new Buffer(); source.writeUtf8("party"); Buffer target = new Buffer(); source.copyTo(target, 1, 3); assertEquals("art", target.readUtf8()); assertEquals("party", source.readUtf8()); }
@Test public void copyToEmptySource() { Buffer source = new Buffer(); Buffer target = new Buffer().writeUtf8("aaa"); source.copyTo(target, 0L, 0L); assertEquals("", source.readUtf8()); assertEquals("aaa", target.readUtf8()); }
@Test public void copyToEmptyTarget() { Buffer source = new Buffer().writeUtf8("aaa"); Buffer target = new Buffer(); source.copyTo(target, 0L, 3L); assertEquals("aaa", source.readUtf8()); assertEquals("aaa", target.readUtf8()); }
@Test public void copyToOffSegmentBoundary() { String as = repeat('a', SEGMENT_SIZE - 1); String bs = repeat('b', SEGMENT_SIZE + 2); String cs = repeat('c', SEGMENT_SIZE - 4); String ds = repeat('d', SEGMENT_SIZE + 8); Buffer source = new Buffer(); source.writeUtf8(as); source.writeUtf8(bs); source.writeUtf8(cs); Buffer target = new Buffer(); target.writeUtf8(ds); source.copyTo(target, as.length(), bs.length() + cs.length()); assertEquals(ds + bs + cs, target.readUtf8()); }
@Test public void copyToOnSegmentBoundary() { String as = repeat('a', SEGMENT_SIZE); String bs = repeat('b', SEGMENT_SIZE); String cs = repeat('c', SEGMENT_SIZE); String ds = repeat('d', SEGMENT_SIZE); Buffer source = new Buffer(); source.writeUtf8(as); source.writeUtf8(bs); source.writeUtf8(cs); Buffer target = new Buffer(); target.writeUtf8(ds); source.copyTo(target, as.length(), bs.length() + cs.length()); assertEquals(ds + bs + cs, target.readUtf8()); }
@Override public long read(Buffer sink, long byteCount) throws IOException { long bytesRead; try { bytesRead = source.read(sink, byteCount); } catch (IOException e) { if (!cacheRequestClosed) { cacheRequestClosed = true; cacheRequest.abort(); // Failed to write a complete cache response. } throw e; } if (bytesRead == -1) { if (!cacheRequestClosed) { cacheRequestClosed = true; cacheBody.close(); // The cache response is complete! } return -1; } sink.copyTo(cacheBody.buffer(), sink.size() - bytesRead, bytesRead); cacheBody.emitCompleteSegments(); return bytesRead; }
@Test public void copyToSpanningSegments() throws Exception { Buffer source = new Buffer(); source.writeUtf8(repeat('a', SEGMENT_SIZE * 2)); source.writeUtf8(repeat('b', SEGMENT_SIZE * 2)); ByteArrayOutputStream out = new ByteArrayOutputStream(); source.copyTo(out, 10, SEGMENT_SIZE * 3); assertEquals(repeat('a', SEGMENT_SIZE * 2 - 10) + repeat('b', SEGMENT_SIZE + 10), out.toString()); assertEquals(repeat('a', SEGMENT_SIZE * 2) + repeat('b', SEGMENT_SIZE * 2), source.readUtf8(SEGMENT_SIZE * 4)); }
@Test public void copyToSourceAndTargetCanBeTheSame() { String as = repeat('a', SEGMENT_SIZE); String bs = repeat('b', SEGMENT_SIZE); Buffer source = new Buffer(); source.writeUtf8(as); source.writeUtf8(bs); source.copyTo(source, 0, source.size()); assertEquals(as + bs + as + bs, source.readUtf8()); }
@Test public void shrink() throws Exception { Buffer buffer = bufferFactory.newBuffer(); assumeTrue(buffer.size() > 3); long originalSize = buffer.size(); Buffer expected = new Buffer(); deepCopy(buffer).copyTo(expected, 0, originalSize - 3); try (UnsafeCursor cursor = buffer.readAndWriteUnsafe()) { assertEquals(originalSize, cursor.resizeBuffer(originalSize - 3)); } assertEquals(expected, buffer); }