src/java.base/share/classes/java/nio/Heap-X-Buffer.java.template
changeset 54107 1d7aec80147a
parent 53959 1542e63eb537
child 54129 deb99f13c6cd
equal deleted inserted replaced
54106:9a90236ab64c 54107:1d7aec80147a
   102         this.isReadOnly = true;
   102         this.isReadOnly = true;
   103 #end[rw]
   103 #end[rw]
   104     }
   104     }
   105 
   105 
   106     public $Type$Buffer slice() {
   106     public $Type$Buffer slice() {
       
   107         int rem = this.remaining();
   107         return new Heap$Type$Buffer$RW$(hb,
   108         return new Heap$Type$Buffer$RW$(hb,
   108                                         -1,
   109                                         -1,
   109                                         0,
   110                                         0,
   110                                         this.remaining(),
   111                                         rem,
   111                                         this.remaining(),
   112                                         rem,
   112                                         this.position() + offset);
   113                                         this.position() + offset);
   113     }
   114     }
   114 
   115 
   115     @Override
   116     @Override
   116     public $Type$Buffer slice(int index, int length) {
   117     public $Type$Buffer slice(int index, int length) {
   172     }
   173     }
   173 #end[streamableType]
   174 #end[streamableType]
   174 
   175 
   175     public $Type$Buffer get($type$[] dst, int offset, int length) {
   176     public $Type$Buffer get($type$[] dst, int offset, int length) {
   176         checkBounds(offset, length, dst.length);
   177         checkBounds(offset, length, dst.length);
   177         if (length > remaining())
   178         int pos = position();
       
   179         if (length > limit() - pos)
   178             throw new BufferUnderflowException();
   180             throw new BufferUnderflowException();
   179         System.arraycopy(hb, ix(position()), dst, offset, length);
   181         System.arraycopy(hb, ix(pos), dst, offset, length);
   180         position(position() + length);
   182         position(pos + length);
   181         return this;
   183         return this;
   182     }
   184     }
   183 
   185 
   184     public $Type$Buffer get(int index, $type$[] dst, int offset, int length) {
   186     public $Type$Buffer get(int index, $type$[] dst, int offset, int length) {
   185         Objects.checkFromIndexSize(index, length, limit());
   187         Objects.checkFromIndexSize(index, length, limit());
   217     }
   219     }
   218 
   220 
   219     public $Type$Buffer put($type$[] src, int offset, int length) {
   221     public $Type$Buffer put($type$[] src, int offset, int length) {
   220 #if[rw]
   222 #if[rw]
   221         checkBounds(offset, length, src.length);
   223         checkBounds(offset, length, src.length);
   222         if (length > remaining())
   224         int pos = position();
       
   225         if (length > limit() - pos)
   223             throw new BufferOverflowException();
   226             throw new BufferOverflowException();
   224         System.arraycopy(src, offset, hb, ix(position()), length);
   227         System.arraycopy(src, offset, hb, ix(pos), length);
   225         position(position() + length);
   228         position(pos + length);
   226         return this;
   229         return this;
   227 #else[rw]
   230 #else[rw]
   228         throw new ReadOnlyBufferException();
   231         throw new ReadOnlyBufferException();
   229 #end[rw]
   232 #end[rw]
   230     }
   233     }
   233 #if[rw]
   236 #if[rw]
   234         if (src instanceof Heap$Type$Buffer) {
   237         if (src instanceof Heap$Type$Buffer) {
   235             if (src == this)
   238             if (src == this)
   236                 throw createSameBufferException();
   239                 throw createSameBufferException();
   237             Heap$Type$Buffer sb = (Heap$Type$Buffer)src;
   240             Heap$Type$Buffer sb = (Heap$Type$Buffer)src;
   238             int n = sb.remaining();
   241             int pos = position();
   239             if (n > remaining())
   242             int sbpos = sb.position();
       
   243             int n = sb.limit() - sbpos;
       
   244             if (n > limit() - pos)
   240                 throw new BufferOverflowException();
   245                 throw new BufferOverflowException();
   241             System.arraycopy(sb.hb, sb.ix(sb.position()),
   246             System.arraycopy(sb.hb, sb.ix(sbpos),
   242                              hb, ix(position()), n);
   247                              hb, ix(pos), n);
   243             sb.position(sb.position() + n);
   248             sb.position(sbpos + n);
   244             position(position() + n);
   249             position(pos + n);
   245         } else if (src.isDirect()) {
   250         } else if (src.isDirect()) {
   246             int n = src.remaining();
   251             int n = src.remaining();
   247             if (n > remaining())
   252             int pos = position();
       
   253             if (n > limit() - pos)
   248                 throw new BufferOverflowException();
   254                 throw new BufferOverflowException();
   249             src.get(hb, ix(position()), n);
   255             src.get(hb, ix(pos), n);
   250             position(position() + n);
   256             position(pos + n);
   251         } else {
   257         } else {
   252             super.put(src);
   258             super.put(src);
   253         }
   259         }
   254         return this;
   260         return this;
   255 #else[rw]
   261 #else[rw]
   287 
   293 
   288 #end[char]
   294 #end[char]
   289 
   295 
   290     public $Type$Buffer compact() {
   296     public $Type$Buffer compact() {
   291 #if[rw]
   297 #if[rw]
   292         System.arraycopy(hb, ix(position()), hb, ix(0), remaining());
   298         int pos = position();
   293         position(remaining());
   299         int rem = limit() - pos;
       
   300         System.arraycopy(hb, ix(pos), hb, ix(0), rem);
       
   301         position(rem);
   294         limit(capacity());
   302         limit(capacity());
   295         discardMark();
   303         discardMark();
   296         return this;
   304         return this;
   297 #else[rw]
   305 #else[rw]
   298         throw new ReadOnlyBufferException();
   306         throw new ReadOnlyBufferException();
   346         throw new ReadOnlyBufferException();
   354         throw new ReadOnlyBufferException();
   347 #end[rw]
   355 #end[rw]
   348     }
   356     }
   349 
   357 
   350     public CharBuffer asCharBuffer() {
   358     public CharBuffer asCharBuffer() {
   351         int size = this.remaining() >> 1;
   359         int pos = position();
   352         long addr = address + position();
   360         int size = (limit() - pos) >> 1;
       
   361         long addr = address + pos;
   353         return (bigEndian
   362         return (bigEndian
   354                 ? (CharBuffer)(new ByteBufferAsCharBuffer$RW$B(this,
   363                 ? (CharBuffer)(new ByteBufferAsCharBuffer$RW$B(this,
   355                                                                -1,
   364                                                                -1,
   356                                                                0,
   365                                                                0,
   357                                                                size,
   366                                                                size,
   397         throw new ReadOnlyBufferException();
   406         throw new ReadOnlyBufferException();
   398 #end[rw]
   407 #end[rw]
   399     }
   408     }
   400 
   409 
   401     public ShortBuffer asShortBuffer() {
   410     public ShortBuffer asShortBuffer() {
   402         int size = this.remaining() >> 1;
   411         int pos = position();
   403         long addr = address + position();
   412         int size = (limit() - pos) >> 1;
       
   413         long addr = address + pos;
   404         return (bigEndian
   414         return (bigEndian
   405                 ? (ShortBuffer)(new ByteBufferAsShortBuffer$RW$B(this,
   415                 ? (ShortBuffer)(new ByteBufferAsShortBuffer$RW$B(this,
   406                                                                  -1,
   416                                                                  -1,
   407                                                                  0,
   417                                                                  0,
   408                                                                  size,
   418                                                                  size,
   448         throw new ReadOnlyBufferException();
   458         throw new ReadOnlyBufferException();
   449 #end[rw]
   459 #end[rw]
   450     }
   460     }
   451 
   461 
   452     public IntBuffer asIntBuffer() {
   462     public IntBuffer asIntBuffer() {
   453         int size = this.remaining() >> 2;
   463         int pos = position();
   454         long addr = address + position();
   464         int size = (limit() - pos) >> 2;
       
   465         long addr = address + pos;
   455         return (bigEndian
   466         return (bigEndian
   456                 ? (IntBuffer)(new ByteBufferAsIntBuffer$RW$B(this,
   467                 ? (IntBuffer)(new ByteBufferAsIntBuffer$RW$B(this,
   457                                                              -1,
   468                                                              -1,
   458                                                              0,
   469                                                              0,
   459                                                              size,
   470                                                              size,
   499         throw new ReadOnlyBufferException();
   510         throw new ReadOnlyBufferException();
   500 #end[rw]
   511 #end[rw]
   501     }
   512     }
   502 
   513 
   503     public LongBuffer asLongBuffer() {
   514     public LongBuffer asLongBuffer() {
   504         int size = this.remaining() >> 3;
   515         int pos = position();
   505         long addr = address + position();
   516         int size = (limit() - pos) >> 3;
       
   517         long addr = address + pos;
   506         return (bigEndian
   518         return (bigEndian
   507                 ? (LongBuffer)(new ByteBufferAsLongBuffer$RW$B(this,
   519                 ? (LongBuffer)(new ByteBufferAsLongBuffer$RW$B(this,
   508                                                                -1,
   520                                                                -1,
   509                                                                0,
   521                                                                0,
   510                                                                size,
   522                                                                size,
   554         throw new ReadOnlyBufferException();
   566         throw new ReadOnlyBufferException();
   555 #end[rw]
   567 #end[rw]
   556     }
   568     }
   557 
   569 
   558     public FloatBuffer asFloatBuffer() {
   570     public FloatBuffer asFloatBuffer() {
   559         int size = this.remaining() >> 2;
   571         int pos = position();
   560         long addr = address + position();
   572         int size = (limit() - pos) >> 2;
       
   573         long addr = address + pos;
   561         return (bigEndian
   574         return (bigEndian
   562                 ? (FloatBuffer)(new ByteBufferAsFloatBuffer$RW$B(this,
   575                 ? (FloatBuffer)(new ByteBufferAsFloatBuffer$RW$B(this,
   563                                                                  -1,
   576                                                                  -1,
   564                                                                  0,
   577                                                                  0,
   565                                                                  size,
   578                                                                  size,
   609         throw new ReadOnlyBufferException();
   622         throw new ReadOnlyBufferException();
   610 #end[rw]
   623 #end[rw]
   611     }
   624     }
   612 
   625 
   613     public DoubleBuffer asDoubleBuffer() {
   626     public DoubleBuffer asDoubleBuffer() {
   614         int size = this.remaining() >> 3;
   627         int pos = position();
   615         long addr = address + position();
   628         int size = (limit() - pos) >> 3;
       
   629         long addr = address + pos;
   616         return (bigEndian
   630         return (bigEndian
   617                 ? (DoubleBuffer)(new ByteBufferAsDoubleBuffer$RW$B(this,
   631                 ? (DoubleBuffer)(new ByteBufferAsDoubleBuffer$RW$B(this,
   618                                                                    -1,
   632                                                                    -1,
   619                                                                    0,
   633                                                                    0,
   620                                                                    size,
   634                                                                    size,