Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.
public final class ByteSink
- Object
- ByteSink
A growable byte buffer that callers reuse.
This exists because building output as a String and then encoding it is the single most expensive thing a server can do per request. Measured on this server: the response head alone, built with a StringBuilder, turned into a String and then into bytes, was a third of all allocation; the JSON body was most of the rest. Written as bytes into a buffer that lives as long as the connection, both cost nothing.
Deliberately not java.io.ByteArrayOutputStream: that one cannot be reset without discarding its buffer on some implementations, has synchronized methods, and hands out a COPY of its contents – three allocations where this has none.
Not thread safe. One per connection, used by the worker that owns it.
Constructors
public ByteSink(int initialCapacity) |
Methods
public byte[] bytes() | The backing array. |
public int length() | |
public void reset() | |
public void ensure(int extra) | |
public void put(int b) | |
public void put(byte[] source, int offset, int count) | |
public void put(ByteSink other) | |
public void putAscii(String ascii) | ASCII only, one byte per character. |
public void putUtf8(String value) | UTF-8, encoded in place. |
public void putCodePoint(int code) | One code point as UTF-8. |
public void putNumber(long value) | A number as ASCII digits, written in place. |
Inherited methods
Constructor details
ByteSink
public ByteSink(int initialCapacity)Method details
bytes
public byte[] bytes()length; not a copy.length
public int length()reset
public void reset()ensure
public void ensure(int extra)put
public void put(int b)put
public void put(byte[] source, int offset, int count)put
public void put(ByteSink other)putAscii
public void putAscii(String ascii)putUtf8.putUtf8
public void putUtf8(String value)UTF-8, encoded in place.
String.getBytes(“UTF-8”) would allocate the array this exists to avoid, and on the translated target it goes through the platform’s encoder for every call. Surrogate pairs are combined; an unpaired surrogate becomes U+FFFD, because emitting a lone surrogate produces bytes no decoder will accept.
putCodePoint
public void putCodePoint(int code)One code point as UTF-8.
Separate from putUtf8 because a caller walking a String character
by character has to combine a surrogate PAIR itself – handing the halves
over one at a time turns an emoji into two replacement characters, which is
what the JSON writer did until its output was compared against the String
form byte for byte.
putNumber
public void putNumber(long value)