Java Code Examples for org.apache.ratis.thirdparty.com.google.protobuf.ByteString#asReadOnlyByteBuffer()

The following examples show how to use org.apache.ratis.thirdparty.com.google.protobuf.ByteString#asReadOnlyByteBuffer() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileInfo.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
private int write(long offset, ByteString data, boolean close) throws IOException {
  // If leader finish write data with offset = 4096 and writeSize become 8192,
  // and 2 follower has not written the data with offset = 4096,
  // then start a leader election. So client will retry send the data with offset = 4096,
  // then offset < writeSize in leader.
  if (offset < writeSize) {
    return data.size();
  }
  if (offset != writeSize) {
    throw new IOException("Offset/size mismatched: offset = " + offset
        + " != writeSize = " + writeSize + ", path=" + getRelativePath());
  }
  if (out == null) {
    throw new IOException("File output is not initialized, path=" + getRelativePath());
  }

  synchronized (out) {
    int n = 0;
    if (data != null) {
      final ByteBuffer buffer = data.asReadOnlyByteBuffer();
      try {
        for (; buffer.remaining() > 0; ) {
          n += out.write(buffer);
        }
      } finally {
        writeSize += n;
      }
    }

    if (close) {
      out.close();
    }
    return n;
  }
}
 
Example 2
Source File: FileInfo.java    From ratis with Apache License 2.0 5 votes vote down vote up
private int write(long offset, ByteString data, boolean close) throws IOException {
  if (offset != writeSize) {
    throw new IOException("Offset/size mismatched: offset = " + offset
        + " != writeSize = " + writeSize + ", path=" + getRelativePath());
  }
  if (out == null) {
    throw new IOException("File output is not initialized, path=" + getRelativePath());
  }

  synchronized (out) {
    int n = 0;
    if (data != null) {
      final ByteBuffer buffer = data.asReadOnlyByteBuffer();
      try {
        for (; buffer.remaining() > 0; ) {
          n += out.write(buffer);
        }
      } finally {
        writeSize += n;
      }
    }

    if (close) {
      out.close();
    }
    return n;
  }
}
 
Example 3
Source File: RaftId.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
private static UUID toUuid(ByteString bytes) {
  Objects.requireNonNull(bytes, "bytes == null");
  checkLength(bytes.size(), "bytes.size()");
  final ByteBuffer buf = bytes.asReadOnlyByteBuffer();
  return new UUID(buf.getLong(), buf.getLong());
}
 
Example 4
Source File: RaftId.java    From ratis with Apache License 2.0 4 votes vote down vote up
private static UUID toUuid(ByteString bytes) {
  Objects.requireNonNull(bytes, "bytes == null");
  checkLength(bytes.size(), "bytes.size()");
  final ByteBuffer buf = bytes.asReadOnlyByteBuffer();
  return new UUID(buf.getLong(), buf.getLong());
}
 
Example 5
Source File: Checksum.java    From hadoop-ozone with Apache License 2.0 2 votes vote down vote up
/**
 * Computes the ChecksumData for the input data and verifies that it
 * matches with that of the input checksumData, starting from index
 * startIndex.
 * @param byteString input data
 * @param checksumData checksumData to match with
 * @param startIndex index of first checksum in checksumData to match with
 *                   data's computed checksum.
 * @throws OzoneChecksumException is thrown if checksums do not match
 */
public static boolean verifyChecksum(ByteString byteString,
    ChecksumData checksumData, int startIndex) throws OzoneChecksumException {
  final ByteBuffer buffer = byteString.asReadOnlyByteBuffer();
  return verifyChecksum(buffer, checksumData, startIndex);
}