Java Utililty Methods ByteBuffer Put

List of utility methods to do ByteBuffer Put

Description

The list of methods to do ByteBuffer Put are organized into topic(s).

Method

InputStreaminputStream(ByteBuffer bytes)
input Stream
final ByteBuffer copy = bytes.duplicate();
return new InputStream() {
    public int read() throws IOException {
        if (!copy.hasRemaining())
            return -1;
        return copy.get() & 0xFF;
    @Override
...
ByteBufferinputStreamToByteBuffer(InputStream input)
Loads an input stream fully keeping the whole file in memory.
ByteArrayOutputStream bas = new ByteArrayOutputStream(1000000); 
byte[] buf = new byte[1024];
int n;
while ((n = input.read(buf)) != -1)
    bas.write(buf, 0, n);
input.close();
buf = bas.toByteArray();
ByteBuffer byteBuffer = ByteBuffer.wrap(buf);
...
ByteBufferinputStreamToByteBuffer(InputStream is)
input Stream To Byte Buffer
final int PAGE_SIZE = 4096;
final ArrayList<byte[]> pages = new ArrayList<byte[]>();
for (;;) {
    byte[] page = new byte[PAGE_SIZE];
    int pagePos = 0;
    int read;
    do {
        read = is.read(page, pagePos, PAGE_SIZE - pagePos);
...
InputStreammakeInputStream(final ByteBuffer buffer)
make Input Stream
return new InputStream() {
    @Override
    public synchronized int read() throws IOException {
        return buffer.hasRemaining() ? buffer.get() : -1;
    @Override
    public int read(byte[] b, int off, int len) throws IOException {
        final int rv = Math.min(len, buffer.remaining());
...
voidmoveBufferToStream(OutputStream out, ByteBuffer in, int length)
Copy the data to the output stream and update position in buffer.
copyBufferToStream(out, in, in.position(), length);
skip(in, length);
voidoutputBuffer(ByteBuffer buffer)
output Buffer
try {
    byte[] rawBytes = new byte[buffer.capacity()];
    buffer.get(rawBytes);
    File outputFile = new File(String.format("owf_%04d.bin", frame));
    OutputStream streamOut = new BufferedOutputStream(new FileOutputStream(outputFile));
    streamOut.write(rawBytes);
    streamOut.close();
} catch (Throwable th) {
...
voidput(ByteBuffer bb, byte[] bytes, int from)
The missing method on a ByteBuffer - allows copying of multiple bytes into the buffer.
int counter = 0;
for (byte b : bytes) {
    bb.put(from + counter, b);
    ++counter;
ByteBufferput(ByteBuffer bb, String s)
put
bb = expand(bb, s.length());
for (int i = 0; i < s.length(); i++) {
    bb.put(i, (byte) s.charAt(i));
return bb;
ByteBufferput(ByteBuffer bbuf, byte[] post)
put
ByteBuffer b = bbuf;
int postLen = post.length;
if (postLen == 0)
    return b;
if (postLen > bbuf.remaining()) {
    int esize = DEFAULT_ENHANCE_SIZE + postLen - bbuf.remaining();
    b = enhance(bbuf, esize);
b.put(post);
return b;
voidput(ByteBuffer buf, String s, String charsetName)
Puts a String 's bytes in the character set starting at the ByteBuffer 's current position.
try {
    buf.put(s.getBytes(charsetName));
} catch (UnsupportedEncodingException e) {
    throw new IllegalArgumentException(e);