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
     68     protected byte[] protectedArray() {
     69         throw new ReadOnlyBufferException();
     70     }
     71 
     72     @Override
     73     protected int protectedArrayOffset() {
     74         throw new ReadOnlyBufferException();
     75     }
     76 
     77     @Override
     78     protected boolean protectedHasArray() {
     79         return false;
     80     }
     81 
     82     @Override
     83     public ByteBuffer put(byte b) {
     84         throw new ReadOnlyBufferException();
     85     }
     86 
     87     @Override
     88     public ByteBuffer put(int index, byte b) {
     89         throw new ReadOnlyBufferException();
     90     }
     91 
     92     @Override
     93     public ByteBuffer put(byte[] src, int srcOffset, int byteCount) {
     94         throw new ReadOnlyBufferException();
     95     }
     96 
     97     @Override
     98     public ByteBuffer putDouble(double value) {
     99         throw new ReadOnlyBufferException();
    100     }
    101 
    102     @Override
    103     public ByteBuffer putDouble(int index, double value) {
    104         throw new ReadOnlyBufferException();
    105     }
    106 
    107     @Override
    108     public ByteBuffer putFloat(float value) {
    109         throw new ReadOnlyBufferException();
    110     }
    111 
    112     @Override
    113     public ByteBuffer putFloat(int index, float value) {
    114         throw new ReadOnlyBufferException();
    115     }
    116 
    117     @Override
    118     public ByteBuffer putInt(int value) {
    119         throw new ReadOnlyBufferException();
    120     }
    121 
    122     @Override
    123     public ByteBuffer putInt(int index, int value) {
    124         throw new ReadOnlyBufferException();
    125     }
    126 
    127     @Override
    128     public ByteBuffer putLong(int index, long value) {
    129         throw new ReadOnlyBufferException();
    130     }
    131 
    132     @Override
    133     public ByteBuffer putLong(long value) {
    134         throw new ReadOnlyBufferException();
    135     }
    136 
    137     @Override
    138     public ByteBuffer putShort(int index, short value) {
    139         throw new ReadOnlyBufferException();
    140     }
    141 
    142     @Override
    143     public ByteBuffer putShort(short value) {
    144         throw new ReadOnlyBufferException();
    145     }
    146 
    147     @Override
    148     public ByteBuffer put(ByteBuffer buf) {
    149         throw new ReadOnlyBufferException();
    150     }
    151 
    152     @Override
    153     public ByteBuffer slice() {
    154         return new ReadOnlyHeapByteBuffer(backingArray, remaining(), offset + position);
    155     }
    156 }
    157