Returns an output stream for a ByteBuffer. - Java java.nio

Java examples for java.nio:ByteBuffer Stream

Description

Returns an output stream for a ByteBuffer.

Demo Code


//package com.java2s;
import java.io.*;

import java.nio.ByteBuffer;

public class Main {
    /**//from   w ww .  j  a  v a 2 s  .  c o  m
     * Returns an output stream for a ByteBuffer.
     * The write() methods use the relative ByteBuffer put() methods.
     */
    public static OutputStream newOutputStream(final ByteBuffer buf) {
        return new OutputStream() {

            public synchronized void write(int b) throws IOException {
                buf.put((byte) b);
            }

            public synchronized void write(byte[] bytes, int off, int len)
                    throws IOException {
                buf.put(bytes, off, len);
            }
        };
    }
}

Related Tutorials