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.cloudata.core.commitlog.pipe.Message.java

public boolean write(SocketChannel channel) throws IOException {
    if (writingHeaderPhase) {
        ByteBuffer dupHeaderBuf = duplicate(headerBuf);
        dupHeaderBuf.position(writtenPosition);
        dupHeaderBuf.limit(headerBuf.capacity());
        channel.write(dupHeaderBuf);//from  w  w w.  java2  s .com

        if (dupHeaderBuf.hasRemaining()) {
            writtenPosition = dupHeaderBuf.position();
            return false;
        }
        dupHeaderBuf.clear();
        writtenPosition = 0;
        writingHeaderPhase = false;
    }

    ByteBuffer dupBuffer = duplicate(buffer);
    dupBuffer.position(writtenPosition);
    dupBuffer.limit(buffer.capacity());
    channel.write(dupBuffer);

    if (dupBuffer.hasRemaining()) {
        writtenPosition = dupBuffer.position();
        return false;
    }

    writtenPosition = 0;
    writingHeaderPhase = true;
    dupBuffer.clear();
    return true;
}

From source file:fr.cls.atoll.motu.processor.wps.StringList.java

public static void print(ByteBuffer bb) {
    while (bb.hasRemaining()) {
        System.out.print(bb.get() + " ");
    }/*from   w w  w  .  j  a va2  s.  co  m*/
    System.out.println();
    bb.rewind();
}

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 w ww  . j a va2s .  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:me.xingrz.prox.tcp.tunnel.Tunnel.java

protected final boolean writeInternal(ByteBuffer buffer) {
    if (closed || remaining == null) {
        return false;
    }//from  ww w . j  a va  2 s .c om

    try {
        while (buffer.hasRemaining()) {
            if (channel.write(buffer) == 0) {
                break;
            }
        }
    } catch (IOException e) {
        logger.w(e, "Failed writing to " + channelToString());
        return false;
    }

    if (buffer.hasRemaining()) {
        keepRemaining(buffer);
        return false;
    } else {
        return true;
    }
}

From source file:edu.umn.cs.spatialHadoop.indexing.BTRPartitioner.java

@Override
public void write(DataOutput out) throws IOException {
    mbr.write(out);/*  w  w  w  .java2s .  c o  m*/
    out.writeInt(columns);
    out.writeInt(rows);
    ByteBuffer bbuffer = ByteBuffer.allocate((xSplits.length + ySplits.length) * 8);
    for (double xSplit : xSplits)
        bbuffer.putDouble(xSplit);
    for (double ySplit : ySplits)
        bbuffer.putDouble(ySplit);
    if (bbuffer.hasRemaining())
        throw new RuntimeException("Did not calculate buffer size correctly");
    out.write(bbuffer.array(), bbuffer.arrayOffset(), bbuffer.position());
}

From source file:org.cloudata.core.commitlog.pipe.CloseState.java

public boolean writeToNext(Context ctx) throws IOException {
    LOG.debug("writeToNext");

    if (!ctx.isLastPipe) {
        LOG.debug("not last pipe");
        ByteBuffer msg = closeMessage.duplicate();
        msg.position(ctx.closeMessagePos);
        try {/*from  www . j  a  v a  2 s.c  o  m*/
            ctx.closeMessagePos = ctx.nextChannel.write(msg);
        } catch (IOException e) {
            throw new PipeNormallyClosed();
        }

        if (msg.hasRemaining()) {
            ctx.deregisterFromSelect(ctx.nextChannel, SelectionKey.OP_WRITE);
            return false;
        }
    } else {
        LOG.debug("send ok ack to prev");
        ctx.closeMessagePos = 0;
        ctx.ack.clear();
        ctx.ack.put(Constants.PIPE_DISCONNECT_OK);
        ctx.ack.flip();
        return writeToPrev(ctx);
    }

    return true;
}

From source file:edu.umn.cs.spatialHadoop.indexing.BTRPartitioner.java

@Override
public void readFields(DataInput in) throws IOException {
    mbr.readFields(in);//from  w  ww .  jav  a  2  s .  c  om
    columns = in.readInt();
    rows = in.readInt();
    xSplits = new double[columns];
    ySplits = new double[columns * rows];

    int bufferLength = (xSplits.length + ySplits.length) * 8;
    byte[] buffer = new byte[bufferLength];
    in.readFully(buffer);
    ByteBuffer bbuffer = ByteBuffer.wrap(buffer);
    for (int i = 0; i < xSplits.length; i++)
        xSplits[i] = bbuffer.getDouble();
    for (int i = 0; i < ySplits.length; i++)
        ySplits[i] = bbuffer.getDouble();
    if (bbuffer.hasRemaining())
        throw new RuntimeException("Error reading STR partitioner");
}

From source file:com.andrewkroh.cicso.rtp.AudioFileStreamer.java

/**
 * Sends a single packet of audio data. It reads from the
 * {@link #outputDataBuffer} and will rewind that buffer when it reaches the
 * end so that it continuously streams the source file in a loop.
 *///from   w  w w .j a  va 2 s  .  co  m
private void sendAudioData() {
    ByteBuffer packetDataBuffer = ByteBuffer.allocate(payloadSizeBytes);

    while (packetDataBuffer.hasRemaining()) {
        if (!outputDataBuffer.hasRemaining()) {
            outputDataBuffer.rewind();
        }

        packetDataBuffer.put(outputDataBuffer.get());
    }

    timestamp += numSamplesPerPacket;

    RtpPacket packet = new RtpPacket();
    packet.setPayloadType(outputEncodingType.getPayloadType());
    packet.setSSRC(ssrc);
    packet.setSequenceNumber(++sequenceNumber);
    packet.setTimestamp(timestamp);
    packet.setRtpPayloadData(packetDataBuffer.array());

    rtpSession.sendData(packet);
}

From source file:com.zotoh.maedr.device.apache.StreamingNHttpEntity.java

private void sockItDown(ContentDecoder decoder) throws IOException {

    tlog().debug("StreamingNHttpEntity: sockItDown()");

    ByteBuffer buffer;
    int cnt;//from  w w  w  .ja  v a2s . com

    buffer = _alloctor.allocate(4096);
    do {

        buffer.clear();

        if ((cnt = decoder.read(buffer)) == -1)
            break;

        if (cnt == 0) {

            if (buffer.hasRemaining())
                break;
            else
                continue;
        }

        // 
        buffer.flip();
        byte[] bits = new byte[4096];
        int len;

        while (buffer.hasRemaining()) {
            len = Math.min(4096, buffer.remaining());
            buffer.get(bits, 0, len);
            storeBytes(bits, len);
        }
    } while (true);

}