Java Code Examples for java.nio.ByteBuffer.equals()

The following are Jave code examples for showing how to use equals() of the java.nio.ByteBuffer class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: jdk8u-jdk   File: Basic.java   View Source Code Vote up 6 votes
static void testUsingCompletionHandlers(AsynchronousFileChannel ch)
    throws IOException
{
    System.out.println("testUsingCompletionHandlers");

    ch.truncate(0L);

    // generate buffer with random elements and write it to file
    ByteBuffer src = genBuffer();
    writeFully(ch, src, 0L);

    // read to EOF or buffer is full
    ByteBuffer dst = (rand.nextBoolean()) ?
        ByteBuffer.allocateDirect(src.capacity()) :
        ByteBuffer.allocate(src.capacity());
    readAll(ch, dst, 0L);

    // check buffers are the same
    src.flip();
    dst.flip();
    if (!src.equals(dst)) {
        throw new RuntimeException("Contents differ");
    }
}
 
Example 2
Project: openjdk-jdk10   File: Basic.java   View Source Code Vote up 6 votes
static void testUsingCompletionHandlers(AsynchronousFileChannel ch)
    throws IOException
{
    System.out.println("testUsingCompletionHandlers");

    ch.truncate(0L);

    // generate buffer with random elements and write it to file
    ByteBuffer src = genBuffer();
    writeFully(ch, src, 0L);

    // read to EOF or buffer is full
    ByteBuffer dst = (rand.nextBoolean()) ?
        ByteBuffer.allocateDirect(src.capacity()) :
        ByteBuffer.allocate(src.capacity());
    readAll(ch, dst, 0L);

    // check buffers are the same
    src.flip();
    dst.flip();
    if (!src.equals(dst)) {
        throw new RuntimeException("Contents differ");
    }
}
 
Example 3
Project: openjdk-jdk10   File: TestUtils.java   View Source Code Vote up 6 votes
public static void valueCheck(byte[] array1, byte[] array2, int skip1,
        int skip2, int length) {
    ByteBuffer buf1 = ByteBuffer.wrap(array1);
    ByteBuffer buf2 = ByteBuffer.wrap(array2);

    // Skip past however many bytes are requested in both buffers
    buf1.position(skip1);
    buf2.position(skip2);

    // Reset the limits on each buffer to account for the length
    buf1.limit(buf1.position() + length);
    buf2.limit(buf2.position() + length);

    if (!buf1.equals(buf2)) {
        throw new RuntimeException("Array range mismatch");
    }
}
 
Example 4
Project: ditb   File: TestHFileBlock.java   View Source Code Vote up 6 votes
static void assertBuffersEqual(ByteBuffer expectedBuffer,
    ByteBuffer actualBuffer, Compression.Algorithm compression,
    DataBlockEncoding encoding, boolean pread) {
  if (!actualBuffer.equals(expectedBuffer)) {
    int prefix = 0;
    int minLimit = Math.min(expectedBuffer.limit(), actualBuffer.limit());
    while (prefix < minLimit &&
        expectedBuffer.get(prefix) == actualBuffer.get(prefix)) {
      prefix++;
    }

    fail(String.format(
        "Content mismatch for %s, commonPrefix %d, expected %s, got %s",
        buildMessageDetails(compression, encoding, pread), prefix,
        nextBytesToStr(expectedBuffer, prefix),
        nextBytesToStr(actualBuffer, prefix)));
  }
}
 
Example 5
Project: ditb   File: ParseFilter.java   View Source Code Vote up 6 votes
/**
* Takes a compareOperator symbol as a byte array and returns the corresponding CompareOperator
* <p>
* @param compareOpAsByteArray the comparatorOperator symbol as a byte array
* @return the Compare Operator
*/
 public static CompareFilter.CompareOp createCompareOp (byte [] compareOpAsByteArray) {
   ByteBuffer compareOp = ByteBuffer.wrap(compareOpAsByteArray);
   if (compareOp.equals(ParseConstants.LESS_THAN_BUFFER))
     return CompareOp.LESS;
   else if (compareOp.equals(ParseConstants.LESS_THAN_OR_EQUAL_TO_BUFFER))
     return CompareOp.LESS_OR_EQUAL;
   else if (compareOp.equals(ParseConstants.GREATER_THAN_BUFFER))
     return CompareOp.GREATER;
   else if (compareOp.equals(ParseConstants.GREATER_THAN_OR_EQUAL_TO_BUFFER))
     return CompareOp.GREATER_OR_EQUAL;
   else if (compareOp.equals(ParseConstants.NOT_EQUAL_TO_BUFFER))
     return CompareOp.NOT_EQUAL;
   else if (compareOp.equals(ParseConstants.EQUAL_TO_BUFFER))
     return CompareOp.EQUAL;
   else
     throw new IllegalArgumentException("Invalid compare operator");
 }
 
Example 6
Project: sstable-adaptor   File: ByteBufferUtil.java   View Source Code Vote up 5 votes
public static boolean isPrefix(ByteBuffer prefix, ByteBuffer value)
{
    if (prefix.remaining() > value.remaining())
        return false;

    int diff = value.remaining() - prefix.remaining();
    return prefix.equals(value.duplicate().limit(value.remaining() - diff));
}
 
Example 7
Project: mycat-src-1.6.1-RELEASE   File: ByteBufferUtil.java   View Source Code Vote up 5 votes
public static boolean isPrefix(ByteBuffer prefix, ByteBuffer value)
{
    if (prefix.remaining() > value.remaining()) {
        return false;
    }

    int diff = value.remaining() - prefix.remaining();
    return prefix.equals(value.duplicate().limit(value.remaining() - diff));
}
 
Example 8
Project: jdk8u-jdk   File: SameBuffer.java   View Source Code Vote up 5 votes
private void runGCMWithSeparateBuffers(int mode, ByteBuffer buffer,
        ByteBuffer textBB, int txtOffset, int dataLength,
        AlgorithmParameters params) throws Exception {
    // take offset into account
    textBB.position(txtOffset);
    textBB.mark();

    // first, generate the cipher text at an allocated buffer
    Cipher cipher = createCipher(mode, params);
    cipher.updateAAD(buffer);
    buffer.flip();
    ByteBuffer outBB = ByteBuffer.allocateDirect(
            cipher.getOutputSize(dataLength));

    cipher.doFinal(textBB, outBB);// get cipher text in outBB
    outBB.flip();

    // restore positions
    textBB.reset();

    // next, generate cipher text again in a buffer that shares content
    Cipher anotherCipher = createCipher(mode, params);
    anotherCipher.updateAAD(buffer);
    buffer.flip();
    ByteBuffer buf2 = textBB.duplicate(); // buf2 shares textBuf context
    buf2.limit(txtOffset + anotherCipher.getOutputSize(dataLength));
    int dataProcessed2 = anotherCipher.doFinal(textBB, buf2);
    buf2.position(txtOffset);
    buf2.limit(txtOffset + dataProcessed2);

    if (!buf2.equals(outBB)) {
        throw new RuntimeException(
                "Two results are not equal, mode:" + mode);
    }
}
 
Example 9
Project: jdk8u-jdk   File: SameBuffer.java   View Source Code Vote up 5 votes
private void runGCMWithSameBuffer(int mode, ByteBuffer buffer,
        int txtOffset, int length, AlgorithmParameters params)
        throws Exception {

    // allocate a separate buffer
    Cipher cipher = createCipher(mode, params);
    ByteBuffer outBB = ByteBuffer.allocateDirect(
            cipher.getOutputSize(length));

    // first, generate the cipher text at an allocated buffer
    buffer.flip();
    buffer.limit(AADLength);
    cipher.updateAAD(buffer);
    buffer.limit(AADLength + txtOffset + length);
    buffer.position(AADLength + txtOffset);
    cipher.doFinal(buffer, outBB);
    outBB.flip(); // cipher text in outBB

    // next, generate cipherText again in the same buffer
    Cipher anotherCipher = createCipher(mode, params);
    buffer.flip();
    buffer.limit(AADLength);
    anotherCipher.updateAAD(buffer);
    buffer.limit(AADLength + txtOffset + length);
    buffer.position(AADLength + txtOffset);

    // share textBuf context
    ByteBuffer buf2 = buffer.duplicate();
    buf2.limit(AADLength + txtOffset + anotherCipher.getOutputSize(length));
    int dataProcessed2 = anotherCipher.doFinal(buffer, buf2);
    buf2.position(AADLength + txtOffset);
    buf2.limit(AADLength + txtOffset + dataProcessed2);

    if (!buf2.equals(outBB)) {
        throw new RuntimeException(
                "Two results are not equal, mode:" + mode);
    }
}
 
Example 10
Project: openjdk-jdk10   File: SameBuffer.java   View Source Code Vote up 5 votes
private void runGCMWithSeparateBuffers(int mode, ByteBuffer buffer,
        ByteBuffer textBB, int txtOffset, int dataLength,
        AlgorithmParameters params) throws Exception {
    // take offset into account
    textBB.position(txtOffset);
    textBB.mark();

    // first, generate the cipher text at an allocated buffer
    Cipher cipher = createCipher(mode, params);
    cipher.updateAAD(buffer);
    buffer.flip();
    ByteBuffer outBB = ByteBuffer.allocateDirect(
            cipher.getOutputSize(dataLength));

    cipher.doFinal(textBB, outBB);// get cipher text in outBB
    outBB.flip();

    // restore positions
    textBB.reset();

    // next, generate cipher text again in a buffer that shares content
    Cipher anotherCipher = createCipher(mode, params);
    anotherCipher.updateAAD(buffer);
    buffer.flip();
    ByteBuffer buf2 = textBB.duplicate(); // buf2 shares textBuf context
    buf2.limit(txtOffset + anotherCipher.getOutputSize(dataLength));
    int dataProcessed2 = anotherCipher.doFinal(textBB, buf2);
    buf2.position(txtOffset);
    buf2.limit(txtOffset + dataProcessed2);

    if (!buf2.equals(outBB)) {
        throw new RuntimeException(
                "Two results are not equal, mode:" + mode);
    }
}
 
Example 11
Project: openjdk-jdk10   File: SameBuffer.java   View Source Code Vote up 5 votes
private void runGCMWithSameBuffer(int mode, ByteBuffer buffer,
        int txtOffset, int length, AlgorithmParameters params)
        throws Exception {

    // allocate a separate buffer
    Cipher cipher = createCipher(mode, params);
    ByteBuffer outBB = ByteBuffer.allocateDirect(
            cipher.getOutputSize(length));

    // first, generate the cipher text at an allocated buffer
    buffer.flip();
    buffer.limit(AADLength);
    cipher.updateAAD(buffer);
    buffer.limit(AADLength + txtOffset + length);
    buffer.position(AADLength + txtOffset);
    cipher.doFinal(buffer, outBB);
    outBB.flip(); // cipher text in outBB

    // next, generate cipherText again in the same buffer
    Cipher anotherCipher = createCipher(mode, params);
    buffer.flip();
    buffer.limit(AADLength);
    anotherCipher.updateAAD(buffer);
    buffer.limit(AADLength + txtOffset + length);
    buffer.position(AADLength + txtOffset);

    // share textBuf context
    ByteBuffer buf2 = buffer.duplicate();
    buf2.limit(AADLength + txtOffset + anotherCipher.getOutputSize(length));
    int dataProcessed2 = anotherCipher.doFinal(buffer, buf2);
    buf2.position(AADLength + txtOffset);
    buf2.limit(AADLength + txtOffset + dataProcessed2);

    if (!buf2.equals(outBB)) {
        throw new RuntimeException(
                "Two results are not equal, mode:" + mode);
    }
}
 
Example 12
Project: dble   File: ByteBufferUtil.java   View Source Code Vote up 5 votes
public static boolean isPrefix(ByteBuffer prefix, ByteBuffer value) {
    if (prefix.remaining() > value.remaining()) {
        return false;
    }

    int diff = value.remaining() - prefix.remaining();
    return prefix.equals(value.duplicate().limit(value.remaining() - diff));
}
 
Example 13
Project: hadoop   File: TestOfflineEditsViewer.java   View Source Code Vote up 4 votes
/**
 * Compare two files, ignore trailing zeros at the end, for edits log the
 * trailing zeros do not make any difference, throw exception is the files are
 * not same
 *
 * @param filenameSmall first file to compare (doesn't have to be smaller)
 * @param filenameLarge second file to compare (doesn't have to be larger)
 */
private boolean filesEqualIgnoreTrailingZeros(String filenameSmall,
  String filenameLarge) throws IOException {

  ByteBuffer small = ByteBuffer.wrap(DFSTestUtil.loadFile(filenameSmall));
  ByteBuffer large = ByteBuffer.wrap(DFSTestUtil.loadFile(filenameLarge));
  // OEV outputs with the latest layout version, so tweak the old file's
  // contents to have latest version so checkedin binary files don't
  // require frequent updates
  small.put(3, (byte)NameNodeLayoutVersion.CURRENT_LAYOUT_VERSION);

  // now correct if it's otherwise
  if (small.capacity() > large.capacity()) {
    ByteBuffer tmpByteBuffer = small;
    small = large;
    large = tmpByteBuffer;
    String tmpFilename = filenameSmall;
    filenameSmall = filenameLarge;
    filenameLarge = tmpFilename;
  }

  // compare from 0 to capacity of small
  // the rest of the large should be all zeros
  small.position(0);
  small.limit(small.capacity());
  large.position(0);
  large.limit(small.capacity());

  // compares position to limit
  if (!small.equals(large)) {
    return false;
  }

  // everything after limit should be 0xFF
  int i = large.limit();
  large.clear();
  for (; i < large.capacity(); i++) {
    if (large.get(i) != FSEditLogOpCodes.OP_INVALID.getOpCode()) {
      return false;
    }
  }

  return true;
}
 
Example 14
Project: jdk8u-jdk   File: SendFailed.java   View Source Code Vote up 4 votes
static void assertSameContent(ByteBuffer bb1, ByteBuffer bb2) {
    if (!bb1.equals(bb2))
        throw new RuntimeException("Buffers are not equal; bb1: " + bb1 + ", bb2: " + bb2);
}
 
Example 15
Project: openjdk-jdk10   File: SendFailed.java   View Source Code Vote up 4 votes
static void assertSameContent(ByteBuffer bb1, ByteBuffer bb2) {
    if (!bb1.equals(bb2))
        throw new RuntimeException("Buffers are not equal; bb1: " + bb1 + ", bb2: " + bb2);
}
 
Example 16
Project: ditb   File: ParseFilter.java   View Source Code Vote up 4 votes
/**
 * Parses the filterString and constructs a filter using it
 * <p>
 * @param filterStringAsByteArray filter string given by the user
 * @return filter object we constructed
 */
public Filter parseFilterString (byte [] filterStringAsByteArray)
  throws CharacterCodingException {
  // stack for the operators and parenthesis
  Stack <ByteBuffer> operatorStack = new Stack<ByteBuffer>();
  // stack for the filter objects
  Stack <Filter> filterStack = new Stack<Filter>();

  Filter filter = null;
  for (int i=0; i<filterStringAsByteArray.length; i++) {
    if (filterStringAsByteArray[i] == ParseConstants.LPAREN) {
      // LPAREN found
      operatorStack.push(ParseConstants.LPAREN_BUFFER);
    } else if (filterStringAsByteArray[i] == ParseConstants.WHITESPACE ||
               filterStringAsByteArray[i] == ParseConstants.TAB) {
      // WHITESPACE or TAB found
      continue;
    } else if (checkForOr(filterStringAsByteArray, i)) {
      // OR found
      i += ParseConstants.OR_ARRAY.length - 1;
      reduce(operatorStack, filterStack, ParseConstants.OR_BUFFER);
      operatorStack.push(ParseConstants.OR_BUFFER);
    } else if (checkForAnd(filterStringAsByteArray, i)) {
      // AND found
      i += ParseConstants.AND_ARRAY.length - 1;
      reduce(operatorStack, filterStack, ParseConstants.AND_BUFFER);
      operatorStack.push(ParseConstants.AND_BUFFER);
    } else if (checkForSkip(filterStringAsByteArray, i)) {
      // SKIP found
      i += ParseConstants.SKIP_ARRAY.length - 1;
      reduce(operatorStack, filterStack, ParseConstants.SKIP_BUFFER);
      operatorStack.push(ParseConstants.SKIP_BUFFER);
    } else if (checkForWhile(filterStringAsByteArray, i)) {
      // WHILE found
      i += ParseConstants.WHILE_ARRAY.length - 1;
      reduce(operatorStack, filterStack, ParseConstants.WHILE_BUFFER);
      operatorStack.push(ParseConstants.WHILE_BUFFER);
    } else if (filterStringAsByteArray[i] == ParseConstants.RPAREN) {
      // RPAREN found
      if (operatorStack.empty()) {
        throw new IllegalArgumentException("Mismatched parenthesis");
      }
      ByteBuffer argumentOnTopOfStack = operatorStack.peek();
      while (!(argumentOnTopOfStack.equals(ParseConstants.LPAREN_BUFFER))) {
        filterStack.push(popArguments(operatorStack, filterStack));
        if (operatorStack.empty()) {
          throw new IllegalArgumentException("Mismatched parenthesis");
        }
        argumentOnTopOfStack = operatorStack.pop();
      }
    } else {
      // SimpleFilterExpression found
      byte [] filterSimpleExpression = extractFilterSimpleExpression(filterStringAsByteArray, i);
      i+= (filterSimpleExpression.length - 1);
      filter = parseSimpleFilterExpression(filterSimpleExpression);
      filterStack.push(filter);
    }
  }

  // Finished parsing filterString
  while (!operatorStack.empty()) {
    filterStack.push(popArguments(operatorStack, filterStack));
  }
  filter = filterStack.pop();
  if (!filterStack.empty()) {
    throw new IllegalArgumentException("Incorrect Filter String");
  }
  return filter;
}