Example usage for java.nio.channels WritableByteChannel write

List of usage examples for java.nio.channels WritableByteChannel write

Introduction

In this page you can find the example usage for java.nio.channels WritableByteChannel write.

Prototype

public int write(ByteBuffer src) throws IOException;

Source Link

Document

Writes a sequence of bytes to this channel from the given buffer.

Usage

From source file:com.github.neoio.nio.util.NIOUtils.java

public static int writeToChannel(WritableByteChannel channel, ByteBuffer buffer) throws NetIOException {
    try {// w w w  .j ava 2 s.c om
        return channel.write(buffer);
    } catch (IOException e) {
        return -1;
    }
}

From source file:com.github.neoio.nio.util.NIOUtils.java

public static int writeToChannel(WritableByteChannel channel, byte array[], int offset, int length)
        throws NetIOException {
    try {//  ww  w  .  ja v a 2 s. com
        return channel.write(ByteBuffer.wrap(array, offset, length));
    } catch (IndexOutOfBoundsException e) {
        throw new NetIOException(e);
    } catch (IOException e) {
        throw new NetIOException(e);
    }
}

From source file:com.buaa.cfs.utils.IOUtils.java

/**
 * Write a ByteBuffer to a WritableByteChannel, handling short writes.
 *
 * @param bc  The WritableByteChannel to write to
 * @param buf The input buffer//from  www . j a  v  a  2  s  .c  o m
 *
 * @throws IOException On I/O error
 */
public static void writeFully(WritableByteChannel bc, ByteBuffer buf) throws IOException {
    do {
        bc.write(buf);
    } while (buf.remaining() > 0);
}

From source file:org.carlspring.strongbox.storage.metadata.nuget.TempNupkgFile.java

/**
 * Copies data from one channel to another
 *
 * @param src//from   w ww  .  ja  va 2s .c  om
 *            channel source
 * @param dest
 *            destination channel
 * @throws IOException
 *             input / output error
 */
private static void fastChannelCopy(final ReadableByteChannel src, final WritableByteChannel dest)
        throws IOException {
    final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);

    while (src.read(buffer) != -1) {
        buffer.flip();
        dest.write(buffer);
        buffer.compact();
    }

    buffer.flip();

    while (buffer.hasRemaining()) {
        dest.write(buffer);
    }
}

From source file:byps.BWire.java

/**
 * Writes a ByteBuffer to an OutputStream.
 * Closes the OutputStream./*from w w w .j av a  2s.  com*/
 * @param buf
 * @param os
 * @throws IOException
 */
public static void bufferToStream(ByteBuffer buf, boolean gzip, OutputStream os) throws IOException {
    if (gzip) {
        os = new GZIPOutputStream(os);
    }

    WritableByteChannel wch = null;
    try {
        wch = Channels.newChannel(os);
        wch.write(buf);
    } finally {
        if (wch != null)
            wch.close();
    }
}

From source file:com.mgmtp.perfload.perfalyzer.util.IoUtilities.java

public static void writeToChannel(final WritableByteChannel destChannel, final ByteBuffer buffer) {
    try {//from   w  w w .jav  a2 s.com
        // write to the destination channel
        destChannel.write(buffer);

        // If partial transfer, shift remainder down so it does not get lost
        // If buffer is empty, this is the same as calling clear()
        buffer.compact();

        // EOF will leave buffer in fill state
        buffer.flip();

        // make sure the buffer is fully drained
        while (buffer.hasRemaining()) {
            destChannel.write(buffer);
        }
    } catch (IOException ex) {
        throw new UncheckedIOException(ex);
    }
}

From source file:com.msr.dnsdemo.network.DownloadFile.java

public static void fastChannelCopy(final ReadableByteChannel src, final WritableByteChannel dest)
        throws IOException, NullPointerException {
    if (src != null && dest != null) {
        final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
        while (src.read(buffer) != -1) {
            // prepare the buffer to be drained
            buffer.flip();//  w  w w  .  j  a  v  a 2s .  c o  m
            // write to the channel, may block
            dest.write(buffer);
            // If partial transfer, shift remainder down
            // If buffer is empty, same as doing clear()
            buffer.compact();
        }
        // EOF will leave buffer in fill state
        buffer.flip();
        // make sure the buffer is fully drained.
        while (buffer.hasRemaining()) {
            dest.write(buffer);
        }
    }
}

From source file:net.darkmist.alib.io.BufferUtil.java

public static void writeAll(ByteBuffer buf, WritableByteChannel channel) throws IOException {
    while (buf.remaining() > 0)
        channel.write(buf);
}

From source file:StreamUtil.java

/**
 * Copies the content read from InputStream to OutputStream. Uses the NIO Channels to copy.
 * @param is The InputStream that is read.
 * @param os The OutputStream where the data is written.
 * @throws IOException/*from   w  w w .j  a  va2 s.c o  m*/
 */
public static void copy(final InputStream is, final OutputStream os) throws IOException {
    final ReadableByteChannel inChannel = Channels.newChannel(is);
    final WritableByteChannel outChannel = Channels.newChannel(os);

    try {
        final ByteBuffer buffer = ByteBuffer.allocate(65536);
        while (true) {
            int bytesRead = inChannel.read(buffer);
            if (bytesRead == -1)
                break;
            buffer.flip();
            while (buffer.hasRemaining())
                outChannel.write(buffer);
            buffer.clear();
        }
    } finally {
        try {
            inChannel.close();
        } catch (IOException ex) {
            LOG.log(Level.SEVERE, null, ex);
        }
        try {
            outChannel.close();
        } catch (IOException ex) {
            LOG.log(Level.SEVERE, null, ex);
        }
    }
}

From source file:com.discovery.darchrow.io.IOWriteUtil.java

/**
 * NIO API ?? ()./*w w  w . j a v  a  2s . c  om*/
 *
 * @param bufferLength
 *            the buffer length
 * @param inputStream
 *            the input stream
 * @param outputStream
 *            the output stream
 * @since 1.0.8
 * @since jdk1.4
 */
private static void writeUseNIO(int bufferLength, InputStream inputStream, OutputStream outputStream) {
    int i = 0;
    int sumSize = 0;
    int j = 0;

    ///2 
    //As creme de la creme with regard to performance, you could use NIO Channels and ByteBuffer. 

    ReadableByteChannel readableByteChannel = Channels.newChannel(inputStream);
    WritableByteChannel writableByteChannel = Channels.newChannel(outputStream);

    ByteBuffer byteBuffer = ByteBuffer.allocate(bufferLength);

    try {
        while (readableByteChannel.read(byteBuffer) != -1) {
            byteBuffer.flip();
            j = writableByteChannel.write(byteBuffer);
            sumSize += j;
            byteBuffer.clear();
            i++;
        }

        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Write data over,sumSize:[{}],bufferLength:[{}],loopCount:[{}]",
                    FileUtil.formatSize(sumSize), bufferLength, i);
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } finally {
        IOUtils.closeQuietly(outputStream);
        IOUtils.closeQuietly(writableByteChannel);
        IOUtils.closeQuietly(inputStream);
        IOUtils.closeQuietly(readableByteChannel);
    }
}