Package java.nio

Examples of java.nio.ByteBuffer.slice()


  private <K, V> void assertRoundTripEquality(KeyValuesMarshaller<K, V> marshaller, K key,
      List<V> values) {
    ByteBuffer bytes = marshaller.toBytes(new KeyValue<>(key, values));
    KeyValue<K, Iterable<V>> reconstructed = marshaller.fromBytes(bytes.slice());
    validateEqual(key, values, reconstructed);
    reconstructed = marshaller.fromBytes(bytes.slice());
    assertEquals(bytes, marshaller.toBytes(reconstructed));
  }

  private <K, V> void validateEqual(K key, List<V> values,
      KeyValue<K, ? extends Iterable<V>> reconstructed) {
View Full Code Here


      @SuppressWarnings({"unchecked", "rawtypes"})
      ByteBuffer bytes = m.toBytes(new KeyValue(key, valueValues));
      KeyValue<KeyValue<Integer, ? extends Iterable<String>>,
          Iterable<KeyValue<Integer, ? extends Iterable<String>>>> reconstructed =
              m.fromBytes(bytes.slice());
      validateEqual(key.getKey(), keyStrings, reconstructed.getKey());

      Iterator<KeyValue<Integer, ? extends Iterable<String>>> reconValues =
          reconstructed.getValue().iterator();
      for (KeyValue<Integer, ? extends List<String>> value : valueValues) {
View Full Code Here

    LinkedHashMap<ByteBuffer, List<ByteBuffer>> map =
        sortUntilFull(s, new StringStringGenerator(numberToWrite - 1), null);
    s = createWorker(numberToWrite);
    ByteBuffer last = map.keySet().toArray(new ByteBuffer[] {})[numberToWrite - 2];
    map = sortUntilFull(s, new StringStringGenerator(numberToWrite - 1),
        new KeyValue<>(last.slice(), ByteBuffer.allocate(StringStringGenerator.VALUE_SIZE)));
    assertTrue(s.isFull()); // Confirms the bufferCapacity setting above.
    assertEquals(numberToWrite - 1, map.size());
    String previous = "\0";
    for (ByteBuffer key : map.keySet()) {
      String string = US_ASCII.decode(key).toString();
View Full Code Here

     * without the length prefix.
     */
    ByteBuffer result = readBuffer.duplicate();
    result.limit(messageLen);
    result.position(PREFIX_LENGTH);
                set(result.slice().asReadOnlyBuffer());
                return null;
            } else {
    if (logger.isLoggable(Level.FINER)) {
        logger.log(Level.FINER, "{0} read incomplete {1}:{2}",
             this, messageLen, readBuffer.position());
View Full Code Here

    @Override
    public ByteBuffer read(int position, int length) {
        ByteBuffer entry = buffer.asReadOnlyBuffer();
        entry.position(position);
        entry.limit(position + length);
        return entry.slice();
    }

    @Override
    public synchronized void write(
            int position, byte[] b, int offset, int length)
View Full Code Here

            if (encoding != DataBlockEncoding.NONE) {
              // We expect a two-byte big-endian encoding id.
              assertEquals(0, actualBuffer.get(0));
              assertEquals(encoding.getId(), actualBuffer.get(1));
              actualBuffer.position(2);
              actualBuffer = actualBuffer.slice();
            }

            ByteBuffer expectedBuffer = encodedBlocks.get(blockId);
            expectedBuffer.rewind();
View Full Code Here

                case Leader.REQUEST:
                    bb = ByteBuffer.wrap(qp.getData());
                    sessionId = bb.getLong();
                    cxid = bb.getInt();
                    type = bb.getInt();
                    bb = bb.slice();
                    if(type == OpCode.sync){
                       leader.zk.submitRequest(new FollowerSyncRequest(this, sessionId, cxid, type, bb,
                                qp.getAuthinfo()));
                    } else {
                    leader.zk.submitRequest(null, sessionId, type, cxid, bb,
View Full Code Here

      }

      // TODO: Could this be done with one ByteBuffer rather than create two?
      ByteBuffer valueBuff = blockBuffer.slice();
      valueBuff.position(currKeyLen);
      valueBuff = valueBuff.slice();
      valueBuff.limit(currValueLen);
      valueBuff.rewind();
      return valueBuff;
    }
View Full Code Here

  public int charAt(int position) {
    if (position > this.length) return -1; // too long
    if (position < 0) return -1; // duh.
     
    ByteBuffer bb = (ByteBuffer)ByteBuffer.wrap(bytes).position(position);
    return bytesToCodePoint(bb.slice());
  }
 
  public int find(String what) {
    return find(what, 0);
  }
View Full Code Here

            if (encoding != DataBlockEncoding.NONE) {
              // We expect a two-byte big-endian encoding id.
              assertEquals(0, actualBuffer.get(0));
              assertEquals(encoding.getId(), actualBuffer.get(1));
              actualBuffer.position(2);
              actualBuffer = actualBuffer.slice();
            }

            ByteBuffer expectedBuffer = encodedBlocks.get(blockId);
            expectedBuffer.rewind();
View Full Code Here

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.