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:org.paxle.crawler.proxy.impl.ProxyForwarder.java

@Execution(Execution.MULTITHREADED)
public boolean onData(NonBlockingBodyDataSource bodyDataSource) throws BufferUnderflowException {
    try {//www.ja v a 2  s . co m
        int available = bodyDataSource.available();
        if (available > 0) {
            ByteBuffer[] data = bodyDataSource.readByteBufferByLength(available);
            if (this.fileDataSink != null) {
                for (ByteBuffer buf : data) {
                    ByteBuffer copy = buf.duplicate();
                    while (copy.hasRemaining()) {
                        this.fileDataSink.write(copy.get());
                    }

                    //                  Charset charset = Charset.forName("UTF-8");
                    //                  System.out.print(charset.decode(buf.duplicate()).toString());

                }
            }
            this.clientDataSink.write(data);
        } else if (available == -1) {
            this.clientDataSink.close();
            if (this.fileDataSink != null)
                this.fileDataSink.close();
        }
    } catch (Throwable ioe) {
        if (this.fileDataSink != null)
            try {
                this.fileDataSink.close();
            } catch (IOException e) {
                /* ignore this */ }
        this.clientDataSink.destroy();
        this.logger.error(String.format("Unexpected '%s': %s", ioe.getClass().getName(), ioe.getMessage()),
                ioe);
    }

    return true;
}

From source file:com.dianping.puma.parser.mysql.event.AbstractBinlogEvent.java

public boolean isRemaining(ByteBuffer buf, PumaContext context) {
    return context.isCheckSum() ? buf.remaining() - 4 > 0 : buf.hasRemaining();
}

From source file:me.xingrz.prox.tcp.tunnel.OutgoingTunnel.java

@Override
protected void handshake() {
    if (proxy == null) {
        super.handshake();
        return;/*from   ww w. j  a v  a2 s  .com*/
    }

    ByteBuffer handshake = proxy.handshake();
    if (handshake == null || !handshake.hasRemaining()) {
        super.handshake();
        return;
    }

    if (!writeInternal(handshake)) {
        logger.w("Failed to handshake");
        IOUtils.closeQuietly(this);
    }
}

From source file:org.wso2.carbon.http2.transport.util.http2Encoder.java

@Override
public int write(ByteBuffer src) throws IOException {
    int l = 0;//w  ww  .  j a v  a  2 s.c  om
    //channel.newPromise();

    while (src.hasRemaining()) {
        byte[] b;//= new byte[chContext.channel().alloc().buffer().capacity()];
        //  if(src.remaining()<b.length){
        b = new byte[src.remaining()];
        src.get(b);
        // request.replace(Unpooled.wrappedBuffer(b));
        if (src.hasRemaining())
            encoder.writeData(chContext, streamId, Unpooled.wrappedBuffer(b), 0, false, promise);
        else {
            encoder.writeData(chContext, streamId, Unpooled.wrappedBuffer(b), 0, true, promise);
            isComplete = true;
        }
    }

    return src.position();
}

From source file:MainClass.java

public void run() {
    try {//w w  w .  j  a v a 2s .c  o  m
        ByteBuffer buffer = ByteBuffer.allocate(4);
        buffer.putInt(this.howMany);
        buffer.flip();
        while (buffer.hasRemaining())
            out.write(buffer);

        for (int i = 0; i < howMany; i++) {
            byte[] data = new BigInteger(Integer.toString(i)).toByteArray();
            buffer = ByteBuffer.allocate(4 + data.length);

            buffer.putInt(data.length);
            buffer.put(data);
            buffer.flip();

            while (buffer.hasRemaining())
                out.write(buffer);
        }
        out.close();
        System.err.println("Closed");
    } catch (IOException ex) {
        System.err.println(ex);
    }
}

From source file:com.conwet.xjsp.features.MessageChannel.java

synchronized public void sendFragment(String fragment) throws IOException {
    ByteBuffer buffer = ByteBuffer.wrap(fragment.getBytes(StandardCharsets.UTF_8));

    try {// w  ww.j  a  v  a 2s. co  m
        while (buffer.hasRemaining()) {
            channel.write(buffer);
        }
    } catch (IOException ex) {
        logger.error("Closing connection on exception", ex);
        throw ex;
    }
}

From source file:com.codefollower.lealone.omid.tso.persistence.LoggerProtocol.java

/**
 * Execute a logged entry (several logged ops)
 * @param bb Serialized operations// w  w w  .  ja v  a2 s.  co  m
 */
void execute(ByteBuffer bb) {
    boolean done = !bb.hasRemaining();
    while (!done) {
        byte op = bb.get();
        long timestamp, startTimestamp, commitTimestamp;
        if (LOG.isTraceEnabled()) {
            LOG.trace("Operation: " + op);
        }
        switch (op) {
        case TIMESTAMP_ORACLE:
            timestamp = bb.getLong();
            this.getTimestampOracle().initialize(timestamp);
            this.initialize();
            oracle = true;
            break;
        case COMMIT:
            startTimestamp = bb.getLong();
            commitTimestamp = bb.getLong();
            processCommit(startTimestamp, commitTimestamp);
            if (commitTimestamp < largestDeletedTimestamp) {
                commits = true;
            }
            break;
        case LARGEST_DELETED_TIMESTAMP:
            timestamp = bb.getLong();
            processLargestDeletedTimestamp(timestamp);

            break;
        case ABORT:
            timestamp = bb.getLong();
            processHalfAbort(timestamp);

            break;
        case FULL_ABORT:
            timestamp = bb.getLong();
            processFullAbort(timestamp);

            break;
        case LOG_START:
            consumed = true;
            break;
        case SNAPSHOT:
            int snapshot = (int) bb.getLong();
            if (snapshot > this.snapshot) {
                this.snapshot = snapshot;
                this.hasSnapshot = true;
            }
            if (hasSnapshot && snapshot < this.snapshot) {
                this.aborts = true;
            }
            break;
        }
        if (bb.remaining() == 0)
            done = true;
    }
}

From source file:com.yahoo.omid.tso.persistence.LoggerProtocol.java

/**
 * Execute a logged entry (several logged ops)
 * @param bb Serialized operations/*from w w  w .j av  a2  s  . c  o m*/
 */
void execute(ByteBuffer bb) {
    boolean done = !bb.hasRemaining();
    while (!done) {
        byte op = bb.get();
        long timestamp, startTimestamp, commitTimestamp;
        if (LOG.isTraceEnabled()) {
            LOG.trace("Operation: " + op);
        }
        switch (op) {
        case TIMESTAMPORACLE:
            timestamp = bb.getLong();
            this.getSO().initialize(timestamp);
            this.initialize();
            oracle = true;
            break;
        case COMMIT:
            startTimestamp = bb.getLong();
            commitTimestamp = bb.getLong();
            processCommit(startTimestamp, commitTimestamp);
            if (commitTimestamp < largestDeletedTimestamp) {
                commits = true;
            }
            break;
        case LARGESTDELETEDTIMESTAMP:
            timestamp = bb.getLong();
            processLargestDeletedTimestamp(timestamp);

            break;
        case ABORT:
            timestamp = bb.getLong();
            processAbort(timestamp);

            break;
        case FULLABORT:
            timestamp = bb.getLong();
            processFullAbort(timestamp);

            break;
        case LOGSTART:
            consumed = true;
            break;
        case SNAPSHOT:
            int snapshot = (int) bb.getLong();
            if (snapshot > this.snapshot) {
                this.snapshot = snapshot;
                this.hasSnapshot = true;
            }
            if (hasSnapshot && snapshot < this.snapshot) {
                this.aborts = true;
            }
            break;
        }
        if (bb.remaining() == 0)
            done = true;
    }
}

From source file:org.pvalsecc.comm.ServerConnection.java

/**
 * Called when the socket is ready to send.
 *//*from  w  ww .  j  av a2  s.  c  o  m*/
public synchronized int send(SocketChannel socket) throws IOException {
    ByteBuffer buffer = toSend.peek();
    int bytesSent = socket.write(buffer);
    if (!buffer.hasRemaining()) {
        toSend.remove();

        if (!hasSomeMoreDataToSend()) { //no more data to send, set the selector back to read-only
            key.interestOps(SelectionKey.OP_READ);
        }
    }

    return bytesSent;
}

From source file:org.apache.james.mpt.session.ExternalSession.java

public void writeLine(String line) throws Exception {
    monitor.note("-> " + line);
    monitor.debug("[Writing line]");
    ByteBuffer writeBuffer = ascii.encode(line);
    while (writeBuffer.hasRemaining()) {
        socket.write(writeBuffer);//from   w w  w  .  j  a  v a 2s.c o m
    }
    lineEndBuffer.rewind();
    while (lineEndBuffer.hasRemaining()) {
        socket.write(lineEndBuffer);
    }
    monitor.debug("[Done]");
}