Java Code Examples for java.nio.IntBuffer#hasRemaining()

The following examples show how to use java.nio.IntBuffer#hasRemaining() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OffHeapHashMap.java    From offheap-store with Apache License 2.0 6 votes vote down vote up
@Override
public Integer getSlotForHashAndEncoding(int hash, long encoding, long mask) {
  IntBuffer view = (IntBuffer) hashtable.duplicate().position(indexFor(spread(hash)));

  int limit = reprobeLimit();

  for (int i = 0; i < limit; i++) {
    if (!view.hasRemaining()) {
      view.rewind();
    }

    IntBuffer entry = (IntBuffer) view.slice().limit(ENTRY_SIZE);

    if (isTerminating(entry)) {
      return null;
    } else if (isPresent(entry) && (hash == entry.get(KEY_HASHCODE)) && ((encoding & mask) == (readLong(entry, ENCODING) & mask))) {
      return view.position();
    } else {
      view.position(view.position() + ENTRY_SIZE);
    }
  }

  return null;
}
 
Example 2
Source File: OffHeapHashMap.java    From offheap-store with Apache License 2.0 6 votes vote down vote up
private void wipePendingTables() {
  if (hasUsedIterators) {
    pendingTableFrees.reap();

    int[] zeros = new int[1024 >> 2];

    Iterator<PendingPage> it = pendingTableFrees.values();
    while (it.hasNext()) {
      PendingPage pending = it.next();

      IntBuffer pendingTable = pending.tablePage.asIntBuffer();

      pendingTable.clear();
      while (pendingTable.hasRemaining()) {
        if (pendingTable.remaining() < zeros.length) {
          pendingTable.put(zeros, 0, pendingTable.remaining());
        } else {
          pendingTable.put(zeros);
        }
      }
      pendingTable.clear();
    }
  }
}
 
Example 3
Source File: OffHeapHashMap.java    From offheap-store with Apache License 2.0 6 votes vote down vote up
HashIterator() {
  hasUsedIterators = true;
  table = hashtable;
  tableView = (IntBuffer) table.asReadOnlyBuffer().clear();
  expectedModCount = modCount;

  if (size > 0) { // advance to first entry
    while (tableView.hasRemaining()) {
      IntBuffer entry = (IntBuffer) tableView.slice().limit(ENTRY_SIZE);
      tableView.position(tableView.position() + ENTRY_SIZE);

      if (isPresent(entry)) {
        next = create(entry);
        break;
      }
    }
  }
}
 
Example 4
Source File: IntBufferDemo.java    From java-core-learning-example with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    // 创建1字节大小的字节缓冲区
    ByteBuffer bb = ByteBuffer.allocate(BSIZE);
    // int视图缓冲区
    IntBuffer ib = bb.asIntBuffer();
    // 存储一个数组
    ib.put(new int[]{1, 2, 3, 4, 5, 6});
    // 通过访问ByteBuff字节缓冲区,获取某个位置的值
    System.out.println(ib.get(3));
    // 存储一个int数据
    ib.put(3, 44);
    // 反转缓冲区
    ib.flip();
    // 如果当前位置还有元素
    while (ib.hasRemaining()) {
        // 获取当前位置的元素
        int i = ib.get();
        System.out.println(i);
    }
}
 
Example 5
Source File: IntBufferDemo01.java    From javacore with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
public static void main(String[] args) {
    IntBuffer buf = IntBuffer.allocate(10); // 准备出10个大小的缓冲区
    System.out.print("1、写入数据之前的position、limit和capacity:");
    System.out.println("position = " + buf.position() + ",limit = " + buf.limit() + ",capacty = " + buf.capacity());
    int[] temp = { 5, 7, 9 };// 定义一个int数组
    buf.put(3); // 设置一个数据
    buf.put(temp); // 此时已经存放了四个记录
    System.out.print("2、写入数据之后的position、limit和capacity:");
    System.out.println("position = " + buf.position() + ",limit = " + buf.limit() + ",capacty = " + buf.capacity());

    buf.flip(); // 重设缓冲区
    // postion = 0 ,limit = 原本position
    System.out.print("3、准备输出数据时的position、limit和capacity:");
    System.out.println("position = " + buf.position() + ",limit = " + buf.limit() + ",capacty = " + buf.capacity());
    System.out.print("缓冲区中的内容:");
    while (buf.hasRemaining()) {
        int x = buf.get();
        System.out.print(x + "、");
    }
}
 
Example 6
Source File: IntBufferDemo02.java    From javacore with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
public static void main(String[] args) {
    IntBuffer buf = IntBuffer.allocate(10); // 准备出10个大小的缓冲区
    IntBuffer sub = null; // 定义子缓冲区
    for (int i = 0; i < 10; i++) {
        buf.put(2 * i + 1); // 在主缓冲区中加入10个奇数
    }

    // 需要通过slice() 创建子缓冲区
    buf.position(2);
    buf.limit(6);
    sub = buf.slice();
    for (int i = 0; i < sub.capacity(); i++) {
        int temp = sub.get(i);
        sub.put(temp - 1);
    }

    buf.flip(); // 重设缓冲区
    buf.limit(buf.capacity());
    System.out.print("主缓冲区中的内容:");
    while (buf.hasRemaining()) {
        int x = buf.get();
        System.out.print(x + "、");
    }
}
 
Example 7
Source File: MiscTools.java    From megabasterd with GNU General Public License v3.0 6 votes vote down vote up
public static byte[] i32a2bin(int[] i32a) {
    ByteBuffer bin_buffer = ByteBuffer.allocate(i32a.length * 4);
    IntBuffer int_buffer = bin_buffer.asIntBuffer();
    int_buffer.put(i32a);

    if (bin_buffer.hasArray()) {
        return bin_buffer.array();
    } else {
        ArrayList<Byte> list = new ArrayList<>();

        while (int_buffer.hasRemaining()) {
            list.add(bin_buffer.get());
        }

        byte[] aux = new byte[list.size()];

        for (int i = 0; i < aux.length; i++) {
            aux[i] = list.get(i);
        }

        return aux;
    }
}
 
Example 8
Source File: DOMOutputCapsule.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void write(IntBuffer value, String name, IntBuffer defVal) throws IOException {
    if (value == null) {
        return;
    }
    if (value.equals(defVal)) {
        return;
    }

    Element el = appendElement(name);
    el.setAttribute("size", String.valueOf(value.limit()));
    StringBuilder buf = new StringBuilder();
    int pos = value.position();
    value.rewind();
    int ctr = 0;
    while (value.hasRemaining()) {
        ctr++;
        buf.append(value.get());
        buf.append(" ");
    }
    if (ctr != value.limit()) {
        throw new IOException("'" + name
            + "' buffer contention resulted in write data consistency.  "
            + ctr + " values written when should have written "
            + value.limit());
    }
    
    if (buf.length() > 0) {
        //remove last space
        buf.setLength(buf.length() - 1);
    }
    value.position(pos);
    el.setAttribute(dataAttributeName, buf.toString());
    currentElement = (Element) el.getParentNode();
}
 
Example 9
Source File: OffHeapHashMap.java    From offheap-store with Apache License 2.0 5 votes vote down vote up
private void updatePendingTables(int hash, long oldEncoding, IntBuffer newEntry) {
  if (hasUsedIterators) {
    pendingTableFrees.reap();

    Iterator<PendingPage> it = pendingTableFrees.values();
    while (it.hasNext()) {
      PendingPage pending = it.next();

      IntBuffer pendingTable = pending.tablePage.asIntBuffer();
      pendingTable.position(indexFor(spread(hash), pendingTable));

      for (int i = 0; i < pending.reprobe; i++) {
        if (!pendingTable.hasRemaining()) {
          pendingTable.rewind();
        }

        IntBuffer entry = (IntBuffer) pendingTable.slice().limit(ENTRY_SIZE);

        if (isTerminating(entry)) {
          break;
        } else if (isPresent(entry) && (hash == entry.get(KEY_HASHCODE)) && (oldEncoding == readLong(entry, ENCODING))) {
          entry.put(newEntry.duplicate());
          break;
        } else {
          pendingTable.position(pendingTable.position() + ENTRY_SIZE);
        }
      }
    }
  }
}
 
Example 10
Source File: OffHeapHashMap.java    From offheap-store with Apache License 2.0 5 votes vote down vote up
@Override
public Long getEncodingForHashAndBinary(int hash, ByteBuffer binaryKey) {
  if (size == 0) {
    return null;
  }

  IntBuffer view = (IntBuffer) hashtable.duplicate().position(indexFor(spread(hash)));

  int limit = reprobeLimit();

  for (int i = 0; i < limit; i++) {
    if (!view.hasRemaining()) {
      view.rewind();
    }

    IntBuffer entry = (IntBuffer) view.slice().limit(ENTRY_SIZE);

    if (isTerminating(entry)) {
      return null;
    } else if (isPresent(entry) && binaryKeyEquals(binaryKey, hash, readLong(entry, ENCODING), entry.get(KEY_HASHCODE))) {
      return readLong(entry, ENCODING);
    } else {
      view.position(view.position() + ENTRY_SIZE);
    }
  }
  return null;
}
 
Example 11
Source File: UnitHelp.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
public static Set<Integer> socketIndexSet(final IntBuffer buffer) {
    final Set<Integer> set = new HashSet<Integer>();
    while (buffer.hasRemaining()) {
        set.add(buffer.get());
    }
    return set;
}
 
Example 12
Source File: TestUtils.java    From succinct with Apache License 2.0 5 votes vote down vote up
public static FSDataInputStream getStream(IntBuffer buf) throws IOException {
  File tmpDir = Files.createTempDir();
  Path filePath = new Path(tmpDir.getAbsolutePath() + "/testOut");
  FileSystem fs = FileSystem.get(filePath.toUri(), new Configuration());
  FSDataOutputStream fOut = fs.create(filePath);
  buf.rewind();
  while (buf.hasRemaining()) {
    fOut.writeInt(buf.get());
  }
  fOut.close();
  buf.rewind();
  return fs.open(filePath);
}
 
Example 13
Source File: MiscTools.java    From megabasterd with GNU General Public License v3.0 5 votes vote down vote up
public static int[] bin2i32a(byte[] bin) {
    int l = (int) (4 * Math.ceil((double) bin.length / 4));

    byte[] new_bin = Arrays.copyOfRange(bin, 0, l);

    bin = new_bin;

    ByteBuffer bin_buffer = ByteBuffer.wrap(bin);
    IntBuffer int_buffer = bin_buffer.asIntBuffer();

    if (int_buffer.hasArray()) {
        return int_buffer.array();
    } else {
        ArrayList<Integer> list = new ArrayList<>();

        while (int_buffer.hasRemaining()) {
            list.add(int_buffer.get());
        }

        int[] aux = new int[list.size()];

        for (int i = 0; i < aux.length; i++) {
            aux[i] = list.get(i);
        }

        return aux;
    }
}
 
Example 14
Source File: IntBitPacking.java    From metrics with Apache License 2.0 5 votes vote down vote up
protected void decompress(
        IntBuffer src,
        IntOutputStream dst,
        IntFilter filter)
{
    while (src.hasRemaining()) {
        int head = src.get();
        for (int i = (this.blockNum - 1) * 8; i >= 0; i -= 8) {
            int validBits = (int)((head >> i) & 0xff);
            unpack(src, dst, validBits, this.blockLen, filter);
        }
    }
    return;
}
 
Example 15
Source File: IntBufferDemo03.java    From javacore with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
public static void main(String[] args) {
    IntBuffer buf = IntBuffer.allocate(10); // 准备出10个大小的缓冲区
    IntBuffer read = null; // 定义子缓冲区
    for (int i = 0; i < 10; i++) {
        buf.put(2 * i + 1); // 在主缓冲区中加入10个奇数
    }
    read = buf.asReadOnlyBuffer();// 创建只读缓冲区
    read.flip(); // 重设缓冲区
    System.out.print("主缓冲区中的内容:");
    while (read.hasRemaining()) {
        int x = read.get();
        System.out.print(x + "、");
    }
    read.put(30); // 修改,错误
}
 
Example 16
Source File: IntBufferDemo.java    From LearningOfThinkInJava with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    ByteBuffer bb=ByteBuffer.allocate(BSIZE);
    IntBuffer ib=bb.asIntBuffer();
    ib.put(new int[]{11,42,47,99,143,811,1016});
    System.out.println(ib.getClass().getName());
    System.out.println(ib.get(3));
    ib.put(3,1811);
    ib.flip();
    while (ib.hasRemaining()){
        int i=ib.get();
        System.out.println(i);
    }


}
 
Example 17
Source File: TopknMaster.java    From coding-snippets with MIT License 5 votes vote down vote up
private void handleIndexResponse(ByteBuffer readBuffer) throws IOException {
    IntBuffer intBuffer = readBuffer.asIntBuffer();
    synchronized (Index.bucketCount) {
        while (intBuffer.hasRemaining()) {
            Index.bucketCount[writeIndexPtr++] += intBuffer.get();
        }
    }
}
 
Example 18
Source File: DOMOutputCapsule.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void write(IntBuffer value, String name, IntBuffer defVal) throws IOException {
    if (value == null) {
        return;
    }
    if (value.equals(defVal)) {
        return;
    }

    Element el = appendElement(name);
    el.setAttribute("size", String.valueOf(value.limit()));
    StringBuilder buf = new StringBuilder();
    int pos = value.position();
    value.rewind();
    int ctr = 0;
    while (value.hasRemaining()) {
        ctr++;
        buf.append(value.get());
        buf.append(" ");
    }
    if (ctr != value.limit())
        throw new IOException("'" + name
            + "' buffer contention resulted in write data consistency.  "
            + ctr + " values written when should have written "
            + value.limit());
    buf.setLength(buf.length() - 1);
    value.position(pos);
    el.setAttribute(dataAttributeName, buf.toString());
    currentElement = (Element) el.getParentNode();
}
 
Example 19
Source File: CodecUtils.java    From metrics with Apache License 2.0 5 votes vote down vote up
public static void decodeBlockPack(
        IntBuffer src,
        IntFilterFactory filterFactory,
        IntBitPacking packer,
        IntOutputStream dst)
{
    // Fetch length of original array.
    if (!src.hasRemaining()) {
        return;
    }
    final int outLen = (int)src.get() - 1;

    // Fetch and output first int, and set it as delta's initial context.
    final int first = src.get();
    dst.write(first);
    IntFilter filter = filterFactory.newFilter(first);

    // Decompress intermediate blocks.
    final int chunkSize = packer.getBlockSize();
    final int chunkNum = outLen / chunkSize;
    if (chunkNum > 0) {
        packer.decompress(src, dst, filter, chunkNum);
    }

    // Decompress last block.
    final int chunkRemain = outLen % chunkSize;
    if (chunkRemain > 0) {
        int[] last = new int[chunkSize];
        IntBuffer buf = IntBuffer.wrap(last);
        packer.decompress(src, new IntBufferOutputStream(buf),
                filter, 1);
        dst.write(last, 0, chunkRemain);
    }
}
 
Example 20
Source File: ViewBuffers.java    From java-core-learning-example with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[]{0,0,0,0,0,0,0,'a'});
    bb.rewind();
    System.out.print("Byte Buffer ");
    while (bb.hasRemaining())
        System.out.print(bb.position() + " -> " + bb.get() + ", ");
    System.out.println();

    CharBuffer cb = ((ByteBuffer)bb.rewind()).asCharBuffer();
    System.out.print("Char Buffer ");
    while (cb.hasRemaining())
        System.out.print(cb.position() + " -> " + cb.get() + ", ");
    System.out.println();

    ShortBuffer sb = ((ByteBuffer)bb.rewind()).asShortBuffer();
    System.out.print("Short Buffer ");
    while (sb.hasRemaining())
        System.out.print(sb.position() + " -> " + sb.get() + ", ");
    System.out.println();

    IntBuffer ib = ((ByteBuffer)bb.rewind()).asIntBuffer();
    System.out.print("Int Buffer ");
    while (ib.hasRemaining())
        System.out.print(ib.position() + " -> " + ib.get());
    System.out.println();

    FloatBuffer fb = ((ByteBuffer)bb.rewind()).asFloatBuffer();
    System.out.print("Float Buffer ");
    while (fb.hasRemaining())
        System.out.print(fb.position() + " -> " + fb.get() + ", ");
    System.out.println();

    LongBuffer lb = ((ByteBuffer)bb.rewind()).asLongBuffer();
    System.out.print("Long Buffer ");
    while (lb.hasRemaining())
        System.out.print(lb.position() + " -> " + lb.get() + ", ");
    System.out.println();

    DoubleBuffer db = ((ByteBuffer)bb.rewind()).asDoubleBuffer();
    System.out.print("Double Buffer ");
    while (db.hasRemaining())
        System.out.print(db.position() + " -> " + db.get() + ", ");
    System.out.println();
}