Package io.grpc.internal
Class ReadableBuffers
- java.lang.Object
-
- io.grpc.internal.ReadableBuffers
-
public final class ReadableBuffers extends java.lang.Object
Utility methods for creatingReadableBuffer
instances.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
ReadableBuffers.BufferInputStream
AnInputStream
that is backed by aReadableBuffer
.private static class
ReadableBuffers.ByteArrayWrapper
AReadableBuffer
that is backed by a byte array.private static class
ReadableBuffers.ByteReadableBufferWrapper
AReadableBuffer
that is backed by aByteBuffer
.
-
Field Summary
Fields Modifier and Type Field Description private static ReadableBuffer
EMPTY_BUFFER
-
Constructor Summary
Constructors Modifier Constructor Description private
ReadableBuffers()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static ReadableBuffer
empty()
Returns an emptyReadableBuffer
instance.static ReadableBuffer
ignoreClose(ReadableBuffer buffer)
Decorates the givenReadableBuffer
to ignore calls toReadableBuffer.close()
.static java.io.InputStream
openStream(ReadableBuffer buffer, boolean owner)
Creates a newInputStream
backed by the given buffer.static byte[]
readArray(ReadableBuffer buffer)
Reads an entireReadableBuffer
to a new array.static java.lang.String
readAsString(ReadableBuffer buffer, java.nio.charset.Charset charset)
Reads the entireReadableBuffer
to a newString
with the given charset.static java.lang.String
readAsStringUtf8(ReadableBuffer buffer)
Reads the entireReadableBuffer
to a newString
using UTF-8 decoding.static ReadableBuffer
wrap(byte[] bytes)
Shortcut forwrap(bytes, 0, bytes.length
.static ReadableBuffer
wrap(byte[] bytes, int offset, int length)
Creates a newReadableBuffer
that is backed by the given byte array.static ReadableBuffer
wrap(java.nio.ByteBuffer bytes)
Creates a newReadableBuffer
that is backed by the givenByteBuffer
.
-
-
-
Field Detail
-
EMPTY_BUFFER
private static final ReadableBuffer EMPTY_BUFFER
-
-
Method Detail
-
empty
public static ReadableBuffer empty()
Returns an emptyReadableBuffer
instance.
-
wrap
public static ReadableBuffer wrap(byte[] bytes)
Shortcut forwrap(bytes, 0, bytes.length
.
-
wrap
public static ReadableBuffer wrap(byte[] bytes, int offset, int length)
Creates a newReadableBuffer
that is backed by the given byte array.- Parameters:
bytes
- the byte array being wrapped.offset
- the starting offset for the buffer within the byte array.length
- the length of the buffer from theoffset
index.
-
wrap
public static ReadableBuffer wrap(java.nio.ByteBuffer bytes)
Creates a newReadableBuffer
that is backed by the givenByteBuffer
. Calls to read from the buffer will increment the position of theByteBuffer
.
-
readArray
public static byte[] readArray(ReadableBuffer buffer)
Reads an entireReadableBuffer
to a new array. After calling this method, the buffer will contain no readable bytes.
-
readAsString
public static java.lang.String readAsString(ReadableBuffer buffer, java.nio.charset.Charset charset)
Reads the entireReadableBuffer
to a newString
with the given charset.
-
readAsStringUtf8
public static java.lang.String readAsStringUtf8(ReadableBuffer buffer)
Reads the entireReadableBuffer
to a newString
using UTF-8 decoding.
-
openStream
public static java.io.InputStream openStream(ReadableBuffer buffer, boolean owner)
Creates a newInputStream
backed by the given buffer. Any read taken on the stream will automatically increment the read position of this buffer. Closing the stream, however, does not affect the original buffer.- Parameters:
buffer
- the buffer backing the newInputStream
.owner
- iftrue
, the returned stream will close the buffer when closed.
-
ignoreClose
public static ReadableBuffer ignoreClose(ReadableBuffer buffer)
Decorates the givenReadableBuffer
to ignore calls toReadableBuffer.close()
.- Parameters:
buffer
- the buffer to be decorated.- Returns:
- a wrapper around
buffer
that ignores calls toReadableBuffer.close()
.
-
-