Example usage for java.nio ByteBuffer hasRemaining

List of usage examples for java.nio ByteBuffer hasRemaining

Introduction

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

Prototype

public final boolean hasRemaining() 

Source Link

Document

Indicates if there are elements remaining in this buffer, that is if position < limit .

Usage

From source file:HttpDownloadManager.java

public void run() {
    log.info("HttpDownloadManager thread starting.");

    // The download thread runs until release() is called
    while (!released) {
        // The thread blocks here waiting for something to happen
        try {/*  ww w  .  j av a2  s.  com*/
            selector.select();
        } catch (IOException e) {
            // This should never happen.
            log.log(Level.SEVERE, "Error in select()", e);
            return;
        }

        // If release() was called, the thread should exit.
        if (released)
            break;

        // If any new Download objects are pending, deal with them first
        if (!pendingDownloads.isEmpty()) {
            // Although pendingDownloads is a synchronized list, we still
            // need to use a synchronized block to iterate through its
            // elements to prevent a concurrent call to download().
            synchronized (pendingDownloads) {
                Iterator iter = pendingDownloads.iterator();
                while (iter.hasNext()) {
                    // Get the pending download object from the list
                    DownloadImpl download = (DownloadImpl) iter.next();
                    iter.remove(); // And remove it.

                    // Now begin an asynchronous connection to the
                    // specified host and port. We don't block while
                    // waiting to connect.
                    SelectionKey key = null;
                    SocketChannel channel = null;
                    try {
                        // Open an unconnected channel
                        channel = SocketChannel.open();
                        // Put it in non-blocking mode
                        channel.configureBlocking(false);
                        // Register it with the selector, specifying that
                        // we want to know when it is ready to connect
                        // and when it is ready to read.
                        key = channel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_CONNECT,
                                download);
                        // Create the web server address
                        SocketAddress address = new InetSocketAddress(download.host, download.port);
                        // Ask the channel to start connecting
                        // Note that we don't send the HTTP request yet.
                        // We'll do that when the connection completes.
                        channel.connect(address);
                    } catch (Exception e) {
                        handleError(download, channel, key, e);
                    }
                }
            }
        }

        // Now get the set of keys that are ready for connecting or reading
        Set keys = selector.selectedKeys();
        if (keys == null)
            continue; // bug workaround; should not be needed
        // Loop through the keys in the set
        for (Iterator i = keys.iterator(); i.hasNext();) {
            SelectionKey key = (SelectionKey) i.next();
            i.remove(); // Remove the key from the set before handling

            // Get the Download object we attached to the key
            DownloadImpl download = (DownloadImpl) key.attachment();
            // Get the channel associated with the key.
            SocketChannel channel = (SocketChannel) key.channel();

            try {
                if (key.isConnectable()) {
                    // If the channel is ready to connect, complete the
                    // connection and then send the HTTP GET request to it.
                    if (channel.finishConnect()) {
                        download.status = Status.CONNECTED;
                        // This is the HTTP request we wend
                        String request = "GET " + download.path + " HTTP/1.1\r\n" + "Host: " + download.host
                                + "\r\n" + "Connection: close\r\n" + "\r\n";
                        // Wrap in a CharBuffer and encode to a ByteBuffer
                        ByteBuffer requestBytes = LATIN1.encode(CharBuffer.wrap(request));
                        // Send the request to the server. If the bytes
                        // aren't all written in one call, we busy loop!
                        while (requestBytes.hasRemaining())
                            channel.write(requestBytes);

                        log.info("Sent HTTP request: " + download.host + ":" + download.port + ": " + request);
                    }
                }
                if (key.isReadable()) {
                    // If the key indicates that there is data to be read,
                    // then read it and store it in the Download object.
                    int numbytes = channel.read(buffer);

                    // If we read some bytes, store them, otherwise
                    // the download is complete and we need to note this
                    if (numbytes != -1) {
                        buffer.flip(); // Prepare to drain the buffer
                        download.addData(buffer); // Store the data
                        buffer.clear(); // Prepare for another read
                        log.info("Read " + numbytes + " bytes from " + download.host + ":" + download.port);
                    } else {
                        // If there are no more bytes to read
                        key.cancel(); // We're done with the key
                        channel.close(); // And with the channel.
                        download.status = Status.DONE;
                        if (download.listener != null) // notify listener
                            download.listener.done(download);
                        log.info("Download complete from " + download.host + ":" + download.port);
                    }
                }
            } catch (Exception e) {
                handleError(download, channel, key, e);
            }
        }
    }
    log.info("HttpDownloadManager thread exiting.");
}

From source file:com.buaa.cfs.net.SocketIOWithTimeout.java

/**
 * Performs one IO and returns number of bytes read or written. It waits up to the specified timeout. If the channel
 * is not read before the timeout, SocketTimeoutException is thrown.
 *
 * @param buf buffer for IO/*from   w w w.  j  a v a 2  s  .  c o  m*/
 * @param ops Selection Ops used for waiting. Suggested values: SelectionKey.OP_READ while reading and
 *            SelectionKey.OP_WRITE while writing.
 *
 * @return number of bytes read or written. negative implies end of stream.
 *
 * @throws IOException
 */
int doIO(ByteBuffer buf, int ops) throws IOException {

    /* For now only one thread is allowed. If user want to read or write
     * from multiple threads, multiple streams could be created. In that
     * case multiple threads work as well as underlying channel supports it.
     */
    if (!buf.hasRemaining()) {
        throw new IllegalArgumentException("Buffer has no data left.");
        //or should we just return 0?
    }

    while (buf.hasRemaining()) {
        if (closed) {
            return -1;
        }

        try {
            int n = performIO(buf);
            if (n != 0) {
                // successful io or an error.
                return n;
            }
        } catch (IOException e) {
            if (!channel.isOpen()) {
                closed = true;
            }
            throw e;
        }

        //now wait for socket to be ready.
        int count = 0;
        try {
            count = selector.select(channel, ops, timeout);
        } catch (IOException e) { //unexpected IOException.
            closed = true;
            throw e;
        }

        if (count == 0) {
            throw new SocketTimeoutException(timeoutExceptionString(channel, timeout, ops));
        }
        // otherwise the socket should be ready for io.
    }

    return 0; // does not reach here.
}

From source file:ed.net.lb.LBCall.java

protected WhatToDo handleRead(ByteBuffer buf, Connection conn) {

    _logger.debug(3, "handleRead  _state:" + _state);

    if (_state == State.WAITING || _state == State.IN_HEADER) {
        // TODO: should i read this all in at once                
        if (_line == null)
            _line = new StringBuilder();
        while (buf.hasRemaining()) {
            char c = (char) buf.get();
            if (c == '\r')
                continue;
            if (c == '\n') {
                if (_line.length() == 0) {
                    _logger.debug(3, "end of header");
                    _state = State.READY_TO_STREAM;
                    break;
                }/*from  www.j  a  v a  2 s.co  m*/

                String line = _line.toString();

                if (_state == State.WAITING) {
                    int idx = line.indexOf(" ");
                    if (idx < 0) {
                        backendError(ServerErrorType.INVALID, "invalid first line [" + line + "]");
                        return WhatToDo.ERROR;
                    }
                    line = line.substring(idx + 1).trim();
                    idx = line.indexOf(" ");
                    if (idx < 0)
                        _response.setResponseCode(Integer.parseInt(line));
                    else
                        _response.setStatus(Integer.parseInt(line.substring(0, idx)),
                                line.substring(idx + 1).trim());

                    _logger.debug(3, "got first line ", line);
                    _state = State.IN_HEADER;
                } else {
                    int idx = line.indexOf(":");
                    if (idx < 0) {
                        backendError(ServerErrorType.INVALID, "invalid line [ " + line + "]");
                        return WhatToDo.ERROR;
                    }
                    String name = line.substring(0, idx);
                    String value = line.substring(idx + 1).trim();
                    _logger.debug(3, "got header line ", line);

                    if (name.equalsIgnoreCase("Connection")) {
                        _keepalive = !value.toLowerCase().contains("close");
                    } else {
                        _response.addHeader(name, value);
                    }

                }
                _line.setLength(0);
                continue;
            }
            _line.append(c);
        }

        if (_state != State.READY_TO_STREAM)
            return WhatToDo.CONTINUE;

        _logger.debug(3, "starting to stream data");
    }

    if (_state == State.READY_TO_STREAM) {
        MyChunk chunk = new MyChunk(this, conn, _response.getContentLength(), buf);
        _response.sendFile(new MySender(chunk));
        _state = State.STREAMING;
    }

    try {
        _response.done();
    } catch (IOException ioe) {
        _logger.debug(1, "client error", ioe);
        return WhatToDo.CLIENT_ERROR;
    }

    if (isDone() && !_keepalive)
        return WhatToDo.DONE_AND_CLOSE;

    if (_request.isHeadRequest()) {
        assert (_response.isFullySent());
        _lbsuccess(conn);
        return WhatToDo.DONE_AND_CONTINUE;
    }

    return WhatToDo.PAUSE;
}

From source file:org.bimserver.collada.ColladaSerializer.java

private List<String> intBufferToStringList(ByteBuffer buffer, Format formatter) {
    List<Integer> list = new ArrayList<Integer>();
    while (buffer.hasRemaining())
        list.add(new Integer(buffer.getInt()));
    // Get the data as a list of String objects.
    return listToStringList(list, formatter);
}

From source file:org.cloudata.core.common.io.CText.java

/**
 * Finds any occurence of <code>what</code> in the backing
 * buffer, starting as position <code>start</code>. The starting
 * position is measured in bytes and the return value is in
 * terms of byte position in the buffer. The backing buffer is
 * not converted to a string for this operation.
 * @return byte position of the first occurence of the search
 *         string in the UTF-8 buffer or -1 if not found
 *///from w  w  w. j  a va2s.c o  m
public int find(String what, int start) {
    try {
        ByteBuffer src = ByteBuffer.wrap(this.bytes);
        ByteBuffer tgt = encode(what);
        byte b = tgt.get();
        src.position(start);

        while (src.hasRemaining()) {
            if (b == src.get()) { // matching first byte
                src.mark(); // save position in loop
                tgt.mark(); // save position in target
                boolean found = true;
                int pos = src.position() - 1;
                while (tgt.hasRemaining()) {
                    if (!src.hasRemaining()) { // src expired first
                        tgt.reset();
                        src.reset();
                        found = false;
                        break;
                    }
                    if (!(tgt.get() == src.get())) {
                        tgt.reset();
                        src.reset();
                        found = false;
                        break; // no match
                    }
                }
                if (found)
                    return pos;
            }
        }
        return -1; // not found
    } catch (CharacterCodingException e) {
        // can't get here
        e.printStackTrace();
        return -1;
    }
}

From source file:org.apache.hama.util.SocketIOWithTimeout.java

/**
 * Performs one IO and returns number of bytes read or written. It waits up to
 * the specified timeout. If the channel is not read before the timeout,
 * SocketTimeoutException is thrown.//from  w  w w . ja v a 2 s  .co m
 * 
 * @param buf buffer for IO
 * @param ops Selection Ops used for waiting. Suggested values:
 *          SelectionKey.OP_READ while reading and SelectionKey.OP_WRITE while
 *          writing.
 * 
 * @return number of bytes read or written. negative implies end of stream.
 * @throws IOException
 */
int doIO(ByteBuffer buf, int ops) throws IOException {

    /*
     * For now only one thread is allowed. If user want to read or write from
     * multiple threads, multiple streams could be created. In that case
     * multiple threads work as well as underlying channel supports it.
     */
    if (!buf.hasRemaining()) {
        throw new IllegalArgumentException("Buffer has no data left.");
        // or should we just return 0?
    }

    while (buf.hasRemaining()) {
        if (closed) {
            return -1;
        }

        try {
            int n = performIO(buf);
            if (n != 0) {
                // successful io or an error.
                return n;
            }
        } catch (IOException e) {
            if (!channel.isOpen()) {
                closed = true;
            }
            throw e;
        }

        // now wait for socket to be ready.
        int count = 0;
        try {
            count = selector.select(channel, ops, timeout);
        } catch (IOException e) { // unexpected IOException.
            closed = true;
            throw e;
        }

        if (count == 0) {
            throw new SocketTimeoutException(timeoutExceptionString(channel, timeout, ops));
        }
        // otherwise the socket should be ready for io.
    }

    return 0; // does not reach here.
}

From source file:org.alfresco.cacheserver.dropwizard.resources.CacheServerResource.java

private void fastChannelCopy(final ReadableByteChannel src, final WritableByteChannel dest) throws IOException {
    final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
    while (src.read(buffer) != -1) {
        // prepare the buffer to be drained
        buffer.flip();//from  w  w  w  .  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:com.openteach.diamond.network.waverider.network.DefaultNetWorkClient.java

/**
 * ?, ?inputBuffer//from ww w. j  a  v  a 2s. c  om
 * @param key
 * @throws IOException
 * @throws InterruptedException
 */
private void onRead(SelectionKey key) throws IOException, InterruptedException {
    logger.debug("onRead");
    // ?
    ByteBuffer readByteBuffer = ByteBuffer.allocate(NetWorkConstants.DEFAULT_NETWORK_BUFFER_SIZE);
    int ret = 0;
    do {
        ret = socketChannel.read(readByteBuffer);
    } while (ret > 0);

    // , ???
    if (ret == -1) {
        throw new IOException("EOF");
    }
    readByteBuffer.flip();
    if (readByteBuffer.hasRemaining()) {
        inputBuffer.put(readByteBuffer);
        synchronized (waitMoreDataLock) {
            waitMoreDataLock.notifyAll();
        }
    }

    //logger.info("Slave read " + readByteBuffer.remaining() + " bytes");
}

From source file:eu.stratosphere.nephele.net.SocketIOWithTimeout.java

/**
 * Performs one IO and returns number of bytes read or written.
 * It waits up to the specified timeout. If the channel is
 * not read before the timeout, SocketTimeoutException is thrown.
 * /* w  w  w  .j ava2s  . co m*/
 * @param buf
 *        buffer for IO
 * @param ops
 *        Selection Ops used for waiting. Suggested values:
 *        SelectionKey.OP_READ while reading and SelectionKey.OP_WRITE while
 *        writing.
 * @return number of bytes read or written. negative implies end of stream.
 * @throws IOException
 */
int doIO(ByteBuffer buf, int ops) throws IOException {

    /*
     * For now only one thread is allowed. If user want to read or write
     * from multiple threads, multiple streams could be created. In that
     * case multiple threads work as well as underlying channel supports it.
     */
    if (!buf.hasRemaining()) {
        throw new IllegalArgumentException("Buffer has no data left.");
        // or should we just return 0?
    }

    while (buf.hasRemaining()) {
        if (closed) {
            return -1;
        }

        try {
            int n = performIO(buf);
            if (n != 0) {
                // successful io or an error.
                return n;
            }
        } catch (IOException e) {
            if (!channel.isOpen()) {
                closed = true;
            }
            throw e;
        }

        // now wait for socket to be ready.
        int count = 0;
        try {
            count = selector.select(channel, ops, timeout);
        } catch (IOException e) { // unexpected IOException.
            closed = true;
            throw e;
        }

        if (count == 0) {
            throw new SocketTimeoutException(timeoutExceptionString(channel, timeout, ops));
        }
        // otherwise the socket should be ready for io.
    }

    return 0; // does not reach here.
}

From source file:io.Text.java

/**
 * Finds any occurence of <code>what</code> in the backing
 * buffer, starting as position <code>start</code>. The starting
 * position is measured in bytes and the return value is in
 * terms of byte position in the buffer. The backing buffer is
 * not converted to a string for this operation.
 * @return byte position of the first occurence of the search
 *         string in the UTF-8 buffer or -1 if not found
 *//*from   w w w. j  a va 2  s .c  o m*/
public int find(String what, int start) {
    try {
        ByteBuffer src = ByteBuffer.wrap(this.bytes, 0, this.length);
        ByteBuffer tgt = encode(what);
        byte b = tgt.get();
        src.position(start);

        while (src.hasRemaining()) {
            if (b == src.get()) { // matching first byte
                src.mark(); // save position in loop
                tgt.mark(); // save position in target
                boolean found = true;
                int pos = src.position() - 1;
                while (tgt.hasRemaining()) {
                    if (!src.hasRemaining()) { // src expired first
                        tgt.reset();
                        src.reset();
                        found = false;
                        break;
                    }
                    if (!(tgt.get() == src.get())) {
                        tgt.reset();
                        src.reset();
                        found = false;
                        break; // no match
                    }
                }
                if (found)
                    return pos;
            }
        }
        return -1; // not found
    } catch (CharacterCodingException e) {
        // can't get here
        e.printStackTrace();
        return -1;
    }
}