< prev index next >
src/java.base/share/classes/java/nio/Heap-X-Buffer.java.template
Print this page
rev 52906 : 8219597: (bf) Heap buffer state changes could provoke unexpected exceptions
Reviewed-by: alanb, rriggs
rev 52907 : 8234841: Enhance buffering of byte buffers
Reviewed-by: alanb, ahgross, rhalade, psandoz
@@ -100,16 +100,19 @@
this.isReadOnly = true;
#end[rw]
}
public $Type$Buffer slice() {
+ int pos = this.position();
+ int lim = this.limit();
+ int rem = (pos <= lim ? lim - pos : 0);
return new Heap$Type$Buffer$RW$(hb,
-1,
0,
- this.remaining(),
- this.remaining(),
- this.position() + offset);
+ rem,
+ rem,
+ pos + offset);
}
#if[byte]
$Type$Buffer slice(int pos, int lim) {
assert (pos >= 0);
@@ -172,14 +175,15 @@
}
#end[streamableType]
public $Type$Buffer get($type$[] dst, int offset, int length) {
checkBounds(offset, length, dst.length);
- if (length > remaining())
+ int pos = position();
+ if (length > limit() - pos)
throw new BufferUnderflowException();
- System.arraycopy(hb, ix(position()), dst, offset, length);
- position(position() + length);
+ System.arraycopy(hb, ix(pos), dst, offset, length);
+ position(pos + length);
return this;
}
public boolean isDirect() {
return false;
@@ -210,14 +214,15 @@
}
public $Type$Buffer put($type$[] src, int offset, int length) {
#if[rw]
checkBounds(offset, length, src.length);
- if (length > remaining())
+ int pos = position();
+ if (length > limit() - pos)
throw new BufferOverflowException();
- System.arraycopy(src, offset, hb, ix(position()), length);
- position(position() + length);
+ System.arraycopy(src, offset, hb, ix(pos), length);
+ position(pos + length);
return this;
#else[rw]
throw new ReadOnlyBufferException();
#end[rw]
}
@@ -226,23 +231,26 @@
#if[rw]
if (src instanceof Heap$Type$Buffer) {
if (src == this)
throw createSameBufferException();
Heap$Type$Buffer sb = (Heap$Type$Buffer)src;
- int n = sb.remaining();
- if (n > remaining())
+ int pos = position();
+ int sbpos = sb.position();
+ int n = sb.limit() - sbpos;
+ if (n > limit() - pos)
throw new BufferOverflowException();
- System.arraycopy(sb.hb, sb.ix(sb.position()),
- hb, ix(position()), n);
- sb.position(sb.position() + n);
- position(position() + n);
+ System.arraycopy(sb.hb, sb.ix(sbpos),
+ hb, ix(pos), n);
+ sb.position(sbpos + n);
+ position(pos + n);
} else if (src.isDirect()) {
int n = src.remaining();
- if (n > remaining())
+ int pos = position();
+ if (n > limit() - pos)
throw new BufferOverflowException();
- src.get(hb, ix(position()), n);
- position(position() + n);
+ src.get(hb, ix(pos), n);
+ position(pos + n);
} else {
super.put(src);
}
return this;
#else[rw]
@@ -250,12 +258,14 @@
#end[rw]
}
public $Type$Buffer compact() {
#if[rw]
- System.arraycopy(hb, ix(position()), hb, ix(0), remaining());
- position(remaining());
+ int pos = position();
+ int rem = limit() - pos;
+ System.arraycopy(hb, ix(pos), hb, ix(0), rem);
+ position(rem);
limit(capacity());
discardMark();
return this;
#else[rw]
throw new ReadOnlyBufferException();
@@ -309,12 +319,13 @@
throw new ReadOnlyBufferException();
#end[rw]
}
public CharBuffer asCharBuffer() {
- int size = this.remaining() >> 1;
- long addr = address + position();
+ int pos = position();
+ int size = (limit() - pos) >> 1;
+ long addr = address + pos;
return (bigEndian
? (CharBuffer)(new ByteBufferAsCharBuffer$RW$B(this,
-1,
0,
size,
@@ -360,12 +371,13 @@
throw new ReadOnlyBufferException();
#end[rw]
}
public ShortBuffer asShortBuffer() {
- int size = this.remaining() >> 1;
- long addr = address + position();
+ int pos = position();
+ int size = (limit() - pos) >> 1;
+ long addr = address + pos;
return (bigEndian
? (ShortBuffer)(new ByteBufferAsShortBuffer$RW$B(this,
-1,
0,
size,
@@ -411,12 +423,13 @@
throw new ReadOnlyBufferException();
#end[rw]
}
public IntBuffer asIntBuffer() {
- int size = this.remaining() >> 2;
- long addr = address + position();
+ int pos = position();
+ int size = (limit() - pos) >> 2;
+ long addr = address + pos;
return (bigEndian
? (IntBuffer)(new ByteBufferAsIntBuffer$RW$B(this,
-1,
0,
size,
@@ -462,12 +475,13 @@
throw new ReadOnlyBufferException();
#end[rw]
}
public LongBuffer asLongBuffer() {
- int size = this.remaining() >> 3;
- long addr = address + position();
+ int pos = position();
+ int size = (limit() - pos) >> 3;
+ long addr = address + pos;
return (bigEndian
? (LongBuffer)(new ByteBufferAsLongBuffer$RW$B(this,
-1,
0,
size,
@@ -517,12 +531,13 @@
throw new ReadOnlyBufferException();
#end[rw]
}
public FloatBuffer asFloatBuffer() {
- int size = this.remaining() >> 2;
- long addr = address + position();
+ int pos = position();
+ int size = (limit() - pos) >> 2;
+ long addr = address + pos;
return (bigEndian
? (FloatBuffer)(new ByteBufferAsFloatBuffer$RW$B(this,
-1,
0,
size,
@@ -572,12 +587,13 @@
throw new ReadOnlyBufferException();
#end[rw]
}
public DoubleBuffer asDoubleBuffer() {
- int size = this.remaining() >> 3;
- long addr = address + position();
+ int pos = position();
+ int size = (limit() - pos) >> 3;
+ long addr = address + pos;
return (bigEndian
? (DoubleBuffer)(new ByteBufferAsDoubleBuffer$RW$B(this,
-1,
0,
size,
< prev index next >