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.codehaus.groovy.grails.web.util.StreamByteBuffer.java

public String readAsString(Charset charset) throws CharacterCodingException {
    int unreadSize = totalBytesUnread();
    if (unreadSize > 0) {
        CharsetDecoder decoder = charset.newDecoder().onMalformedInput(CodingErrorAction.REPLACE)
                .onUnmappableCharacter(CodingErrorAction.REPLACE);
        CharBuffer charbuffer = CharBuffer.allocate(unreadSize);
        ByteBuffer buf = null;
        while (prepareRead() != -1) {
            buf = currentReadChunk.readToNioBuffer();
            boolean endOfInput = (prepareRead() == -1);
            CoderResult result = decoder.decode(buf, charbuffer, endOfInput);
            if (endOfInput) {
                if (!result.isUnderflow()) {
                    result.throwException();
                }/*  w w w.  j a  v  a2s.c om*/
            }
        }
        CoderResult result = decoder.flush(charbuffer);
        if (buf.hasRemaining()) {
            throw new IllegalStateException("There's a bug here, buffer wasn't read fully.");
        }
        if (!result.isUnderflow()) {
            result.throwException();
        }
        charbuffer.flip();
        String str;
        if (charbuffer.hasArray()) {
            int len = charbuffer.remaining();
            char[] ch = charbuffer.array();
            if (len != ch.length) {
                ch = ArrayUtils.subarray(ch, 0, len);
            }
            str = StringCharArrayAccessor.createString(ch);
        } else {
            str = charbuffer.toString();
        }
        return str;
    }
    return null;
}

From source file:org.apache.hc.client5.http.impl.auth.CredSspScheme.java

private byte[] encodeUnicode(final CharBuffer charBuffer) {
    if (charBuffer == null) {
        return EMPTYBUFFER;
    }/*from   ww  w.j a  v a 2  s  . co  m*/
    final ByteBuffer encoded = UNICODE_LITTLE_UNMARKED.encode(charBuffer);
    if (!encoded.hasRemaining()) {
        return EMPTYBUFFER;
    }
    final byte[] bytes = new byte[encoded.remaining()];
    encoded.get(bytes);
    return bytes;
}

From source file:com.healthmarketscience.jackcess.impl.UsageMap.java

/**
 * Read in the page numbers in this inline map
 *///w  ww .  j a  v a 2s  .c  o m
protected void processMap(ByteBuffer buffer, int bufferStartPage) {
    int byteCount = 0;
    while (buffer.hasRemaining()) {
        byte b = buffer.get();
        if (b != (byte) 0) {
            for (int i = 0; i < 8; i++) {
                if ((b & (1 << i)) != 0) {
                    int pageNumberOffset = (byteCount * 8 + i) + bufferStartPage;
                    int pageNumber = bitIndexToPageNumber(pageNumberOffset, PageChannel.INVALID_PAGE_NUMBER);
                    if (!isPageWithinRange(pageNumber)) {
                        throw new IllegalStateException("found page number " + pageNumber
                                + " in usage map outside of expected range " + _startPage + " to " + _endPage);
                    }
                    _pageNumbers.set(pageNumberOffset);
                }
            }
        }
        byteCount++;
    }
}

From source file:org.apache.hadoop.mapred.FadvisedFileRegion.java

/**
 * This method transfers data using local buffer. It transfers data from 
 * a disk to a local buffer in memory, and then it transfers data from the 
 * buffer to the target. This is used only if transferTo is disallowed in
 * the configuration file. super.TransferTo does not perform well on Windows 
 * due to a small IO request generated. customShuffleTransfer can control 
 * the size of the IO requests by changing the size of the intermediate 
 * buffer.// w ww . j  a v  a  2  s  .com
 */
@VisibleForTesting
long customShuffleTransfer(WritableByteChannel target, long position) throws IOException {
    long actualCount = this.count - position;
    if (actualCount < 0 || position < 0) {
        throw new IllegalArgumentException(
                "position out of range: " + position + " (expected: 0 - " + (this.count - 1) + ')');
    }
    if (actualCount == 0) {
        return 0L;
    }

    long trans = actualCount;
    int readSize;
    ByteBuffer byteBuffer = ByteBuffer.allocate(this.shuffleBufferSize);

    while (trans > 0L && (readSize = fileChannel.read(byteBuffer, this.position + position)) > 0) {
        //adjust counters and buffer limit
        if (readSize < trans) {
            trans -= readSize;
            position += readSize;
            byteBuffer.flip();
        } else {
            //We can read more than we need if the actualCount is not multiple 
            //of the byteBuffer size and file is big enough. In that case we cannot
            //use flip method but we need to set buffer limit manually to trans.
            byteBuffer.limit((int) trans);
            byteBuffer.position(0);
            position += trans;
            trans = 0;
        }

        //write data to the target
        while (byteBuffer.hasRemaining()) {
            target.write(byteBuffer);
        }

        byteBuffer.clear();
    }

    return actualCount - trans;
}

From source file:org.apache.tajo.pullserver.FadvisedFileRegion.java

/**
 * This method transfers data using local buffer. It transfers data from
 * a disk to a local buffer in memory, and then it transfers data from the
 * buffer to the target. This is used only if transferTo is disallowed in
 * the configuration file. super.TransferTo does not perform well on Windows
 * due to a small IO request generated. customShuffleTransfer can control
 * the size of the IO requests by changing the size of the intermediate
 * buffer.//www .j  a va  2s  . c  o m
 */
@VisibleForTesting
long customShuffleTransfer(WritableByteChannel target, long position) throws IOException {
    long actualCount = this.count - position;
    if (actualCount < 0 || position < 0) {
        throw new IllegalArgumentException(
                "position out of range: " + position + " (expected: 0 - " + (this.count - 1) + ')');
    }
    if (actualCount == 0) {
        return 0L;
    }

    long trans = actualCount;
    int readSize;
    ByteBuffer byteBuffer = ByteBuffer.allocate(this.shuffleBufferSize);

    while (trans > 0L && (readSize = fileChannel.read(byteBuffer, this.position + position)) > 0) {
        //adjust counters and buffer limit
        if (readSize < trans) {
            trans -= readSize;
            position += readSize;
            byteBuffer.flip();
        } else {
            //We can read more than we need if the actualCount is not multiple
            //of the byteBuffer size and file is big enough. In that case we cannot
            //use flip method but we need to set buffer limit manually to trans.
            byteBuffer.limit((int) trans);
            byteBuffer.position(0);
            position += trans;
            trans = 0;
        }

        //write data to the target
        while (byteBuffer.hasRemaining()) {
            target.write(byteBuffer);
        }

        byteBuffer.clear();
    }

    return actualCount - trans;
}

From source file:com.taobao.common.store.journal.JournalStore.java

/**
 * //from w w  w  . j  a v  a 2s .com
 * 
 * @throws IOException
 */
private void initLoad() throws IOException {
    log.warn("");
    final String nm = this.name + ".";
    final File dir = new File(this.path);
    this.checkParentDir(dir);
    final File[] fs = dir.listFiles(new FilenameFilter() {
        @Override
        public boolean accept(final File dir, final String n) {
            return n.startsWith(nm) && !n.endsWith(".log");
        }
    });
    if (fs == null || fs.length == 0) {
        return;
    }
    log.warn("");
    final List<Integer> indexList = new LinkedList<Integer>();
    for (final File f : fs) {
        try {
            final String fn = f.getName();
            final int n = Integer.parseInt(fn.substring(nm.length()));
            indexList.add(Integer.valueOf(n));
        } catch (final Exception e) {
            log.error("parse file index error" + f, e);
        }
    }

    Integer[] indices = indexList.toArray(new Integer[indexList.size()]);

    // 
    Arrays.sort(indices);

    for (final Integer n : indices) {
        log.warn("index" + n + "");
        // 
        final Map<BytesKey, OpItem> idx = new HashMap<BytesKey, OpItem>();
        // dataFilelogFile
        final File f = new File(dir, this.name + "." + n);
        final DataFile df = new DataFile(f, n, this.force);
        final LogFile lf = new LogFile(new File(f.getAbsolutePath() + ".log"), n, this.force);
        final long size = lf.getLength() / OpItem.LENGTH;

        for (int i = 0; i < size; ++i) { // 
            final ByteBuffer bf = ByteBuffer.wrap(new byte[OpItem.LENGTH]);
            lf.read(bf, i * OpItem.LENGTH);
            if (bf.hasRemaining()) {
                log.warn("log file error:" + lf + ", index:" + i);
                continue;
            }
            final OpItem op = new OpItem();
            op.parse(bf.array());
            final BytesKey key = new BytesKey(op.key);
            switch (op.op) {
            case OpItem.OP_ADD: // 
                final OpItem o = this.indices.get(key);
                if (null != o) {
                    // UpdateRemove

                    // Remove
                    this.innerRemove(o, key, true);

                    // map
                    this.indices.remove(key);
                    this.lastModifiedMap.remove(key);
                }
                boolean addRefCount = true;
                if (idx.get(key) != null) {
                    // addupdate
                    addRefCount = false;
                }

                idx.put(key, op);

                if (addRefCount) {
                    df.increment();
                }
                break;

            case OpItem.OP_DEL: // 
                idx.remove(key);
                df.decrement();
                break;

            default:
                log.warn("unknow op:" + (int) op.op);
                break;
            }
        }
        if (df.getLength() >= FILE_SIZE && df.isUnUsed()) { // 
            df.delete();
            lf.delete();
            log.warn("");
        } else { // map
            this.dataFiles.put(n, df);
            this.logFiles.put(n, lf);
            if (!df.isUnUsed()) { // 
                this.indices.putAll(idx);
                // ,.
                final long lastModified = lf.lastModified();
                for (final BytesKey key : idx.keySet()) {
                    this.lastModifiedMap.put(key, lastModified);
                }
                log.warn("referenceCount:" + df.getReferenceCount() + ", index:"
                        + idx.size());
            }
        }
    }
    // 
    if (this.dataFiles.size() > 0) {
        indices = this.dataFiles.keySet().toArray(new Integer[this.dataFiles.keySet().size()]);
        Arrays.sort(indices);
        for (int i = 0; i < indices.length - 1; i++) {
            final DataFile df = this.dataFiles.get(indices[i]);
            if (df.isUnUsed() || df.getLength() < FILE_SIZE) {
                throw new IllegalStateException("used");
            }
        }
        final Integer n = indices[indices.length - 1];
        this.number.set(n.intValue());
        this.dataFile = this.dataFiles.get(n);
        this.logFile = this.logFiles.get(n);
    }
    log.warn("" + this.size());
}

From source file:org.eclipse.packagedrone.utils.rpm.build.PayloadRecorder.java

public Result addFile(final String targetPath, final ByteBuffer data,
        final Consumer<CpioArchiveEntry> customizer) throws IOException {
    final long size = data.remaining();

    final CpioArchiveEntry entry = new CpioArchiveEntry(targetPath);
    entry.setSize(size);/*from  www  .  j  a va2s .  c  o  m*/

    if (customizer != null) {
        customizer.accept(entry);
    }

    this.archiveStream.putArchiveEntry(entry);

    // record digest

    MessageDigest digest;
    try {
        digest = createDigest();
        digest.update(data.slice());
    } catch (final NoSuchAlgorithmException e) {
        throw new IOException(e);
    }

    // write data

    final WritableByteChannel channel = Channels.newChannel(this.archiveStream);
    while (data.hasRemaining()) {
        channel.write(data);
    }

    // close archive entry

    this.archiveStream.closeArchiveEntry();

    return new Result(size, digest.digest());
}

From source file:io.druid.hll.HyperLogLogCollectorTest.java

@Test
public void testFoldWithUpperNibbleTriggersOffsetChange() throws Exception {
    byte[] arr1 = new byte[HyperLogLogCollector.getLatestNumBytesForDenseStorage()];
    Arrays.fill(arr1, (byte) 0x11);
    ByteBuffer buffer1 = ByteBuffer.wrap(arr1);
    buffer1.put(0, HLLCV1.VERSION);/*from  w  w w  . j  av a  2s . com*/
    buffer1.put(1, (byte) 0);
    buffer1.putShort(2, (short) (2047));
    buffer1.put(HLLCV1.HEADER_NUM_BYTES, (byte) 0x1);

    byte[] arr2 = new byte[HyperLogLogCollector.getLatestNumBytesForDenseStorage()];
    Arrays.fill(arr2, (byte) 0x11);
    ByteBuffer buffer2 = ByteBuffer.wrap(arr2);
    buffer2.put(0, HLLCV1.VERSION);
    buffer2.put(1, (byte) 0);
    buffer2.putShort(2, (short) (2048));

    HyperLogLogCollector collector = HyperLogLogCollector.makeCollector(buffer1);
    collector.fold(buffer2);

    ByteBuffer outBuffer = collector.toByteBuffer();

    Assert.assertEquals(outBuffer.get(), HLLCV1.VERSION);
    Assert.assertEquals(outBuffer.get(), 1);
    Assert.assertEquals(outBuffer.getShort(), 0);
    outBuffer.get();
    outBuffer.getShort();
    Assert.assertFalse(outBuffer.hasRemaining());
}

From source file:io.druid.hll.HyperLogLogCollectorTest.java

@Test
public void testFoldWithDifferentOffsets2() throws Exception {
    ByteBuffer biggerOffset = makeCollectorBuffer(1, (byte) 0x01, 0x11);
    ByteBuffer smallerOffset = makeCollectorBuffer(0, (byte) 0x20, 0x00);

    HyperLogLogCollector collector = HyperLogLogCollector.makeLatestCollector();
    collector.fold(biggerOffset);//from  www .  j  a v a  2  s  .c  o  m
    collector.fold(smallerOffset);

    ByteBuffer outBuffer = collector.toByteBuffer();

    Assert.assertEquals(outBuffer.get(), collector.getVersion());
    Assert.assertEquals(outBuffer.get(), 2);
    Assert.assertEquals(outBuffer.getShort(), 0);
    outBuffer.get();
    outBuffer.getShort();
    Assert.assertFalse(outBuffer.hasRemaining());

    collector = HyperLogLogCollector.makeLatestCollector();
    collector.fold(smallerOffset);
    collector.fold(biggerOffset);

    outBuffer = collector.toByteBuffer();

    Assert.assertEquals(outBuffer.get(), collector.getVersion());
    Assert.assertEquals(outBuffer.get(), 2);
    Assert.assertEquals(outBuffer.getShort(), 0);
    outBuffer.get();
    outBuffer.getShort();
    Assert.assertFalse(outBuffer.hasRemaining());
}

From source file:org.apache.druid.hll.HyperLogLogCollectorTest.java

@Test
public void testFoldWithDifferentOffsets2() {
    ByteBuffer biggerOffset = makeCollectorBuffer(1, (byte) 0x01, 0x11);
    ByteBuffer smallerOffset = makeCollectorBuffer(0, (byte) 0x20, 0x00);

    HyperLogLogCollector collector = HyperLogLogCollector.makeLatestCollector();
    collector.fold(biggerOffset);/*w w w. j  a v  a 2 s  .  c  o m*/
    collector.fold(smallerOffset);

    ByteBuffer outBuffer = collector.toByteBuffer();

    Assert.assertEquals(outBuffer.get(), collector.getVersion());
    Assert.assertEquals(outBuffer.get(), 2);
    Assert.assertEquals(outBuffer.getShort(), 0);
    outBuffer.get();
    outBuffer.getShort();
    Assert.assertFalse(outBuffer.hasRemaining());

    collector = HyperLogLogCollector.makeLatestCollector();
    collector.fold(smallerOffset);
    collector.fold(biggerOffset);

    outBuffer = collector.toByteBuffer();

    Assert.assertEquals(outBuffer.get(), collector.getVersion());
    Assert.assertEquals(outBuffer.get(), 2);
    Assert.assertEquals(outBuffer.getShort(), 0);
    outBuffer.get();
    outBuffer.getShort();
    Assert.assertFalse(outBuffer.hasRemaining());
}