下面列出了java.nio.IntBuffer#hasRemaining ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
}
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 + "、");
}
}
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 + "、");
}
}
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;
}
}
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;
}
}
}
}
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();
}
}
}
@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;
}
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;
}
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);
}
}
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();
}
private void handleIndexResponse(ByteBuffer readBuffer) throws IOException {
IntBuffer intBuffer = readBuffer.asIntBuffer();
synchronized (Index.bucketCount) {
while (intBuffer.hasRemaining()) {
Index.bucketCount[writeIndexPtr++] += intBuffer.get();
}
}
}
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);
}
}
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); // 修改,错误
}
@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();
}
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;
}
}
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);
}
public static Set<Integer> socketIndexSet(final IntBuffer buffer) {
final Set<Integer> set = new HashSet<Integer>();
while (buffer.hasRemaining()) {
set.add(buffer.get());
}
return set;
}
@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;
}
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);
}
}
}
}
}
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();
}