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 * DirectByteBuffer, ReadWriteDirectByteBuffer and ReadOnlyDirectByteBuffer 22 * compose the implementation of platform memory based byte buffers. 23 * <p> 24 * ReadOnlyDirectByteBuffer extends DirectByteBuffer 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 final class ReadOnlyDirectByteBuffer extends DirectByteBuffer { 32 static ReadOnlyDirectByteBuffer copy(DirectByteBuffer other, int markOfOther) { 33 ReadOnlyDirectByteBuffer buf = new ReadOnlyDirectByteBuffer(other.block, other.capacity(), other.offset); 34 buf.limit = other.limit; 35 buf.position = other.position(); 36 buf.mark = markOfOther; 37 return buf; 38 } 39 40 protected ReadOnlyDirectByteBuffer(MemoryBlock block, int capacity, int offset) { 41 super(block, capacity, offset); 42 } 43 44 @Override 45 public ByteBuffer asReadOnlyBuffer() { 46 return copy(this, mark); 47 } 48 49 @Override 50 public ByteBuffer compact() { 51 throw new ReadOnlyBufferException(); 52 } 53 54 @Override 55 public ByteBuffer duplicate() { 56 return copy(this, mark); 57 } 58 59 @Override 60 public boolean isReadOnly() { 61 return true; 62 } 63 64 @Override 65 public ByteBuffer put(byte value) { 66 throw new ReadOnlyBufferException(); 67 } 68 69 @Override 70 public ByteBuffer put(int index, byte value) { 71 throw new ReadOnlyBufferException(); 72 } 73 74 @Override 75 public ByteBuffer put(byte[] src, int srcOffset, int byteCount) { 76 throw new ReadOnlyBufferException(); 77 } 78 79 @Override 80 public ByteBuffer putDouble(double value) { 81 throw new ReadOnlyBufferException(); 82 } 83 84 @Override 85 public ByteBuffer putDouble(int index, double value) { 86 throw new ReadOnlyBufferException(); 87 } 88 89 @Override 90 public ByteBuffer putFloat(float value) { 91 throw new ReadOnlyBufferException(); 92 } 93 94 @Override 95 public ByteBuffer putFloat(int index, float value) { 96 throw new ReadOnlyBufferException(); 97 } 98 99 @Override 100 public ByteBuffer putInt(int value) { 101 throw new ReadOnlyBufferException(); 102 } 103 104 @Override 105 public ByteBuffer putInt(int index, int value) { 106 throw new ReadOnlyBufferException(); 107 } 108 109 @Override 110 public ByteBuffer putLong(int index, long value) { 111 throw new ReadOnlyBufferException(); 112 } 113 114 @Override 115 public ByteBuffer putLong(long value) { 116 throw new ReadOnlyBufferException(); 117 } 118 119 @Override 120 public ByteBuffer putShort(int index, short value) { 121 throw new ReadOnlyBufferException(); 122 } 123 124 @Override 125 public ByteBuffer putShort(short value) { 126 throw new ReadOnlyBufferException(); 127 } 128 129 @Override 130 public ByteBuffer put(ByteBuffer buf) { 131 throw new ReadOnlyBufferException(); 132 } 133 134 @Override 135 public ByteBuffer slice() { 136 return new ReadOnlyDirectByteBuffer(block, remaining(), offset + position); 137 } 138 139 @Override protected byte[] protectedArray() { 140 throw new ReadOnlyBufferException(); 141 } 142 143 @Override protected int protectedArrayOffset() { 144 throw new ReadOnlyBufferException(); 145 } 146 147 @Override protected boolean protectedHasArray() { 148 return false; 149 } 150 } 151