@Override public int read(CharBuffer cb) throws IOException { return a.read(cb); } };
private static StringBuilder toStringBuilder(final Readable input) throws IOException { final StringBuilder text = new StringBuilder(); final CharBuffer buffer = CharBuffer.allocate(BUFFER_SIZE); while (true) { final int n = input.read(buffer); if (n == -1) { break; } buffer.flip(); text.append(buffer, 0, n); } return text; }
private static StringBuilder toStringBuilder(final Readable input) throws IOException { final StringBuilder text = new StringBuilder(); final CharBuffer buffer = CharBuffer.allocate(BUFFER_SIZE); while (true) { final int n = input.read(buffer); if (n == -1) { break; } buffer.flip(); text.append(buffer, 0, n); } return text; }
/** * Reads and discards data from the given {@code Readable} until the end of the stream is reached. * Returns the total number of chars read. Does not close the stream. * * @since 20.0 */ @CanIgnoreReturnValue public static long exhaust(Readable readable) throws IOException { long total = 0; long read; CharBuffer buf = createBuffer(); while ((read = readable.read(buf)) != -1) { total += read; buf.clear(); } return total; }
/** * Reads a line of text. A line is considered to be terminated by any one of a line feed ({@code * '\n'}), a carriage return ({@code '\r'}), or a carriage return followed immediately by a * linefeed ({@code "\r\n"}). * * @return a {@code String} containing the contents of the line, not including any * line-termination characters, or {@code null} if the end of the stream has been reached. * @throws IOException if an I/O error occurs */ @CanIgnoreReturnValue // to skip a line public String readLine() throws IOException { while (lines.peek() == null) { cbuf.clear(); // The default implementation of Reader#read(CharBuffer) allocates a // temporary char[], so we call Reader#read(char[], int, int) instead. int read = (reader != null) ? reader.read(buf, 0, buf.length) : readable.read(cbuf); if (read == -1) { lineBuf.finish(); break; } lineBuf.add(buf, 0, read); } return lines.poll(); } }
/** * Reads and discards data from the given {@code Readable} until the end of the stream is reached. * Returns the total number of chars read. Does not close the stream. * * @since 20.0 */ @CanIgnoreReturnValue public static long exhaust(Readable readable) throws IOException { long total = 0; long read; CharBuffer buf = createBuffer(); while ((read = readable.read(buf)) != -1) { total += read; buf.clear(); } return total; }
/** * Creates a {@link Reader} from the given {@link Readable} object using the * specified buffer size. * * @param readable a readable * @param bufferCapacity a new buffer capacity, in chars * @return a reader * @throws IOException when {@link Readable#read(CharBuffer)} encounters an error */ static Reader createReader(Readable readable, int bufferCapacity) throws IOException { if (readable instanceof Reader) { return (Reader) readable; } CharBuffer cb = CharBuffer.allocate(bufferCapacity); StringBuilder sb = new StringBuilder(); while (readable.read(cb) != -1) { cb.flip(); sb.append(cb.toString()); } return new StringReader(sb.toString()); }
/** * Reads and discards data from the given {@code Readable} until the end of the stream is reached. * Returns the total number of chars read. Does not close the stream. * * @since 20.0 */ @CanIgnoreReturnValue public static long exhaust(Readable readable) throws IOException { long total = 0; long read; CharBuffer buf = createBuffer(); while ((read = readable.read(buf)) != -1) { total += read; buf.clear(); } return total; }
long total = 0; CharBuffer buf = createBuffer(); while (from.read(buf) != -1) { buf.flip(); to.append(buf);
/** * Reads a line of text. A line is considered to be terminated by any one of a line feed ({@code * '\n'}), a carriage return ({@code '\r'}), or a carriage return followed immediately by a * linefeed ({@code "\r\n"}). * * @return a {@code String} containing the contents of the line, not including any * line-termination characters, or {@code null} if the end of the stream has been reached. * @throws IOException if an I/O error occurs */ @CanIgnoreReturnValue // to skip a line public String readLine() throws IOException { while (lines.peek() == null) { cbuf.clear(); // The default implementation of Reader#read(CharBuffer) allocates a // temporary char[], so we call Reader#read(char[], int, int) instead. int read = (reader != null) ? reader.read(buf, 0, buf.length) : readable.read(cbuf); if (read == -1) { lineBuf.finish(); break; } lineBuf.add(buf, 0, read); } return lines.poll(); } }
buffer.limit(buffer.capacity()); buffer.position(oldBufferLength); while ((readCount = input.read(buffer)) == 0) {
/** * Reads a line of text. A line is considered to be terminated by any one of a line feed ({@code * '\n'}), a carriage return ({@code '\r'}), or a carriage return followed immediately by a * linefeed ({@code "\r\n"}). * * @return a {@code String} containing the contents of the line, not including any * line-termination characters, or {@code null} if the end of the stream has been reached. * @throws IOException if an I/O error occurs */ @CanIgnoreReturnValue // to skip a line public String readLine() throws IOException { while (lines.peek() == null) { cbuf.clear(); // The default implementation of Reader#read(CharBuffer) allocates a // temporary char[], so we call Reader#read(char[], int, int) instead. int read = (reader != null) ? reader.read(buf, 0, buf.length) : readable.read(cbuf); if (read == -1) { lineBuf.finish(); break; } lineBuf.add(buf, 0, read); } return lines.poll(); } }
/** * Reads and discards data from the given {@code Readable} until the end of the stream is reached. * Returns the total number of chars read. Does not close the stream. * * @since 20.0 */ @CanIgnoreReturnValue public static long exhaust(Readable readable) throws IOException { long total = 0; long read; CharBuffer buf = createBuffer(); while ((read = readable.read(buf)) != -1) { total += read; buf.clear(); } return total; }
ensureCapacity(size + 1); final CharBuffer buf = CharBuffer.wrap(buffer, size, buffer.length - size); final int read = readable.read(buf); if (read == -1) { break;
/** * Reads a line of text. A line is considered to be terminated by any one of a line feed ({@code * '\n'}), a carriage return ({@code '\r'}), or a carriage return followed immediately by a * linefeed ({@code "\r\n"}). * * @return a {@code String} containing the contents of the line, not including any * line-termination characters, or {@code null} if the end of the stream has been reached. * @throws IOException if an I/O error occurs */ @CanIgnoreReturnValue // to skip a line public String readLine() throws IOException { while (lines.peek() == null) { cbuf.clear(); // The default implementation of Reader#read(CharBuffer) allocates a // temporary char[], so we call Reader#read(char[], int, int) instead. int read = (reader != null) ? reader.read(buf, 0, buf.length) : readable.read(cbuf); if (read == -1) { lineBuf.finish(); break; } lineBuf.add(buf, 0, read); } return lines.poll(); } }
long total = 0; CharBuffer buf = createBuffer(); while (from.read(buf) != -1) { buf.flip(); to.append(buf);
long total = 0; CharBuffer buf = createBuffer(); while (from.read(buf) != -1) { buf.flip(); to.append(buf);
long total = 0; CharBuffer buf = createBuffer(); while (from.read(buf) != -1) { buf.flip(); to.append(buf);
public void testGet_io() throws IOException { assertEquals(-1, ArbitraryInstances.get(InputStream.class).read()); assertEquals(-1, ArbitraryInstances.get(ByteArrayInputStream.class).read()); assertEquals(-1, ArbitraryInstances.get(Readable.class).read(CharBuffer.allocate(1))); assertEquals(-1, ArbitraryInstances.get(Reader.class).read()); assertEquals(-1, ArbitraryInstances.get(StringReader.class).read()); assertEquals(0, ArbitraryInstances.get(Buffer.class).capacity()); assertEquals(0, ArbitraryInstances.get(CharBuffer.class).capacity()); assertEquals(0, ArbitraryInstances.get(ByteBuffer.class).capacity()); assertEquals(0, ArbitraryInstances.get(ShortBuffer.class).capacity()); assertEquals(0, ArbitraryInstances.get(IntBuffer.class).capacity()); assertEquals(0, ArbitraryInstances.get(LongBuffer.class).capacity()); assertEquals(0, ArbitraryInstances.get(FloatBuffer.class).capacity()); assertEquals(0, ArbitraryInstances.get(DoubleBuffer.class).capacity()); ArbitraryInstances.get(PrintStream.class).println("test"); ArbitraryInstances.get(PrintWriter.class).println("test"); assertNotNull(ArbitraryInstances.get(File.class)); assertFreshInstanceReturned( ByteArrayOutputStream.class, OutputStream.class, Writer.class, StringWriter.class, PrintStream.class, PrintWriter.class); assertEquals(ByteSource.empty(), ArbitraryInstances.get(ByteSource.class)); assertEquals(CharSource.empty(), ArbitraryInstances.get(CharSource.class)); assertNotNull(ArbitraryInstances.get(ByteSink.class)); assertNotNull(ArbitraryInstances.get(CharSink.class)); }