Java ByteBuffer to OutputStream newOutputStream(final ByteBuffer buf)

Here you can find the source of newOutputStream(final ByteBuffer buf)

Description

new Output Stream

License

Apache License

Declaration

public static OutputStream newOutputStream(final ByteBuffer buf) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.IOException;

import java.io.OutputStream;
import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;

public class Main {
    public static OutputStream newOutputStream(final ByteBuffer buf) {
        return new OutputStream() {
            public synchronized void write(int b) throws IOException {
                try {
                    buf.put((byte) b);
                } catch (BufferOverflowException e) {
                    throw new IOException(e);
                }//w ww. j  av a2s .c  om
            }

            public synchronized void write(byte[] bytes, int off, int len) throws IOException {
                try {
                    buf.put(bytes, off, len);
                } catch (BufferOverflowException e) {
                    throw new IOException(e);
                }
            }
        };
    }
}

Related

  1. newOutputStream(final ByteBuffer dst)