Example usage for java.nio.channels ReadableByteChannel ReadableByteChannel

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

Introduction

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

Prototype

ReadableByteChannel

Source Link

Usage

From source file:Main.java

public static ReadableByteChannel asReadableByteChannel(final ByteBuffer buffer) {

    return new ReadableByteChannel() {

        private boolean open = true;

        public int read(ByteBuffer dst) throws IOException {
            if (open == false) {
                throw new ClosedChannelException();
            }/*from ww  w  .  j a v  a 2 s .c  om*/

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

            buffer.limit(p + r);

            dst.put(buffer);

            buffer.limit(l);

            return r;
        }

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

        public boolean isOpen() {
            return open;
        }

    };
}

From source file:neembuu.uploader.external.HttpUtil.java

private static ReadableByteChannel wrap(final InputStream is, final HttpEntity he, final Content c,
        final double contentLength) {
    return new ReadableByteChannel() {
        double total = 0;

        @Override/*from w ww  . j a  va  2s.  c om*/
        public int read(ByteBuffer dst) throws IOException {
            byte[] b = new byte[dst.capacity()];
            int r = is.read(b);
            //this sleep is just to slow down update to see, if the UI is working or not !
            // NU's update is very very very fast
            //try{Thread.sleep(1000);}catch(Exception a){}
            dst.put(b, 0, r);
            total += r;
            c.setProgress(total / contentLength);
            return r;
        }

        @Override
        public boolean isOpen() {
            return he.isStreaming();
        }

        @Override
        public void close() throws IOException {
            is.close();
        }
    };
}

From source file:com.sshtools.j2ssh.net.ConnectedSocketChannelTransportProvider.java

public ReadableByteChannel getReadableByteChannel() throws IOException {
    return new ReadableByteChannel() {
        public int read(ByteBuffer dst) throws IOException {
            return socketChannel.read(dst);
        }/*from ww  w .  j  a v a  2s. com*/

        public void close() throws IOException {
            socketChannel.close();
        }

        public boolean isOpen() {
            return socketChannel.isOpen();
        }
    };
}

From source file:org.callimachusproject.server.AccessLog.java

InputStream logOnClose(final String addr, final String username, final String line, final int code,
        final long length, final Header referer, final Header agent, InputStream in) {
    final ReadableByteChannel delegate = ChannelUtil.newChannel(in);
    return ChannelUtil.newInputStream(new ReadableByteChannel() {
        private long size = 0;
        private boolean complete;
        private boolean error;

        public boolean isOpen() {
            return delegate.isOpen();
        }//  w  ww  .  ja v a  2s.  c o  m

        public synchronized void close() throws IOException {
            delegate.close();
            if (!complete) {
                complete = true;
                if (error) {
                    log(addr, username, line, 599, size, referer, agent);
                } else if (size < length) {
                    log(addr, username, line, 499, size, referer, agent);
                } else {
                    log(addr, username, line, code, size, referer, agent);
                }
            }
        }

        public synchronized int read(ByteBuffer dst) throws IOException {
            error = true;
            int read = delegate.read(dst);
            if (read < 0) {
                complete = true;
                log(addr, username, line, code, size, referer, agent);
            } else {
                size += read;
            }
            error = false;
            return read;
        }
    });
}