< 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,115 **** this.isReadOnly = true; #end[rw] } public $Type$Buffer slice() { return new Heap$Type$Buffer$RW$(hb, -1, 0, ! this.remaining(), ! this.remaining(), ! this.position() + offset); } #if[byte] $Type$Buffer slice(int pos, int lim) { assert (pos >= 0); --- 100,118 ---- 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, ! rem, ! rem, ! pos + offset); } #if[byte] $Type$Buffer slice(int pos, int lim) { assert (pos >= 0);
*** 172,185 **** } #end[streamableType] public $Type$Buffer get($type$[] dst, int offset, int length) { checkBounds(offset, length, dst.length); ! if (length > remaining()) throw new BufferUnderflowException(); ! System.arraycopy(hb, ix(position()), dst, offset, length); ! position(position() + length); return this; } public boolean isDirect() { return false; --- 175,189 ---- } #end[streamableType] public $Type$Buffer get($type$[] dst, int offset, int length) { checkBounds(offset, length, dst.length); ! int pos = position(); ! if (length > limit() - pos) throw new BufferUnderflowException(); ! System.arraycopy(hb, ix(pos), dst, offset, length); ! position(pos + length); return this; } public boolean isDirect() { return false;
*** 210,223 **** } public $Type$Buffer put($type$[] src, int offset, int length) { #if[rw] checkBounds(offset, length, src.length); ! if (length > remaining()) throw new BufferOverflowException(); ! System.arraycopy(src, offset, hb, ix(position()), length); ! position(position() + length); return this; #else[rw] throw new ReadOnlyBufferException(); #end[rw] } --- 214,228 ---- } public $Type$Buffer put($type$[] src, int offset, int length) { #if[rw] checkBounds(offset, length, src.length); ! int pos = position(); ! if (length > limit() - pos) throw new BufferOverflowException(); ! System.arraycopy(src, offset, hb, ix(pos), length); ! position(pos + length); return this; #else[rw] throw new ReadOnlyBufferException(); #end[rw] }
*** 226,248 **** #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()) throw new BufferOverflowException(); ! System.arraycopy(sb.hb, sb.ix(sb.position()), ! hb, ix(position()), n); ! sb.position(sb.position() + n); ! position(position() + n); } else if (src.isDirect()) { int n = src.remaining(); ! if (n > remaining()) throw new BufferOverflowException(); ! src.get(hb, ix(position()), n); ! position(position() + n); } else { super.put(src); } return this; #else[rw] --- 231,256 ---- #if[rw] if (src instanceof Heap$Type$Buffer) { if (src == this) throw createSameBufferException(); Heap$Type$Buffer sb = (Heap$Type$Buffer)src; ! 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(sbpos), ! hb, ix(pos), n); ! sb.position(sbpos + n); ! position(pos + n); } else if (src.isDirect()) { int n = src.remaining(); ! int pos = position(); ! if (n > limit() - pos) throw new BufferOverflowException(); ! src.get(hb, ix(pos), n); ! position(pos + n); } else { super.put(src); } return this; #else[rw]
*** 250,261 **** #end[rw] } public $Type$Buffer compact() { #if[rw] ! System.arraycopy(hb, ix(position()), hb, ix(0), remaining()); ! position(remaining()); limit(capacity()); discardMark(); return this; #else[rw] throw new ReadOnlyBufferException(); --- 258,271 ---- #end[rw] } public $Type$Buffer compact() { #if[rw] ! 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,320 **** throw new ReadOnlyBufferException(); #end[rw] } public CharBuffer asCharBuffer() { ! int size = this.remaining() >> 1; ! long addr = address + position(); return (bigEndian ? (CharBuffer)(new ByteBufferAsCharBuffer$RW$B(this, -1, 0, size, --- 319,331 ---- throw new ReadOnlyBufferException(); #end[rw] } public CharBuffer asCharBuffer() { ! int pos = position(); ! int size = (limit() - pos) >> 1; ! long addr = address + pos; return (bigEndian ? (CharBuffer)(new ByteBufferAsCharBuffer$RW$B(this, -1, 0, size,
*** 360,371 **** throw new ReadOnlyBufferException(); #end[rw] } public ShortBuffer asShortBuffer() { ! int size = this.remaining() >> 1; ! long addr = address + position(); return (bigEndian ? (ShortBuffer)(new ByteBufferAsShortBuffer$RW$B(this, -1, 0, size, --- 371,383 ---- throw new ReadOnlyBufferException(); #end[rw] } public ShortBuffer asShortBuffer() { ! int pos = position(); ! int size = (limit() - pos) >> 1; ! long addr = address + pos; return (bigEndian ? (ShortBuffer)(new ByteBufferAsShortBuffer$RW$B(this, -1, 0, size,
*** 411,422 **** throw new ReadOnlyBufferException(); #end[rw] } public IntBuffer asIntBuffer() { ! int size = this.remaining() >> 2; ! long addr = address + position(); return (bigEndian ? (IntBuffer)(new ByteBufferAsIntBuffer$RW$B(this, -1, 0, size, --- 423,435 ---- throw new ReadOnlyBufferException(); #end[rw] } public IntBuffer asIntBuffer() { ! int pos = position(); ! int size = (limit() - pos) >> 2; ! long addr = address + pos; return (bigEndian ? (IntBuffer)(new ByteBufferAsIntBuffer$RW$B(this, -1, 0, size,
*** 462,473 **** throw new ReadOnlyBufferException(); #end[rw] } public LongBuffer asLongBuffer() { ! int size = this.remaining() >> 3; ! long addr = address + position(); return (bigEndian ? (LongBuffer)(new ByteBufferAsLongBuffer$RW$B(this, -1, 0, size, --- 475,487 ---- throw new ReadOnlyBufferException(); #end[rw] } public LongBuffer asLongBuffer() { ! int pos = position(); ! int size = (limit() - pos) >> 3; ! long addr = address + pos; return (bigEndian ? (LongBuffer)(new ByteBufferAsLongBuffer$RW$B(this, -1, 0, size,
*** 517,528 **** throw new ReadOnlyBufferException(); #end[rw] } public FloatBuffer asFloatBuffer() { ! int size = this.remaining() >> 2; ! long addr = address + position(); return (bigEndian ? (FloatBuffer)(new ByteBufferAsFloatBuffer$RW$B(this, -1, 0, size, --- 531,543 ---- throw new ReadOnlyBufferException(); #end[rw] } public FloatBuffer asFloatBuffer() { ! int pos = position(); ! int size = (limit() - pos) >> 2; ! long addr = address + pos; return (bigEndian ? (FloatBuffer)(new ByteBufferAsFloatBuffer$RW$B(this, -1, 0, size,
*** 572,583 **** throw new ReadOnlyBufferException(); #end[rw] } public DoubleBuffer asDoubleBuffer() { ! int size = this.remaining() >> 3; ! long addr = address + position(); return (bigEndian ? (DoubleBuffer)(new ByteBufferAsDoubleBuffer$RW$B(this, -1, 0, size, --- 587,599 ---- throw new ReadOnlyBufferException(); #end[rw] } public DoubleBuffer asDoubleBuffer() { ! 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 >