1 /* Licensed to the Apache Software Foundation (ASF) under one or more 2 * contributor license agreements. See the NOTICE file distributed with 3 * this work for additional information regarding copyright ownership. 4 * The ASF licenses this file to You under the Apache License, Version 2.0 5 * (the "License"); you may not use this file except in compliance with 6 * the License. You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package java.nio; 18 19 import libcore.io.SizeOf; 20 21 /** 22 * This class wraps a byte buffer to be a long buffer. 23 * <p> 24 * Implementation notice: 25 * <ul> 26 * <li>After a byte buffer instance is wrapped, it becomes privately owned by 27 * the adapter. It must NOT be accessed outside the adapter any more.</li> 28 * <li>The byte buffer's position and limit are NOT linked with the adapter. 29 * The adapter extends Buffer, thus has its own position and limit.</li> 30 * </ul> 31 * </p> 32 * 33 */ 34 final class ByteBufferAsLongBuffer extends LongBuffer { 35 36 private final ByteBuffer byteBuffer; 37 38 static LongBuffer asLongBuffer(ByteBuffer byteBuffer) { 39 ByteBuffer slice = byteBuffer.slice(); 40 slice.order(byteBuffer.order()); 41 return new ByteBufferAsLongBuffer(slice); 42 } 43 44 private ByteBufferAsLongBuffer(ByteBuffer byteBuffer) { 45 super(byteBuffer.capacity() / SizeOf.LONG); 46 this.byteBuffer = byteBuffer; 47 this.byteBuffer.clear(); 48 this.effectiveDirectAddress = byteBuffer.effectiveDirectAddress; 49 } 50 51 @Override 52 public LongBuffer asReadOnlyBuffer() { 53 ByteBufferAsLongBuffer buf = new ByteBufferAsLongBuffer(byteBuffer.asReadOnlyBuffer()); 54 buf.limit = limit; 55 buf.position = position; 56 buf.mark = mark; 57 buf.byteBuffer.order = byteBuffer.order; 58 return buf; 59 } 60 61 @Override 62 public LongBuffer compact() { 63 if (byteBuffer.isReadOnly()) { 64 throw new ReadOnlyBufferException(); 65 } 66 byteBuffer.limit(limit * SizeOf.LONG); 67 byteBuffer.position(position * SizeOf.LONG); 68 byteBuffer.compact(); 69 byteBuffer.clear(); 70 position = limit - position; 71 limit = capacity; 72 mark = UNSET_MARK; 73 return this; 74 } 75 76 @Override 77 public LongBuffer duplicate() { 78 ByteBuffer bb = byteBuffer.duplicate().order(byteBuffer.order()); 79 ByteBufferAsLongBuffer buf = new ByteBufferAsLongBuffer(bb); 80 buf.limit = limit; 81 buf.position = position; 82 buf.mark = mark; 83 return buf; 84 } 85 86 @Override 87 public long get() { 88 if (position == limit) { 89 throw new BufferUnderflowException(); 90 } 91 return byteBuffer.getLong(position++ * SizeOf.LONG); 92 } 93 94 @Override 95 public long get(int index) { 96 checkIndex(index); 97 return byteBuffer.getLong(index * SizeOf.LONG); 98 } 99 100 @Override 101 public LongBuffer get(long[] dst, int dstOffset, int longCount) { 102 byteBuffer.limit(limit * SizeOf.LONG); 103 byteBuffer.position(position * SizeOf.LONG); 104 if (byteBuffer instanceof DirectByteBuffer) { 105 ((DirectByteBuffer) byteBuffer).get(dst, dstOffset, longCount); 106 } else { 107 ((ByteArrayBuffer) byteBuffer).get(dst, dstOffset, longCount); 108 } 109 this.position += longCount; 110 return this; 111 } 112 113 @Override 114 public boolean isDirect() { 115 return byteBuffer.isDirect(); 116 } 117 118 @Override 119 public boolean isReadOnly() { 120 return byteBuffer.isReadOnly(); 121 } 122 123 @Override 124 public ByteOrder order() { 125 return byteBuffer.order(); 126 } 127 128 @Override long[] protectedArray() { 129 throw new UnsupportedOperationException(); 130 } 131 132 @Override int protectedArrayOffset() { 133 throw new UnsupportedOperationException(); 134 } 135 136 @Override boolean protectedHasArray() { 137 return false; 138 } 139 140 @Override 141 public LongBuffer put(long c) { 142 if (position == limit) { 143 throw new BufferOverflowException(); 144 } 145 byteBuffer.putLong(position++ * SizeOf.LONG, c); 146 return this; 147 } 148 149 @Override 150 public LongBuffer put(int index, long c) { 151 checkIndex(index); 152 byteBuffer.putLong(index * SizeOf.LONG, c); 153 return this; 154 } 155 156 @Override 157 public LongBuffer put(long[] src, int srcOffset, int longCount) { 158 byteBuffer.limit(limit * SizeOf.LONG); 159 byteBuffer.position(position * SizeOf.LONG); 160 if (byteBuffer instanceof DirectByteBuffer) { 161 ((DirectByteBuffer) byteBuffer).put(src, srcOffset, longCount); 162 } else { 163 ((ByteArrayBuffer) byteBuffer).put(src, srcOffset, longCount); 164 } 165 this.position += longCount; 166 return this; 167 } 168 169 @Override 170 public LongBuffer slice() { 171 byteBuffer.limit(limit * SizeOf.LONG); 172 byteBuffer.position(position * SizeOf.LONG); 173 ByteBuffer bb = byteBuffer.slice().order(byteBuffer.order()); 174 LongBuffer result = new ByteBufferAsLongBuffer(bb); 175 byteBuffer.clear(); 176 return result; 177 } 178 179 } 180