Package io.fabric8.mockwebserver.http
Class Buffer
- java.lang.Object
-
- io.fabric8.mockwebserver.http.Buffer
-
public class Buffer extends Object
Compatibility layer for OkHttp.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidflush()No operation is performed, added for compatibility.byte[]getBytes()Returns the bytes in this source as a byte array.InputStreaminputStream()Returns an input stream that reads from bytes cloned from this buffer.byte[]readByteArray()Removes all bytes from this and returns them as a byte array.StringreadString(Charset charset)Removes all bytes from this, decodes them ascharset, and returns the string.StringreadUtf8()Removes all bytes from this, decodes them as UTF-8, and returns the string.longsize()Returns the number of bytes in this buffer.Bufferwrite(byte[] bytes)LikeOutputStream.write(byte[]), this writes a complete byte array to this sink.Bufferwrite(byte[] bytes, int off, int len)LikeOutputStream.write(byte[], int, int), this writesbyteCountbytes ofsource, starting atoffset.BufferwriteString(String string, Charset charset)Encodesstringincharsetand writes it to this buffer.BufferwriteUtf8(String string)Encodesstringin UTF-8 and writes it to this buffer.
-
-
-
Method Detail
-
getBytes
public final byte[] getBytes()
Returns the bytes in this source as a byte array.- Returns:
- the bytes in this source as a byte array.
-
size
public final long size()
Returns the number of bytes in this buffer.- Returns:
- the number of bytes in this buffer.
-
readByteArray
public final byte[] readByteArray()
Removes all bytes from this and returns them as a byte array.- Returns:
- the byte array containing the bytes in this buffer.
-
readString
public final String readString(Charset charset)
Removes all bytes from this, decodes them ascharset, and returns the string.- Parameters:
charset- the charset to use to decode the bytes.- Returns:
- the string containing the bytes in this buffer.
-
readUtf8
public final String readUtf8()
Removes all bytes from this, decodes them as UTF-8, and returns the string. Returns the empty string if this source is empty.Buffer buffer = new Buffer() .writeUtf8("Uh uh uh!") .writeByte(' ') .writeUtf8("You didn't say the magic word!"); assertEquals("Uh uh uh! You didn't say the magic word!", buffer.readUtf8()); assertEquals(0, buffer.size()); assertEquals("", buffer.readUtf8()); assertEquals(0, buffer.size());
-
write
public final Buffer write(byte[] bytes)
LikeOutputStream.write(byte[]), this writes a complete byte array to this sink.- Parameters:
bytes- the bytes to write.
-
write
public Buffer write(byte[] bytes, int off, int len)
LikeOutputStream.write(byte[], int, int), this writesbyteCountbytes ofsource, starting atoffset.- Parameters:
bytes- the bytes to write.off- the index of the first byte to write.len- the number of bytes to write.
-
writeString
public final Buffer writeString(String string, Charset charset)
Encodesstringincharsetand writes it to this buffer.- Parameters:
string- the string to write.charset- the charset to use to encode the string.
-
writeUtf8
public final Buffer writeUtf8(String string)
Encodesstringin UTF-8 and writes it to this buffer.Buffer buffer = new Buffer(); buffer.writeUtf8("Uh uh uh!"); buffer.writeByte(' '); buffer.writeUtf8("You didn't say the magic word!"); assertEquals("Uh uh uh! You didn't say the magic word!", buffer.readUtf8());- Parameters:
string- the string to write.
-
inputStream
public final InputStream inputStream()
Returns an input stream that reads from bytes cloned from this buffer.- Returns:
- an input stream that reads from bytes cloned from this buffer.
-
flush
public final void flush()
No operation is performed, added for compatibility.
-
-