Example usage for java.nio.channels WritableByteChannel WritableByteChannel

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

Introduction

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

Prototype

WritableByteChannel

Source Link

Usage

From source file:Main.java

/**
 * @return//ww  w.  j a  v  a2 s .co m
 */
public static WritableByteChannel asWritableByteChannel(final ByteBuffer buffer) {
    return new WritableByteChannel() {

        private boolean open = true;

        public int write(ByteBuffer src) throws IOException {
            if (open == false) {
                throw new ClosedChannelException();
            }

            final int p = buffer.position();
            final int l = buffer.limit();
            final int r = src.remaining();

            buffer.limit(l + r);
            buffer.position(l);

            buffer.put(src);

            buffer.position(p);

            return r;
        }

        public void close() throws IOException {
            open = false;
        }

        public boolean isOpen() {
            return open;
        }

    };
}

From source file:rascal.object.name.MessageDigestObjectNameResolver.java

private byte[] getObjectHash(ObjectSource source) throws IOException {
    final MessageDigest digest = getMessageDigest();
    source.copyTo(new WritableByteChannel() {
        public int write(ByteBuffer src) throws IOException {
            int n = src.remaining();
            digest.update(src);/*from  ww  w .  j  ava  2s  .  c o  m*/
            return n;
        }

        public boolean isOpen() {
            return true;
        }

        public void close() throws IOException {
        }
    });
    return digest.digest();
}