Home | History | Annotate | Download | only in nio
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 
     18 package java.nio;
     19 
     20 /**
     21  * HeapByteBuffer, ReadWriteHeapByteBuffer and ReadOnlyHeapByteBuffer compose
     22  * the implementation of array based byte buffers.
     23  * <p>
     24  * ReadOnlyHeapByteBuffer extends HeapByteBuffer with all the write methods
     25  * throwing read only exception.
     26  * </p>
     27  * <p>
     28  * This class is marked final for runtime performance.
     29  * </p>
     30  *
     31  */
     32 final class ReadOnlyHeapByteBuffer extends HeapByteBuffer {
     33 
     34     static ReadOnlyHeapByteBuffer copy(HeapByteBuffer other, int markOfOther) {
     35         ReadOnlyHeapByteBuffer buf =
     36                 new ReadOnlyHeapByteBuffer(other.backingArray, other.capacity(), other.offset);
     37         buf.limit = other.limit;
     38         buf.position = other.position();
     39         buf.mark = markOfOther;
     40         return buf;
     41     }
     42 
     43     ReadOnlyHeapByteBuffer(byte[] backingArray, int capacity, int arrayOffset) {
     44         super(backingArray, capacity, arrayOffset);
     45     }
     46 
     47     @Override
     48     public ByteBuffer asReadOnlyBuffer() {
     49         return ReadOnlyHeapByteBuffer.copy(this, mark);
     50     }
     51 
     52     @Override
     53     public ByteBuffer compact() {
     54         throw new ReadOnlyBufferException();
     55     }
     56 
     57     @Override
     58     public ByteBuffer duplicate() {
     59         return copy(this, mark);
     60     }
     61 
     62     @Override
     63     public boolean isReadOnly() {
     64         return true;
     65     }
     66 
     67     @Override byte[] protectedArray() {
     68         throw new ReadOnlyBufferException();
     69     }
     70 
     71     @Override int protectedArrayOffset() {
     72         throw new ReadOnlyBufferException();
     73     }
     74 
     75     @Override boolean protectedHasArray() {
     76         return false;
     77     }
     78 
     79     @Override
     80     public ByteBuffer put(byte b) {
     81         throw new ReadOnlyBufferException();
     82     }
     83 
     84     @Override
     85     public ByteBuffer put(int index, byte b) {
     86         throw new ReadOnlyBufferException();
     87     }
     88 
     89     @Override
     90     public ByteBuffer put(byte[] src, int srcOffset, int byteCount) {
     91         throw new ReadOnlyBufferException();
     92     }
     93 
     94     @Override
     95     public ByteBuffer putDouble(double value) {
     96         throw new ReadOnlyBufferException();
     97     }
     98 
     99     @Override
    100     public ByteBuffer putDouble(int index, double value) {
    101         throw new ReadOnlyBufferException();
    102     }
    103 
    104     @Override
    105     public ByteBuffer putFloat(float value) {
    106         throw new ReadOnlyBufferException();
    107     }
    108 
    109     @Override
    110     public ByteBuffer putFloat(int index, float value) {
    111         throw new ReadOnlyBufferException();
    112     }
    113 
    114     @Override
    115     public ByteBuffer putInt(int value) {
    116         throw new ReadOnlyBufferException();
    117     }
    118 
    119     @Override
    120     public ByteBuffer putInt(int index, int value) {
    121         throw new ReadOnlyBufferException();
    122     }
    123 
    124     @Override
    125     public ByteBuffer putLong(int index, long value) {
    126         throw new ReadOnlyBufferException();
    127     }
    128 
    129     @Override
    130     public ByteBuffer putLong(long value) {
    131         throw new ReadOnlyBufferException();
    132     }
    133 
    134     @Override
    135     public ByteBuffer putShort(int index, short value) {
    136         throw new ReadOnlyBufferException();
    137     }
    138 
    139     @Override
    140     public ByteBuffer putShort(short value) {
    141         throw new ReadOnlyBufferException();
    142     }
    143 
    144     @Override
    145     public ByteBuffer put(ByteBuffer buf) {
    146         throw new ReadOnlyBufferException();
    147     }
    148 
    149     @Override
    150     public ByteBuffer slice() {
    151         return new ReadOnlyHeapByteBuffer(backingArray, remaining(), offset + position);
    152     }
    153 }
    154