1 7 8 10 package java.nio; 11 12 13 class ByteBufferAsCharBufferB extends CharBuffer 15 { 16 17 18 19 protected final ByteBuffer bb; 20 protected final int offset; 21 22 23 24 ByteBufferAsCharBufferB(ByteBuffer bb) { 26 super(-1, 0, 27 bb.remaining() >> 1, 28 bb.remaining() >> 1); 29 this.bb = bb; 30 int cap = this.capacity(); 32 this.limit(cap); 33 int pos = this.position(); 34 assert (pos <= cap); 35 offset = pos; 36 37 38 39 } 40 41 ByteBufferAsCharBufferB(ByteBuffer bb, 42 int mark, int pos, int lim, int cap, 43 int off) 44 { 45 46 super(mark, pos, lim, cap); 47 this.bb = bb; 48 offset = off; 49 50 51 52 } 53 54 public CharBuffer slice() { 55 int pos = this.position(); 56 int lim = this.limit(); 57 assert (pos <= lim); 58 int rem = (pos <= lim ? lim - pos : 0); 59 int off = (pos << 1) + offset; 60 assert (off >= 0); 61 return new ByteBufferAsCharBufferB (bb, -1, 0, rem, rem, off); 62 } 63 64 public CharBuffer duplicate() { 65 return new ByteBufferAsCharBufferB (bb, 66 this.markValue(), 67 this.position(), 68 this.limit(), 69 this.capacity(), 70 offset); 71 } 72 73 public CharBuffer asReadOnlyBuffer() { 74 75 return new ByteBufferAsCharBufferRB (bb, 76 this.markValue(), 77 this.position(), 78 this.limit(), 79 this.capacity(), 80 offset); 81 82 83 84 } 85 86 87 88 protected int ix(int i) { 89 return (i << 1) + offset; 90 } 91 92 public char get() { 93 return Bits.getCharB(bb, ix(nextGetIndex())); 94 } 95 96 public char get(int i) { 97 return Bits.getCharB(bb, ix(checkIndex(i))); 98 } 99 100 101 102 public CharBuffer put(char x) { 103 104 Bits.putCharB(bb, ix(nextPutIndex()), x); 105 return this; 106 107 108 109 } 110 111 public CharBuffer put(int i, char x) { 112 113 Bits.putCharB(bb, ix(checkIndex(i)), x); 114 return this; 115 116 117 118 } 119 120 public CharBuffer compact() { 121 122 int pos = position(); 123 int lim = limit(); 124 assert (pos <= lim); 125 int rem = (pos <= lim ? lim - pos : 0); 126 127 ByteBuffer db = bb.duplicate(); 128 db.limit(ix(lim)); 129 db.position(ix(0)); 130 ByteBuffer sb = db.slice(); 131 sb.position(pos << 1); 132 sb.compact(); 133 position(rem); 134 limit(capacity()); 135 return this; 136 137 138 139 } 140 141 public boolean isDirect() { 142 return bb.isDirect(); 143 } 144 145 public boolean isReadOnly() { 146 return false; 147 } 148 149 150 151 public String toString(int start, int end) { 152 if ((end > limit()) || (start > end)) 153 throw new IndexOutOfBoundsException (); 154 try { 155 int len = end - start; 156 char[] ca = new char[len]; 157 CharBuffer cb = CharBuffer.wrap(ca); 158 CharBuffer db = this.duplicate(); 159 db.position(start); 160 db.limit(end); 161 cb.put(db); 162 return new String (ca); 163 } catch (StringIndexOutOfBoundsException x) { 164 throw new IndexOutOfBoundsException (); 165 } 166 } 167 168 169 171 public CharSequence subSequence(int start, int end) { 172 int pos = position(); 173 int lim = limit(); 174 assert (pos <= lim); 175 pos = (pos <= lim ? pos : lim); 176 int len = lim - pos; 177 178 if ((start < 0) || (end > len) || (start > end)) 179 throw new IndexOutOfBoundsException (); 180 int sublen = end - start; 181 int off = offset + ((pos + start) << 1); 182 assert (off >= 0); 183 return new ByteBufferAsCharBufferB (bb, -1, 0, sublen, sublen, off); 184 } 185 186 187 188 189 public ByteOrder order() { 190 191 return ByteOrder.BIG_ENDIAN; 192 193 194 195 196 } 197 198 } 199 | Popular Tags |