Beispiel #1
0
 public static void main(String[] args) {
   ByteBuffer bb = ByteBuffer.wrap(new byte[12]);
   bb.asCharBuffer().put("abcdef");
   System.out.println(Arrays.toString(bb.array()));
   bb.rewind();
   bb.order(ByteOrder.BIG_ENDIAN);
   bb.asCharBuffer().put("abcdef");
   System.out.println(Arrays.toString(bb.array()));
   bb.rewind();
   bb.order(ByteOrder.LITTLE_ENDIAN);
   bb.asCharBuffer().put("abcdef");
   System.out.println(Arrays.toString(bb.array()));
 }
 /**
  * Receive as many items as possible from the given byte buffer to this buffer.
  *
  * <p>The <TT>receiveItems()</TT> method must not block the calling thread; if it does, all
  * message I/O in MP will be blocked.
  *
  * @param i Index of first item to receive, in the range 0 .. <TT>length</TT>-1.
  * @param num Maximum number of items to receive.
  * @param buffer Byte buffer.
  * @return Number of items received.
  */
 protected int receiveItems(int i, int num, ByteBuffer buffer) {
   CharBuffer charbuffer = buffer.asCharBuffer();
   int n = 0;
   int r = i / myColCount;
   int row = r + myLowerRow;
   int c = i % myColCount;
   int col = c + myLowerCol;
   int ncols = Math.min(myColCount - c, charbuffer.remaining());
   while (r < myRowCount && ncols > 0) {
     char[] myMatrix_row = myMatrix[row];
     while (c < ncols) {
       myMatrix_row[col] = myOp.op(myMatrix_row[col], charbuffer.get());
       ++c;
       ++col;
     }
     n += ncols;
     ++r;
     ++row;
     c = 0;
     col = myLowerCol;
     ncols = Math.min(myColCount, charbuffer.remaining());
   }
   buffer.position(buffer.position() + 2 * n);
   return n;
 }
 public static void main(String[] args) {
   ByteBuffer bb = ByteBuffer.allocate(BSIZE);
   // Allocation automatically zeroes the ByteBuffer:
   int i = 0;
   while (i++ < bb.limit()) if (bb.get() != 0) print("nonzero");
   print("i = " + i);
   bb.rewind();
   // Store and read a char array:
   bb.asCharBuffer().put("Howdy!");
   char c;
   while ((c = bb.getChar()) != 0) printnb(c + " ");
   print();
   bb.rewind();
   // Store and read a short:
   bb.asShortBuffer().put((short) 471142);
   print(bb.getShort());
   bb.rewind();
   // Store and read an int:
   bb.asIntBuffer().put(99471142);
   print(bb.getInt());
   bb.rewind();
   // Store and read a long:
   bb.asLongBuffer().put(99471142);
   print(bb.getLong());
   bb.rewind();
   // Store and read a float:
   bb.asFloatBuffer().put(99471142);
   print(bb.getFloat());
   bb.rewind();
   // Store and read a double:
   bb.asDoubleBuffer().put(99471142);
   print(bb.getDouble());
   bb.rewind();
 }
 /**
  * Send as many items as possible from this buffer to the given byte buffer.
  *
  * <p>The <TT>sendItems()</TT> method must not block the calling thread; if it does, all message
  * I/O in MP will be blocked.
  *
  * @param i Index of first item to send, in the range 0 .. <TT>length</TT>-1.
  * @param buffer Byte buffer.
  * @return Number of items sent.
  */
 protected int sendItems(int i, ByteBuffer buffer) {
   CharBuffer charbuffer = buffer.asCharBuffer();
   int n = 0;
   int r = i2r(i);
   int row = r * myRowStride + myLowerRow;
   int c = i2c(i);
   int col = c * myColStride + myLowerCol;
   int ncols = Math.min(myColCount - c, charbuffer.remaining());
   while (r < myRowCount && ncols > 0) {
     char[] myMatrix_row = myMatrix[row];
     while (c < ncols) {
       charbuffer.put(myMatrix_row[col]);
       ++c;
       col += myColStride;
     }
     n += ncols;
     ++r;
     row += myRowStride;
     c = 0;
     col = myLowerCol;
     ncols = Math.min(myColCount, charbuffer.remaining());
   }
   buffer.position(buffer.position() + 2 * n);
   return n;
 }
 public static String joinRecordsIntoString(
     long startRecordId, List<LegacyDynamicRecord> legacyDynamicRecords) {
   Map<Long, LegacyDynamicRecord> recordsMap = new HashMap<Long, LegacyDynamicRecord>();
   long recordToFind = startRecordId;
   for (LegacyDynamicRecord record : legacyDynamicRecords) {
     recordsMap.put(record.getId(), record);
   }
   List<char[]> charList = new LinkedList<char[]>();
   while (recordToFind != Record.NO_NEXT_BLOCK.intValue()) {
     LegacyDynamicRecord record = recordsMap.get(recordToFind);
     if (!record.isCharData()) {
       ByteBuffer buf = ByteBuffer.wrap(record.getData());
       char[] chars = new char[record.getData().length / 2];
       buf.asCharBuffer().get(chars);
       charList.add(chars);
     } else {
       charList.add(record.getDataAsChar());
     }
     recordToFind = record.getNextBlock();
   }
   StringBuffer buf = new StringBuffer();
   for (char[] str : charList) {
     buf.append(str);
   }
   return buf.toString();
 }
Beispiel #6
0
 /** @param isize */
 public BufferCounter(int isize) {
   size = isize;
   if (direct == true) {
     buf = ByteBuffer.allocateDirect(size * 4);
   } else {
     buf = ByteBuffer.allocate(size * 4);
   }
   buffer = buf.asCharBuffer();
 }
  public NioWrapLittleConversion() {

    // big/little endian difference one liner
    order = ByteOrder.LITTLE_ENDIAN;

    byteBuffer = ByteBuffer.allocateDirect(BUFFER_SIZE).order(order);

    // views on the bytebuffer to fill/drain it efficiently
    charBuffer = byteBuffer.asCharBuffer();
    shortBuffer = byteBuffer.asShortBuffer();
    intBuffer = byteBuffer.asIntBuffer();
    longBuffer = byteBuffer.asLongBuffer();
    floatBuffer = byteBuffer.asFloatBuffer();
    doubleBuffer = byteBuffer.asDoubleBuffer();
  }
  @NotNull
  public static IndexedCollection<Character> ofCharacter(@NotNull ByteBuffer byteBuffer) {
    int magic = byteBuffer.getInt();
    if (magic != MAGIC) {
      throw new IllegalArgumentException("bad magic number");
    }
    int version = byteBuffer.getInt();
    if (version != VERSION) {
      throw new IllegalArgumentException("bad version number");
    }

    int size = byteBuffer.getInt();
    CharBuffer values = byteBuffer.asCharBuffer();
    values.limit(size);
    byteBuffer.position(byteBuffer.position() + size * Integer.BYTES);
    return new CharacterBufferCollection(values.slice());
  }
Beispiel #9
0
 /**
  * Creates a buffer for the given number of elements and native byte ordering
  *
  * @param elements The number of elements in the buffer
  * @return The buffer
  */
 public static CharBuffer createCharBuffer(int elements) {
   ByteBuffer byteBuffer = ByteBuffer.allocateDirect(elements * 2);
   byteBuffer.order(ByteOrder.nativeOrder());
   return byteBuffer.asCharBuffer();
 }
 protected static CharBuffer newCharBuffer(int numElements) {
   ByteBuffer bb = ByteBuffer.allocateDirect((Character.SIZE / 8) * numElements);
   bb.order(ByteOrder.nativeOrder());
   return bb.asCharBuffer();
 }
Beispiel #11
0
 public static CharBuffer newCharBuffer(int numChars) {
   ByteBuffer buffer = ByteBuffer.allocateDirect(numChars * 2);
   buffer.order(ByteOrder.nativeOrder());
   return buffer.asCharBuffer();
 }
Beispiel #12
0
/**