001/* ShortBuffer.java -- 002 Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc. 003 004This file is part of GNU Classpath. 005 006GNU Classpath is free software; you can redistribute it and/or modify 007it under the terms of the GNU General Public License as published by 008the Free Software Foundation; either version 2, or (at your option) 009any later version. 010 011GNU Classpath is distributed in the hope that it will be useful, but 012WITHOUT ANY WARRANTY; without even the implied warranty of 013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014General Public License for more details. 015 016You should have received a copy of the GNU General Public License 017along with GNU Classpath; see the file COPYING. If not, write to the 018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 01902110-1301 USA. 020 021Linking this library statically or dynamically with other modules is 022making a combined work based on this library. Thus, the terms and 023conditions of the GNU General Public License cover the whole 024combination. 025 026As a special exception, the copyright holders of this library give you 027permission to link this library with independent modules to produce an 028executable, regardless of the license terms of these independent 029modules, and to copy and distribute the resulting executable under 030terms of your choice, provided that you also meet, for each linked 031independent module, the terms and conditions of the license of that 032module. An independent module is a module which is not derived from 033or based on this library. If you modify this library, you may extend 034this exception to your version of the library, but you are not 035obligated to do so. If you do not wish to do so, delete this 036exception statement from your version. */ 037 038 039package java.nio; 040 041// GCJ LOCAL: Use RawData instead of gnu.classpath.Pointer 042import gnu.gcj.RawData; 043 044/** 045 * @since 1.4 046 */ 047public abstract class ShortBuffer extends Buffer 048 implements Comparable<ShortBuffer> 049{ 050 final int array_offset; 051 final short[] backing_buffer; 052 053 ShortBuffer (int capacity, int limit, int position, 054 int mark, RawData address, short[] backing_buffer, 055 int array_offset) 056 { 057 super (capacity, limit, position, mark, address); 058 this.backing_buffer = backing_buffer; 059 this.array_offset = array_offset; 060 } 061 062 /** 063 * Allocates a new <code>ShortBuffer</code> object with a given capacity. 064 */ 065 public static ShortBuffer allocate (int capacity) 066 { 067 return new ShortBufferImpl (capacity); 068 } 069 070 /** 071 * Wraps a <code>short</code> array into a <code>ShortBuffer</code> 072 * object. 073 * 074 * @exception IndexOutOfBoundsException If the preconditions on the offset 075 * and length parameters do not hold 076 */ 077 public static final ShortBuffer wrap (short[] array, int offset, int length) 078 { 079 return new ShortBufferImpl (array, 0, array.length, offset + length, offset, -1, false); 080 } 081 082 /** 083 * Wraps a <code>short</code> array into a <code>ShortBuffer</code> 084 * object. 085 */ 086 public static final ShortBuffer wrap (short[] array) 087 { 088 return wrap (array, 0, array.length); 089 } 090 091 /** 092 * This method transfers <code>short</code>s from this buffer into the given 093 * destination array. Before the transfer, it checks if there are fewer than 094 * length <code>short</code>s remaining in this buffer. 095 * 096 * @param dst The destination array 097 * @param offset The offset within the array of the first <code>short</code> 098 * to be written; must be non-negative and no larger than dst.length. 099 * @param length The maximum number of bytes to be written to the given array; 100 * must be non-negative and no larger than dst.length - offset. 101 * 102 * @exception BufferUnderflowException If there are fewer than length 103 * <code>short</code>s remaining in this buffer. 104 * @exception IndexOutOfBoundsException If the preconditions on the offset 105 * and length parameters do not hold. 106 */ 107 public ShortBuffer get (short[] dst, int offset, int length) 108 { 109 checkArraySize(dst.length, offset, length); 110 checkForUnderflow(length); 111 112 for (int i = offset; i < offset + length; i++) 113 { 114 dst [i] = get (); 115 } 116 117 return this; 118 } 119 120 /** 121 * This method transfers <code>short</code>s from this buffer into the given 122 * destination array. 123 * 124 * @param dst The byte array to write into. 125 * 126 * @exception BufferUnderflowException If there are fewer than dst.length 127 * <code>short</code>s remaining in this buffer. 128 */ 129 public ShortBuffer get (short[] dst) 130 { 131 return get (dst, 0, dst.length); 132 } 133 134 /** 135 * Writes the content of the the <code>ShortBUFFER</code> src 136 * into the buffer. Before the transfer, it checks if there is fewer than 137 * <code>src.remaining()</code> space remaining in this buffer. 138 * 139 * @param src The source data. 140 * 141 * @exception BufferOverflowException If there is insufficient space in this 142 * buffer for the remaining <code>short</code>s in the source buffer. 143 * @exception IllegalArgumentException If the source buffer is this buffer. 144 * @exception ReadOnlyBufferException If this buffer is read-only. 145 */ 146 public ShortBuffer put (ShortBuffer src) 147 { 148 if (src == this) 149 throw new IllegalArgumentException (); 150 151 checkForOverflow(src.remaining ()); 152 153 if (src.remaining () > 0) 154 { 155 short[] toPut = new short [src.remaining ()]; 156 src.get (toPut); 157 put (toPut); 158 } 159 160 return this; 161 } 162 163 /** 164 * Writes the content of the the <code>short array</code> src 165 * into the buffer. Before the transfer, it checks if there is fewer than 166 * length space remaining in this buffer. 167 * 168 * @param src The array to copy into the buffer. 169 * @param offset The offset within the array of the first byte to be read; 170 * must be non-negative and no larger than src.length. 171 * @param length The number of bytes to be read from the given array; 172 * must be non-negative and no larger than src.length - offset. 173 * 174 * @exception BufferOverflowException If there is insufficient space in this 175 * buffer for the remaining <code>short</code>s in the source array. 176 * @exception IndexOutOfBoundsException If the preconditions on the offset 177 * and length parameters do not hold 178 * @exception ReadOnlyBufferException If this buffer is read-only. 179 */ 180 public ShortBuffer put (short[] src, int offset, int length) 181 { 182 checkArraySize(src.length, offset, length); 183 checkForOverflow(length); 184 185 for (int i = offset; i < offset + length; i++) 186 put (src [i]); 187 188 return this; 189 } 190 191 /** 192 * Writes the content of the the <code>short array</code> src 193 * into the buffer. 194 * 195 * @param src The array to copy into the buffer. 196 * 197 * @exception BufferOverflowException If there is insufficient space in this 198 * buffer for the remaining <code>short</code>s in the source array. 199 * @exception ReadOnlyBufferException If this buffer is read-only. 200 */ 201 public final ShortBuffer put (short[] src) 202 { 203 return put (src, 0, src.length); 204 } 205 206 /** 207 * Tells whether ot not this buffer is backed by an accessible 208 * <code>short</code> array. 209 */ 210 public final boolean hasArray () 211 { 212 return (backing_buffer != null 213 && !isReadOnly ()); 214 } 215 216 /** 217 * Returns the <code>short</code> array that backs this buffer. 218 * 219 * @exception ReadOnlyBufferException If this buffer is read-only. 220 * @exception UnsupportedOperationException If this buffer is not backed 221 * by an accessible array. 222 */ 223 public final short[] array () 224 { 225 if (backing_buffer == null) 226 throw new UnsupportedOperationException (); 227 228 checkIfReadOnly(); 229 230 return backing_buffer; 231 } 232 233 /** 234 * Returns the offset within this buffer's backing array of the first element. 235 * 236 * @exception ReadOnlyBufferException If this buffer is read-only. 237 * @exception UnsupportedOperationException If this buffer is not backed 238 * by an accessible array. 239 */ 240 public final int arrayOffset () 241 { 242 if (backing_buffer == null) 243 throw new UnsupportedOperationException (); 244 245 checkIfReadOnly(); 246 247 return array_offset; 248 } 249 250 /** 251 * Calculates a hash code for this buffer. 252 * 253 * This is done with <code>int</code> arithmetic, 254 * where ** represents exponentiation, by this formula:<br> 255 * <code>s[position()] + 31 + (s[position()+1] + 30)*31**1 + ... + 256 * (s[limit()-1]+30)*31**(limit()-1)</code>. 257 * Where s is the buffer data. Note that the hashcode is dependent 258 * on buffer content, and therefore is not useful if the buffer 259 * content may change. 260 * 261 * @return the hash code 262 */ 263 public int hashCode () 264 { 265 int hashCode = get(position()) + 31; 266 int multiplier = 1; 267 for (int i = position() + 1; i < limit(); ++i) 268 { 269 multiplier *= 31; 270 hashCode += (get(i) + 30)*multiplier; 271 } 272 return hashCode; 273 } 274 275 /** 276 * Checks if this buffer is equal to obj. 277 */ 278 public boolean equals (Object obj) 279 { 280 if (obj instanceof ShortBuffer) 281 { 282 return compareTo ((ShortBuffer) obj) == 0; 283 } 284 285 return false; 286 } 287 288 /** 289 * Compares two <code>ShortBuffer</code> objects. 290 * 291 * @exception ClassCastException If obj is not an object derived from 292 * <code>ShortBuffer</code>. 293 */ 294 public int compareTo (ShortBuffer other) 295 { 296 int num = Math.min(remaining(), other.remaining()); 297 int pos_this = position(); 298 int pos_other = other.position(); 299 300 for (int count = 0; count < num; count++) 301 { 302 short a = get(pos_this++); 303 short b = other.get(pos_other++); 304 305 if (a == b) 306 continue; 307 308 if (a < b) 309 return -1; 310 311 return 1; 312 } 313 314 return remaining() - other.remaining(); 315 } 316 317 /** 318 * Returns the byte order of this buffer. 319 */ 320 public abstract ByteOrder order (); 321 322 /** 323 * Reads the <code>short</code> at this buffer's current position, 324 * and then increments the position. 325 * 326 * @exception BufferUnderflowException If there are no remaining 327 * <code>short</code>s in this buffer. 328 */ 329 public abstract short get (); 330 331 /** 332 * Writes the <code>short</code> at this buffer's current position, 333 * and then increments the position. 334 * 335 * @exception BufferOverflowException If there no remaining 336 * <code>short</code>s in this buffer. 337 * @exception ReadOnlyBufferException If this buffer is read-only. 338 */ 339 public abstract ShortBuffer put (short b); 340 341 /** 342 * Absolute get method. 343 * 344 * @exception IndexOutOfBoundsException If index is negative or not smaller 345 * than the buffer's limit. 346 */ 347 public abstract short get (int index); 348 349 /** 350 * Absolute put method. 351 * 352 * @exception IndexOutOfBoundsException If index is negative or not smaller 353 * than the buffer's limit. 354 * @exception ReadOnlyBufferException If this buffer is read-only. 355 */ 356 public abstract ShortBuffer put (int index, short b); 357 358 /** 359 * Compacts this buffer. 360 * 361 * @exception ReadOnlyBufferException If this buffer is read-only. 362 */ 363 public abstract ShortBuffer compact (); 364 365 /** 366 * Tells wether or not this buffer is direct. 367 */ 368 public abstract boolean isDirect (); 369 370 /** 371 * Creates a new <code>ShortBuffer</code> whose content is a shared 372 * subsequence of this buffer's content. 373 */ 374 public abstract ShortBuffer slice (); 375 376 /** 377 * Creates a new <code>ShortBuffer</code> that shares this buffer's 378 * content. 379 */ 380 public abstract ShortBuffer duplicate (); 381 382 /** 383 * Creates a new read-only <code>ShortBuffer</code> that shares this 384 * buffer's content. 385 */ 386 public abstract ShortBuffer asReadOnlyBuffer (); 387}