Example usage for java.nio ByteBuffer compact

List of usage examples for java.nio ByteBuffer compact

Introduction

In this page you can find the example usage for java.nio ByteBuffer compact.

Prototype

public abstract ByteBuffer compact();

Source Link

Document

Compacts this byte buffer.

Usage

From source file:org.apache.xmlgraphics.util.io.Base64Test.java

/**
 * Returns true if the contents of <tt>is1</tt> match the contents of
 * <tt>is2</tt>/*from w ww  .java2 s .c o  m*/
 *
 * @throws IOException
 */
public static boolean compareStreams(final InputStream i1, final InputStream i2, final boolean skipws)
        throws IOException {
    try (final ReadableByteChannel ch1 = Channels.newChannel(i1)) {
        try (final ReadableByteChannel ch2 = Channels.newChannel(i2)) {

            final ByteBuffer buf1 = ByteBuffer.allocateDirect(1024);
            final ByteBuffer buf2 = ByteBuffer.allocateDirect(1024);

            while (true) {

                final int n1 = ch1.read(buf1);
                final int n2 = ch2.read(buf2);

                if (n1 == -1 || n2 == -1) {
                    return n1 == n2;
                }

                buf1.flip();
                buf2.flip();

                for (int i = 0; i < Math.min(n1, n2); ++i) {
                    if (buf1.get() != buf2.get()) {
                        return false;
                    }
                }

                buf1.compact();
                buf2.compact();
            }
        }
    }
}

From source file:ChannelToWriter.java

/**
 * Read bytes from the specified channel, decode them using the specified
 * Charset, and write the resulting characters to the specified writer
 *//*w w  w  .ja v a  2  s. c om*/
public static void copy(ReadableByteChannel channel, Writer writer, Charset charset) throws IOException {
    // Get and configure the CharsetDecoder we'll use
    CharsetDecoder decoder = charset.newDecoder();
    decoder.onMalformedInput(CodingErrorAction.IGNORE);
    decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);

    // Get the buffers we'll use, and the backing array for the CharBuffer.
    ByteBuffer bytes = ByteBuffer.allocateDirect(2 * 1024);
    CharBuffer chars = CharBuffer.allocate(2 * 1024);
    char[] array = chars.array();

    while (channel.read(bytes) != -1) { // Read from channel until EOF
        bytes.flip(); // Switch to drain mode for decoding
        // Decode the byte buffer into the char buffer.
        // Pass false to indicate that we're not done.
        decoder.decode(bytes, chars, false);

        // Put the char buffer into drain mode, and write its contents
        // to the Writer, reading them from the backing array.
        chars.flip();
        writer.write(array, chars.position(), chars.remaining());

        // Discard all bytes we decoded, and put the byte buffer back into
        // fill mode. Since all characters were output, clear that buffer.
        bytes.compact(); // Discard decoded bytes
        chars.clear(); // Clear the character buffer
    }

    // At this point there may still be some bytes in the buffer to decode
    // So put the buffer into drain mode call decode() a final time, and
    // finish with a flush().
    bytes.flip();
    decoder.decode(bytes, chars, true); // True means final call
    decoder.flush(chars); // Flush any buffered chars
    // Write these final chars (if any) to the writer.
    chars.flip();
    writer.write(array, chars.position(), chars.remaining());
    writer.flush();
}

From source file:de.metalcon.imageServer.protocol.CreateRequestTest.java

/**
 * compare two input streams/*from w  ww. jav a 2  s .  c o m*/
 * 
 * @param stream1
 *            first input stream
 * @param stream2
 *            second input stream
 * @return true - if the two streams does contain the same content<br>
 *         false - otherwise
 * @throws IOException
 *             if IO errors occurred
 */
private static boolean compareInputStreams(final InputStream stream1, final InputStream stream2)
        throws IOException {
    final ReadableByteChannel channel1 = Channels.newChannel(stream1);
    final ReadableByteChannel channel2 = Channels.newChannel(stream2);
    final ByteBuffer buffer1 = ByteBuffer.allocateDirect(4096);
    final ByteBuffer buffer2 = ByteBuffer.allocateDirect(4096);

    try {
        while (true) {

            int n1 = channel1.read(buffer1);
            int n2 = channel2.read(buffer2);

            if ((n1 == -1) || (n2 == -1)) {
                return n1 == n2;
            }

            buffer1.flip();
            buffer2.flip();

            for (int i = 0; i < Math.min(n1, n2); i++) {
                if (buffer1.get() != buffer2.get()) {
                    return false;
                }
            }

            buffer1.compact();
            buffer2.compact();
        }

    } finally {
        if (stream1 != null) {
            stream1.close();
        }
        if (stream2 != null) {
            stream2.close();
        }
    }
}

From source file:com.glaf.core.util.FileUtils.java

public static byte[] getBytes(InputStream inputStream) {
    if (inputStream == null) {
        return null;
    }// w ww  .  j  a  v a  2s  . c  om
    ByteArrayOutputStream output = null;
    try {
        ByteBuffer buffer = ByteBuffer.allocate(8192);
        ReadableByteChannel readChannel = Channels.newChannel(inputStream);
        output = new ByteArrayOutputStream(32 * 1024);
        WritableByteChannel writeChannel = Channels.newChannel(output);
        while ((readChannel.read(buffer)) > 0 || buffer.position() != 0) {
            buffer.flip();
            writeChannel.write(buffer);
            buffer.compact();
        }
        return output.toByteArray();
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    } finally {
        if (output != null) {
            try {
                output.close();
                output = null;
            } catch (IOException ex) {
            }
        }
    }
}

From source file:com.glaf.core.util.ByteBufferUtils.java

/**
 * fill?/*from w  ww .ja va  2  s.co  m*/
 * 
 * @param buffer
 * @return
 */
public static int flipToFill(ByteBuffer buffer) {
    int position = buffer.position();
    int limit = buffer.limit();
    // flush??fill?
    if (position == limit) {
        buffer.position(0);
        buffer.limit(buffer.capacity());
        return 0;
    }
    // ?limit equal capacity,?
    int capacity = buffer.capacity();
    if (limit == capacity) {
        buffer.compact();
        return 0;
    }
    // ??
    buffer.position(limit);
    buffer.limit(capacity);
    return position;
}

From source file:org.activiti.engine.test.api.repository.diagram.ProcessDiagramRetrievalTest.java

private static boolean isEqual(InputStream stream1, InputStream stream2) throws IOException {

    ReadableByteChannel channel1 = Channels.newChannel(stream1);
    ReadableByteChannel channel2 = Channels.newChannel(stream2);

    ByteBuffer buffer1 = ByteBuffer.allocateDirect(1024);
    ByteBuffer buffer2 = ByteBuffer.allocateDirect(1024);

    try {//from w  w  w. ja v a2  s .  c o m
        while (true) {

            int bytesReadFromStream1 = channel1.read(buffer1);
            int bytesReadFromStream2 = channel2.read(buffer2);

            if (bytesReadFromStream1 == -1 || bytesReadFromStream2 == -1)
                return bytesReadFromStream1 == bytesReadFromStream2;

            buffer1.flip();
            buffer2.flip();

            for (int i = 0; i < Math.min(bytesReadFromStream1, bytesReadFromStream2); i++)
                if (buffer1.get() != buffer2.get())
                    return false;

            buffer1.compact();
            buffer2.compact();
        }

    } finally {
        if (stream1 != null)
            stream1.close();
        if (stream2 != null)
            stream2.close();
    }
}

From source file:org.alfresco.repo.transfer.HttpClientTransmitterImpl.java

private static void channelCopy(final ReadableByteChannel src, final WritableByteChannel dest)
        throws IOException {
    final ByteBuffer buffer = ByteBuffer.allocateDirect(2 * 1024);
    while (src.read(buffer) != -1) {
        // prepare the buffer to be drained
        buffer.flip();//from   w ww .j  a  va2s  .com
        // 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:org.mycore.common.content.util.MCRServletContentHelper.java

private static long copyChannel(final ReadableByteChannel src, final WritableByteChannel dest,
        final int bufferSize) throws IOException {
    if (src instanceof FileChannel) {
        return copyFileChannel((FileChannel) src, dest, bufferSize);
    }/*from   www. ja v a 2 s .com*/
    long bytes = 0;
    final ByteBuffer buffer = ByteBuffer.allocateDirect(bufferSize);
    while (src.read(buffer) != -1) {
        // prepare the buffer to be drained
        buffer.flip();

        // write to the channel, may block
        bytes += 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()) {
        bytes += dest.write(buffer);
    }
    return bytes;
}

From source file:com.bennavetta.appsite.file.ResourceService.java

private void copy(ReadableByteChannel src, WritableByteChannel dest) throws IOException {
    ByteBuffer buffer = ByteBuffer.allocateDirect(bufferSize.get());
    while (src.read(buffer) != -1 || buffer.position() != 0) {
        buffer.flip();/* w w  w  .j a  v a 2  s .co  m*/
        dest.write(buffer);
        buffer.compact();
    }
}

From source file:com.github.matthesrieke.jprox.JProxViaParameterServlet.java

private void copyChannel(final ReadableByteChannel src, final WritableByteChannel dest) throws IOException {
    final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);

    while (src.read(buffer) != -1) {
        buffer.flip();/*w ww .  j  av a  2  s  . c  o m*/
        dest.write(buffer);
        buffer.compact();
    }

    buffer.flip();

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