Source for java.nio.LongBuffer

   1: /* LongBuffer.java --
   2:    Copyright (C) 2002, 2003, 2004, 2005  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package java.nio;
  40: 
  41: import gnu.classpath.Pointer;
  42: 
  43: /**
  44:  * @since 1.4
  45:  */
  46: public abstract class LongBuffer extends Buffer
  47:   implements Comparable<LongBuffer>
  48: {
  49:   final int array_offset;
  50:   final long[] backing_buffer;
  51: 
  52:   LongBuffer (int capacity, int limit, int position, int mark,
  53:               Pointer address, long[] backing_buffer, int array_offset)
  54:   {
  55:     super (capacity, limit, position, mark, address);
  56:     this.backing_buffer = backing_buffer;
  57:     this.array_offset = array_offset;
  58:   }
  59: 
  60:   /**
  61:    * Allocates a new <code>LongBuffer</code> object with a given capacity.
  62:    */
  63:   public static LongBuffer allocate (int capacity)
  64:   {
  65:     return new LongBufferImpl (capacity);
  66:   }
  67: 
  68:   /**
  69:    * Wraps a <code>long</code> array into a <code>LongBuffer</code>
  70:    * object.
  71:    *
  72:    * @exception IndexOutOfBoundsException If the preconditions on the offset
  73:    * and length parameters do not hold
  74:    */
  75:   public static final LongBuffer wrap (long[] array, int offset, int length)
  76:   {
  77:     return new LongBufferImpl (array, 0, array.length, offset + length, offset, -1, false);
  78:   }
  79: 
  80:   /**
  81:    * Wraps a <code>long</code> array into a <code>LongBuffer</code>
  82:    * object.
  83:    */
  84:   public static final LongBuffer wrap (long[] array)
  85:   {
  86:     return wrap (array, 0, array.length);
  87:   }
  88: 
  89:   /**
  90:    * This method transfers <code>long</code>s from this buffer into the given
  91:    * destination array. Before the transfer, it checks if there are fewer than
  92:    * length <code>long</code>s remaining in this buffer.
  93:    *
  94:    * @param dst The destination array
  95:    * @param offset The offset within the array of the first <code>long</code>
  96:    * to be written; must be non-negative and no larger than dst.length.
  97:    * @param length The maximum number of bytes to be written to the given array;
  98:    * must be non-negative and no larger than dst.length - offset.
  99:    *
 100:    * @exception BufferUnderflowException If there are fewer than length
 101:    * <code>long</code>s remaining in this buffer.
 102:    * @exception IndexOutOfBoundsException If the preconditions on the offset
 103:    * and length parameters do not hold.
 104:    */
 105:   public LongBuffer get (long[] dst, int offset, int length)
 106:   {
 107:     checkArraySize(dst.length, offset, length);
 108:     checkForUnderflow(length);
 109: 
 110:     for (int i = offset; i < offset + length; i++)
 111:       {
 112:         dst [i] = get ();
 113:       }
 114: 
 115:     return this;
 116:   }
 117: 
 118:   /**
 119:    * This method transfers <code>long</code>s from this buffer into the given
 120:    * destination array.
 121:    *
 122:    * @param dst The byte array to write into.
 123:    *
 124:    * @exception BufferUnderflowException If there are fewer than dst.length
 125:    * <code>long</code>s remaining in this buffer.
 126:    */
 127:   public LongBuffer get (long[] dst)
 128:   {
 129:     return get (dst, 0, dst.length);
 130:   }
 131: 
 132:   /**
 133:    * Writes the content of the the <code>LongBUFFER</code> src
 134:    * into the buffer. Before the transfer, it checks if there is fewer than
 135:    * <code>src.remaining()</code> space remaining in this buffer.
 136:    *
 137:    * @param src The source data.
 138:    *
 139:    * @exception BufferOverflowException If there is insufficient space in this
 140:    * buffer for the remaining <code>long</code>s in the source buffer.
 141:    * @exception IllegalArgumentException If the source buffer is this buffer.
 142:    * @exception ReadOnlyBufferException If this buffer is read-only.
 143:    */
 144:   public LongBuffer put (LongBuffer src)
 145:   {
 146:     if (src == this)
 147:       throw new IllegalArgumentException ();
 148: 
 149:     checkForOverflow(src.remaining ());
 150: 
 151:     if (src.remaining () > 0)
 152:       {
 153:         long[] toPut = new long [src.remaining ()];
 154:         src.get (toPut);
 155:         put (toPut);
 156:       }
 157: 
 158:     return this;
 159:   }
 160: 
 161:   /**
 162:    * Writes the content of the the <code>long array</code> src
 163:    * into the buffer. Before the transfer, it checks if there is fewer than
 164:    * length space remaining in this buffer.
 165:    *
 166:    * @param src The array to copy into the buffer.
 167:    * @param offset The offset within the array of the first byte to be read;
 168:    * must be non-negative and no larger than src.length.
 169:    * @param length The number of bytes to be read from the given array;
 170:    * must be non-negative and no larger than src.length - offset.
 171:    *
 172:    * @exception BufferOverflowException If there is insufficient space in this
 173:    * buffer for the remaining <code>long</code>s in the source array.
 174:    * @exception IndexOutOfBoundsException If the preconditions on the offset
 175:    * and length parameters do not hold
 176:    * @exception ReadOnlyBufferException If this buffer is read-only.
 177:    */
 178:   public LongBuffer put (long[] src, int offset, int length)
 179:   {
 180:     checkArraySize(src.length, offset, length);
 181:     checkForOverflow(length);
 182: 
 183:     for (int i = offset; i < offset + length; i++)
 184:       put (src [i]);
 185: 
 186:     return this;
 187:   }
 188: 
 189:   /**
 190:    * Writes the content of the the <code>long array</code> src
 191:    * into the buffer.
 192:    *
 193:    * @param src The array to copy into the buffer.
 194:    *
 195:    * @exception BufferOverflowException If there is insufficient space in this
 196:    * buffer for the remaining <code>long</code>s in the source array.
 197:    * @exception ReadOnlyBufferException If this buffer is read-only.
 198:    */
 199:   public final LongBuffer put (long[] src)
 200:   {
 201:     return put (src, 0, src.length);
 202:   }
 203: 
 204:   /**
 205:    * Tells whether ot not this buffer is backed by an accessible
 206:    * <code>long</code> array.
 207:    */
 208:   public final boolean hasArray ()
 209:   {
 210:     return (backing_buffer != null
 211:             && !isReadOnly ());
 212:   }
 213: 
 214:   /**
 215:    * Returns the <code>long</code> array that backs this buffer.
 216:    *
 217:    * @exception ReadOnlyBufferException If this buffer is read-only.
 218:    * @exception UnsupportedOperationException If this buffer is not backed
 219:    * by an accessible array.
 220:    */
 221:   public final long[] array ()
 222:   {
 223:     if (backing_buffer == null)
 224:       throw new UnsupportedOperationException ();
 225: 
 226:     checkIfReadOnly();
 227: 
 228:     return backing_buffer;
 229:   }
 230: 
 231:   /**
 232:    * Returns the offset within this buffer's backing array of the first element.
 233:    *
 234:    * @exception ReadOnlyBufferException If this buffer is read-only.
 235:    * @exception UnsupportedOperationException If this buffer is not backed
 236:    * by an accessible array.
 237:    */
 238:   public final int arrayOffset ()
 239:   {
 240:     if (backing_buffer == null)
 241:       throw new UnsupportedOperationException ();
 242: 
 243:     checkIfReadOnly();
 244: 
 245:     return array_offset;
 246:   }
 247: 
 248:   /**
 249:    * Calculates a hash code for this buffer.
 250:    *
 251:    * This is done with <code>long</code> arithmetic,
 252:    * where ** represents exponentiation, by this formula:<br>
 253:    * <code>s[position()] + 31 + (s[position()+1] + 30)*31**1 + ... +
 254:    * (s[limit()-1]+30)*31**(limit()-1)</code>.
 255:    * Where s is the buffer data. Note that the hashcode is dependent
 256:    * on buffer content, and therefore is not useful if the buffer
 257:    * content may change.
 258:    *
 259:    * @return the hash code (casted to int)
 260:    */
 261:   public int hashCode ()
 262:   {
 263:     long hashCode = get(position()) + 31;
 264:     long multiplier = 1;
 265:     for (int i = position() + 1; i < limit(); ++i)
 266:       {
 267:           multiplier *= 31;
 268:           hashCode += (get(i) + 30)*multiplier;
 269:       }
 270:     return ((int)hashCode);
 271:   }
 272: 
 273:   /**
 274:    * Checks if this buffer is equal to obj.
 275:    */
 276:   public boolean equals (Object obj)
 277:   {
 278:     if (obj instanceof LongBuffer)
 279:       {
 280:         return compareTo ((LongBuffer) obj) == 0;
 281:       }
 282: 
 283:     return false;
 284:   }
 285: 
 286:   /**
 287:    * Compares two <code>LongBuffer</code> objects.
 288:    *
 289:    * @exception ClassCastException If obj is not an object derived from
 290:    * <code>LongBuffer</code>.
 291:    */
 292:   public int compareTo (LongBuffer other)
 293:   {
 294:     int num = Math.min(remaining(), other.remaining());
 295:     int pos_this = position();
 296:     int pos_other = other.position();
 297: 
 298:     for (int count = 0; count < num; count++)
 299:       {
 300:          long a = get(pos_this++);
 301:          long b = other.get(pos_other++);
 302: 
 303:          if (a == b)
 304:            continue;
 305: 
 306:          if (a < b)
 307:            return -1;
 308: 
 309:          return 1;
 310:       }
 311: 
 312:      return remaining() - other.remaining();
 313:   }
 314: 
 315:   /**
 316:    * Returns the byte order of this buffer.
 317:    */
 318:   public abstract ByteOrder order ();
 319: 
 320:   /**
 321:    * Reads the <code>long</code> at this buffer's current position,
 322:    * and then increments the position.
 323:    *
 324:    * @exception BufferUnderflowException If there are no remaining
 325:    * <code>long</code>s in this buffer.
 326:    */
 327:   public abstract long get ();
 328: 
 329:   /**
 330:    * Writes the <code>long</code> at this buffer's current position,
 331:    * and then increments the position.
 332:    *
 333:    * @exception BufferOverflowException If there no remaining
 334:    * <code>long</code>s in this buffer.
 335:    * @exception ReadOnlyBufferException If this buffer is read-only.
 336:    */
 337:   public abstract LongBuffer put (long b);
 338: 
 339:   /**
 340:    * Absolute get method.
 341:    *
 342:    * @exception IndexOutOfBoundsException If index is negative or not smaller
 343:    * than the buffer's limit.
 344:    */
 345:   public abstract long get (int index);
 346: 
 347:   /**
 348:    * Absolute put method.
 349:    *
 350:    * @exception IndexOutOfBoundsException If index is negative or not smaller
 351:    * than the buffer's limit.
 352:    * @exception ReadOnlyBufferException If this buffer is read-only.
 353:    */
 354:   public abstract LongBuffer put (int index, long b);
 355: 
 356:   /**
 357:    * Compacts this buffer.
 358:    *
 359:    * @exception ReadOnlyBufferException If this buffer is read-only.
 360:    */
 361:   public abstract LongBuffer compact ();
 362: 
 363:   /**
 364:    * Tells wether or not this buffer is direct.
 365:    */
 366:   public abstract boolean isDirect ();
 367: 
 368:   /**
 369:    * Creates a new <code>LongBuffer</code> whose content is a shared
 370:    * subsequence of this buffer's content.
 371:    */
 372:   public abstract LongBuffer slice ();
 373: 
 374:   /**
 375:    * Creates a new <code>LongBuffer</code> that shares this buffer's
 376:    * content.
 377:    */
 378:   public abstract LongBuffer duplicate ();
 379: 
 380:   /**
 381:    * Creates a new read-only <code>LongBuffer</code> that shares this
 382:    * buffer's content.
 383:    */
 384:   public abstract LongBuffer asReadOnlyBuffer ();
 385: }