下面列出了java.nio.ByteBuffer#rewind ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static byte[] getCompositeId(final GeoWaveKey key) {
if ((key.getSortKey() == null) && (key.getPartitionKey() == null)) {
// this is a data ID key
return key.getDataId();
}
final ByteBuffer buffer =
ByteBuffer.allocate(
(key.getPartitionKey() == null ? 0 : key.getPartitionKey().length)
+ key.getSortKey().length
+ key.getDataId().length
+ VarintUtils.unsignedIntByteLength(key.getAdapterId() & 0xFFFF)
+ VarintUtils.unsignedIntByteLength(key.getDataId().length)
+ VarintUtils.unsignedIntByteLength(key.getNumberOfDuplicates()));
if (key.getPartitionKey() != null) {
buffer.put(key.getPartitionKey());
}
buffer.put(key.getSortKey());
VarintUtils.writeUnsignedIntReversed(key.getAdapterId() & 0xFFFF, buffer);
buffer.put(key.getDataId());
VarintUtils.writeUnsignedIntReversed(key.getDataId().length, buffer);
VarintUtils.writeUnsignedIntReversed(key.getNumberOfDuplicates(), buffer);
buffer.rewind();
return buffer.array();
}
@Test
public void testReadUint64() throws AtomLayerException {
ByteBuffer b = ByteBuffer.allocate(8);
Random r = new Random(1);
for (int test_number = 0; test_number < 1000; test_number++) {
long value = r.nextLong();
b.rewind();
b.putLong(value);
for (int i = 1; i <= 8; i++) {
b.rewind();
assertEquals((value >>> (8 * (8 - i))), UnsignedLexVarint.readUInt64(b, i));
}
}
}
@Test
public void testDump() throws Exception {
ByteBuffer bb = struct.encoder()
.string("name", "joe")
.structs("mapEntry").add()
.string("key", "1")
.enm("type", Type.STRING)
.string("string", "one")
.end()
.add()
.string("key", "2")
.string("string", "two")
.end()
.end()
.int64("id", 999L)
.encode();
bb.rewind();
struct.dump(bb, new PrintStream(new ByteArrayOutputStream()));
}
/**
* Parses the next UTF-16 string
*
* @return an UTF-8 string
* @throws IOException
* if error occurred
*/
public String parseUtf16String() throws IOException {
int nchars = readShort();
if ((nchars & 0x8000) != 0) {
nchars = ((nchars & 0x7fff) << 16) | (readShort() & 0xffff);
}
final ByteBuffer data = ByteBuffer.allocate(nchars * 2).order(ByteOrder.LITTLE_ENDIAN);
read(data);
data.rewind();
final int terminator = readChar();
if (0 != terminator) {
throw new AaptException(String.format("Zero terminator expected at position %d, buf 0x%04x found", (tell() - 2), terminator));
}
return StandardCharsets.UTF_16LE.decode(data).toString();
}
private Credentials parseCredentials(ContainerLaunchContext launchContext)
throws IOException {
Credentials credentials = new Credentials();
// //////////// Parse credentials
ByteBuffer tokens = launchContext.getTokens();
if (tokens != null) {
DataInputByteBuffer buf = new DataInputByteBuffer();
tokens.rewind();
buf.reset(tokens);
credentials.readTokenStorageStream(buf);
if (LOG.isDebugEnabled()) {
for (Token<? extends TokenIdentifier> tk : credentials.getAllTokens()) {
LOG.debug(tk.getService() + " = " + tk.toString());
}
}
}
// //////////// End of parsing credentials
return credentials;
}
@Override protected void _fillDirectByteBuffer(ByteBuffer bb) {
if (this.bytes != null) {
this.bytes.rewind();
if (this.bytes.isDirect()) {
_copyPixels(bb, this.bytes, getWidth() * getHeight());
} else {
bb.put(this.bytes);
}
this.bytes.rewind();
} else {
this.ints.rewind();
if (this.ints.isDirect()) {
_copyPixels(bb, this.ints, getWidth() * getHeight());
} else {
for (int i = 0; i < this.ints.capacity(); i++) {
int data = this.ints.get();
bb.put((byte)((data) & 0xff));
bb.put((byte)((data >> 8) & 0xff));
bb.put((byte)((data >> 16) & 0xff));
bb.put((byte)((data >> 24) & 0xff));
}
}
this.ints.rewind();
}
bb.rewind();
}
public void doStuff(boolean last, ByteBuffer b, Buffers.Allocation alloc) {
synchronized(this) {
if (closed) {
if (logger.isInfoEnabled()) {
logger.info("Transfer done. De-allocating one unused buffer");
}
if (alloc != null) {
buffers.free(alloc);
}
return;
}
}
if (alloc != null) {
bufferCreated(alloc);
}
try {
channel.read(b);
b.limit(b.position());
b.rewind();
bufferRead(b);
}
catch (Exception ex) {
error(b, ex);
}
}
@Override
public byte[] exchange(byte[] command) {
ByteArrayOutputStream response = new ByteArrayOutputStream();
byte[] responseData = null;
int offset = 0;
if (debug) {
Timber.d("=> %s", Dump.dump(command));
}
command = LedgerHelper.wrapCommandAPDU(LEDGER_DEFAULT_CHANNEL, command, HID_BUFFER_SIZE);
UsbRequest requestOut = new UsbRequest();
requestOut.initialize(connection, out);
while (offset != command.length) {
int blockSize = (command.length - offset > HID_BUFFER_SIZE ? HID_BUFFER_SIZE : command.length - offset);
System.arraycopy(command, offset, transferBuffer, 0, blockSize);
requestOut.queue(ByteBuffer.wrap(transferBuffer), HID_BUFFER_SIZE);
connection.requestWait();
offset += blockSize;
}
requestOut.close();
ByteBuffer responseBuffer = ByteBuffer.allocate(HID_BUFFER_SIZE);
UsbRequest requestIn = new UsbRequest();
requestIn.initialize(connection, in);
while ((responseData = LedgerHelper.unwrapResponseAPDU(LEDGER_DEFAULT_CHANNEL, response.toByteArray(), HID_BUFFER_SIZE)) == null) {
responseBuffer.clear();
requestIn.queue(responseBuffer, HID_BUFFER_SIZE);
connection.requestWait();
responseBuffer.rewind();
responseBuffer.get(transferBuffer, 0, HID_BUFFER_SIZE);
response.write(transferBuffer, 0, HID_BUFFER_SIZE);
}
requestIn.close();
if (debug) {
Timber.d("<= %s", Dump.dump(responseData));
}
return responseData;
}
@Test
public void testStructWithFp64() throws Exception {
Struct struct = StructBuilder.newStructBuilder()
.fp64("x", 1)
.fp64s("y", 2)
.fp64("z", 3)
.build();
ByteBuffer encoded = struct.encoder()
.fp64("x", -1.0)
.fp64s("y")
.value(1.0)
.value(-0.123)
.value(Double.NaN)
.value(Double.NEGATIVE_INFINITY)
.value(Double.POSITIVE_INFINITY)
.end()
.fp64("z", 2.0)
.encode();
encoded.rewind();
StructDecoder<Void> decoder = struct.decoder(encoded);
assertThat(decoder.fp64("x"), is(-1.0));
ArrayDecoder<Double, StructDecoder<Void>> ad = decoder.fp64s("y");
assertThat(ad.length(), is(5));
assertThat(ad.value(), is(1.0));
assertThat(ad.value(), is(-0.123));
assertThat(ad.value(), is(Double.NaN));
assertThat(ad.value(), is(Double.NEGATIVE_INFINITY));
assertThat(ad.value(), is(Double.POSITIVE_INFINITY));
ad.end();
assertThat(decoder.fp64("z"), is(2.0));
}
@Test
public void testUnsignedVarint64Comparison() {
byte[] imax = UnsignedLexVarint.writeVarUInt64(Long.MAX_VALUE);
byte[] imax2 = UnsignedLexVarint.writeVarUInt64(-1);
assertTrue(Lexicographic.compare(imax, imax2) < 0);
int numtests = 100;
for (int sizea = 1; sizea <= 9; sizea++) {
ByteBuffer bufa = ByteBuffer.allocate(sizea);
for (int sizeb = sizea; sizeb <= 9; sizeb++) {
ByteBuffer bufb = ByteBuffer.allocate(sizeb);
for (int i = 0; i < numtests; i++) {
long a = generate(sizea);
long b = generate(sizeb);
bufa.rewind();
assertEquals(sizea, UnsignedLexVarint.writeLexVarUInt64(bufa, a));
bufb.rewind();
assertEquals(sizeb, UnsignedLexVarint.writeLexVarUInt64(bufb, b));
boolean a_smaller = a >= 0 ? (b < 0 || a < b) : (b < 0 && a < b);
assertEquals(a == b, Lexicographic.compare(bufa.array(), bufb.array()) == 0);
assertEquals(a_smaller, Lexicographic.compare(bufa.array(), bufb.array()) < 0);
assertEquals(a_smaller, Lexicographic.compare(bufb.array(), bufa.array()) > 0);
}
}
}
}
/** {@inheritDoc} */
@Override public FileIO create(File file, OpenOption... modes) throws IOException {
FileIO fileIO = delegateFactory.create(file, modes);
return new FileIODecorator(fileIO) {
@Override public int write(ByteBuffer srcBuf, long position) throws IOException {
int type = PageIO.getType(srcBuf);
AbstractDataLeafIO dataLeafIO = null;
if (type == PageIO.T_DATA_REF_LEAF)
dataLeafIO = DataLeafIO.VERSIONS.latest();
if (type == PageIO.T_DATA_REF_MVCC_LEAF)
dataLeafIO = MvccDataLeafIO.VERSIONS.latest();
if (dataLeafIO != null) {
long pageAddr = GridUnsafe.bufferAddress(srcBuf);
int itemIdx = dataLeafIO.getCount(pageAddr) - 1;
linkRef.set(dataLeafIO.getLink(pageAddr, itemIdx));
fileRef.set(file);
}
srcBuf.rewind();
return super.write(srcBuf, position);
}
};
}
public static byte[] array(ByteBuffer buffer) {
synchronized (buffer) {
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
buffer.rewind();
return bytes;
}
}
public static void byteBufferRGBtoARGB(ByteBuffer bgr, ByteBuffer argb) {
byte[] tmpArr = new byte[3];
for (int i = 0; i < bgr.capacity(); i += 3) {
bgr.get(tmpArr);
argb.put(tmpArr[0]);
argb.put(tmpArr[1]);
argb.put(tmpArr[2]);
argb.put((byte) 255);
}
argb.rewind();
}
private void actualDecryption(Map<String, AttributeValue> itemAttributes,
Map<String, Set<EncryptionFlags>> attributeFlags, SecretKey encryptionKey,
Map<String, String> materialDescription) throws GeneralSecurityException {
final String encryptionMode = encryptionKey != null ? encryptionKey.getAlgorithm() +
materialDescription.get(symmetricEncryptionModeHeader) : null;
Cipher cipher = null;
int blockSize = -1;
for (Map.Entry<String, AttributeValue> entry: itemAttributes.entrySet()) {
Set<EncryptionFlags> flags = attributeFlags.get(entry.getKey());
if (flags != null && flags.contains(EncryptionFlags.ENCRYPT)) {
if (!flags.contains(EncryptionFlags.SIGN)) {
throw new IllegalArgumentException("All encrypted fields must be signed. Bad field: " + entry.getKey());
}
ByteBuffer plainText;
ByteBuffer cipherText = entry.getValue().b().asByteBuffer();
cipherText.rewind();
if (encryptionKey instanceof DelegatedKey) {
plainText = ByteBuffer.wrap(((DelegatedKey)encryptionKey).decrypt(toByteArray(cipherText), null, encryptionMode));
} else {
if (cipher == null) {
blockSize = getBlockSize(encryptionMode);
cipher = Cipher.getInstance(encryptionMode);
}
byte[] iv = new byte[blockSize];
cipherText.get(iv);
cipher.init(Cipher.DECRYPT_MODE, encryptionKey, new IvParameterSpec(iv), Utils.getRng());
plainText = ByteBuffer.allocate(cipher.getOutputSize(cipherText.remaining()));
cipher.doFinal(cipherText, plainText);
plainText.rewind();
}
entry.setValue(AttributeValueMarshaller.unmarshall(plainText));
}
}
}
@Test
public void testByteBufferToHeapBuffer() {
ByteBuffer byteBuffer = ByteBuffer.allocate(8);
byteBuffer.putLong(10);
byteBuffer.rewind();
HeapBuffer directBuffer = HeapBuffer.wrap(byteBuffer.array());
assertEquals(directBuffer.readLong(), byteBuffer.getLong());
}
/**
* libsecp256k1 Compute Pubkey - computes public key from secret key
*
* @param seckey ECDSA Secret key, 32 bytes
* @return pubkey ECDSA Public key, 33 or 65 bytes
*/
// TODO add a 'compressed' arg
public static byte[] computePubkey(byte[] seckey) throws AssertFailException {
Preconditions.checkArgument(seckey.length == 32);
ByteBuffer byteBuff = nativeECDSABuffer.get();
if (byteBuff == null || byteBuff.capacity() < seckey.length) {
byteBuff = ByteBuffer.allocateDirect(seckey.length);
byteBuff.order(ByteOrder.nativeOrder());
nativeECDSABuffer.set(byteBuff);
}
byteBuff.rewind();
byteBuff.put(seckey);
byte[][] retByteArray;
r.lock();
try {
retByteArray = secp256k1_ec_pubkey_create(byteBuff, Secp256k1Context.getContext());
} finally {
r.unlock();
}
byte[] pubArr = retByteArray[0];
int pubLen = new BigInteger(new byte[] { retByteArray[1][0] }).intValue();
int retVal = new BigInteger(new byte[] { retByteArray[1][1] }).intValue();
assertEquals(pubArr.length, pubLen, "Got bad pubkey length.");
return retVal == 0 ? new byte[0] : pubArr;
}
/**
* Write a page (or part of a page) to disk
* @param page Page to write
* @param pageNumber Page number to write the page to
* @param pageOffset offset within the page at which to start writing the
* page data
*/
public void writePage(ByteBuffer page, int pageNumber, int pageOffset)
throws IOException
{
assertWriting();
validatePageNumber(pageNumber);
page.rewind().position(pageOffset);
int writeLen = page.remaining();
if((writeLen + pageOffset) > getFormat().PAGE_SIZE) {
throw new IllegalArgumentException(
"Page buffer is too large, size " + (writeLen + pageOffset));
}
ByteBuffer encodedPage = page;
if(pageNumber == 0) {
// re-mask header
applyHeaderMask(page);
} else {
if(!_codecHandler.canEncodePartialPage()) {
if((pageOffset > 0) && (writeLen < getFormat().PAGE_SIZE)) {
// current codec handler cannot encode part of a page, so need to
// copy the modified part into the current page contents in a temp
// buffer so that we can encode the entire page
ByteBuffer fullPage = _fullPageEncodeBufferH.setPage(
this, pageNumber);
// copy the modified part to the full page
fullPage.position(pageOffset);
fullPage.put(page);
fullPage.rewind();
// reset so we can write the whole page
page = fullPage;
pageOffset = 0;
writeLen = getFormat().PAGE_SIZE;
} else {
_fullPageEncodeBufferH.possiblyInvalidate(pageNumber, null);
}
}
// re-encode page
encodedPage = _codecHandler.encodePage(page, pageNumber, pageOffset);
// reset position/limit in case they were affected by encoding
encodedPage.position(pageOffset).limit(pageOffset + writeLen);
}
try {
_channel.write(encodedPage, (getPageOffset(pageNumber) + pageOffset));
} finally {
if(pageNumber == 0) {
// de-mask header
applyHeaderMask(page);
}
}
}
/**
* Reads a binary STL file.
*
* @param is InputStream to read from.
* @param skip Number of bytes to skip before reading the STL file.
* @return Mesh
* @throws IOException If an error occurs during reading.
*/
private Mesh readBinary(InputStream is, int skip) throws IOException {
/* Prepare a ByteBuffer to read the rest of the STL file. */
byte[] bytes = new byte[50];
ByteBuffer buffer = ByteBuffer.wrap(bytes);
buffer.order(ByteOrder.LITTLE_ENDIAN);
/* Skip the STL header! */
is.skip(skip);
/* Read the bytes for the size (unsigned 32 bit int, little-endian). */
byte[] sizeBytes = new byte[4];
is.read(sizeBytes, 0, 4);
long triangles = ((sizeBytes[0] & 0xFF)) | ((sizeBytes[1] & 0xFF) << 8) | ((sizeBytes[2] & 0xFF) << 16) | ((sizeBytes[3] & 0xFF) << 24);
/* TODO: Properly handle models whose triangles > MAX_TRIANGLES. */
if (triangles <= 0) {
LOGGER.error("The number of triangles in the Mesh seems to be smaller than zero. This STL file is probably corrupt!");
return null;
} else if (triangles > MAX_TRIANGLES) {
LOGGER.error("The number of triangles in the Mesh exceeds the limit that can currently be processed by STLMeshDecoder. The Mesh will be downsampled!");
return null;
}
/* Prepare Mesh. */
Mesh mesh = new Mesh((int)triangles, (int)triangles);
/* Prepare helper structures. */
TObjectIntHashMap<Vector3f> vertexBuffer = new TObjectIntHashMap<>();
int index = 0;
int[] vertexindices = new int[3];
/* Now add all triangles. */
for (int i=0; i<triangles; i++) {
/* Ready 48 bytes from the stream. */
buffer.rewind();
is.read(bytes);
/* Read and ignore three floats. */
buffer.getFloat();
buffer.getFloat();
buffer.getFloat();
/* Add the vertices and the vertex-normal to the mesh. */
for (int vidx = 0; vidx < 3; vidx++) {
Vector3f vertex = new Vector3f(buffer.getFloat(), buffer.getFloat(), buffer.getFloat());
if (!vertexBuffer.containsKey(vertex)) {
mesh.addVertex(vertex);
vertexBuffer.put(vertex, index);
index++;
}
vertexindices[vidx] = vertexBuffer.get(vertex);
}
/* Add a new face to the Mesh. */
if (!mesh.addFace(new Vector3i(vertexindices[0], vertexindices[1], vertexindices[2]))) {
LOGGER.warn("Could not add face {}/{}/{} because index points to non-existing vertex.", vertexindices[0], vertexindices[1], vertexindices[2]);
}
}
/* Closes the InputStream. */
is.close();
return mesh;
}
/**
* Gives the row count for the given column family and column qualifier, in
* the given row range as defined in the Scan object.
*/
@Override
public void getRowNum(RpcController controller, AggregateRequest request,
RpcCallback<AggregateResponse> done) {
AggregateResponse response = null;
long counter = 0L;
List<Cell> results = new ArrayList<>();
InternalScanner scanner = null;
try {
Scan scan = ProtobufUtil.toScan(request.getScan());
byte[][] colFamilies = scan.getFamilies();
byte[] colFamily = colFamilies != null ? colFamilies[0] : null;
NavigableSet<byte[]> qualifiers = colFamilies != null ?
scan.getFamilyMap().get(colFamily) : null;
byte[] qualifier = null;
if (qualifiers != null && !qualifiers.isEmpty()) {
qualifier = qualifiers.pollFirst();
}
if (scan.getFilter() == null && qualifier == null) {
scan.setFilter(new FirstKeyOnlyFilter());
}
scanner = env.getRegion().getScanner(scan);
boolean hasMoreRows = false;
do {
hasMoreRows = scanner.next(results);
if (results.size() > 0) {
counter++;
}
results.clear();
} while (hasMoreRows);
ByteBuffer bb = ByteBuffer.allocate(8).putLong(counter);
bb.rewind();
response = AggregateResponse.newBuilder().addFirstPart(
ByteString.copyFrom(bb)).build();
} catch (IOException e) {
CoprocessorRpcUtils.setControllerException(controller, e);
} finally {
if (scanner != null) {
try {
scanner.close();
} catch (IOException ignored) {}
}
}
log.info("Row counter from this region is "
+ env.getRegion().getRegionInfo().getRegionNameAsString() + ": " + counter);
done.run(response);
}
@Test
public void testReloadInvalidCheckSizeOnTransaction() throws Exception {
final int JOURNAL_SIZE = 2000;
setupAndLoadJournal(JOURNAL_SIZE, 100);
Assert.assertEquals(2, factory.listFiles("tt").size());
Assert.assertEquals(0, records.size());
Assert.assertEquals(0, transactions.size());
for (int i = 0; i < 2; i++) {
journalImpl.appendAddRecordTransactional(1L, i, (byte) 0, new SimpleEncoding(1, (byte) 15));
}
journalImpl.appendCommitRecord(1L, false);
journalImpl.debugWait();
log.debug("Files = " + factory.listFiles("tt"));
SequentialFile file = factory.createSequentialFile("tt-1.tt");
file.open();
ByteBuffer buffer = ByteBuffer.allocate(100);
// Messing up with the first record (removing the position)
file.position(100);
file.read(buffer);
// jumping RecordType, FileId, TransactionID, RecordID, VariableSize,
// RecordType, RecordBody (that we know it is 1 )
buffer.position(1 + 4 + 8 + 8 + 4 + 1 + 1 + 1);
int posCheckSize = buffer.position();
Assert.assertEquals(JournalImpl.SIZE_ADD_RECORD_TX + 2, buffer.getInt());
buffer.position(posCheckSize);
buffer.putInt(-1);
buffer.rewind();
// Changing the check bufferSize, so reload will ignore this record
file.position(100);
file.writeDirect(buffer, true);
file.close();
setupAndLoadJournal(JOURNAL_SIZE, 100);
Assert.assertEquals(0, records.size());
journalImpl.checkReclaimStatus();
Assert.assertEquals(0, journalImpl.getDataFilesCount());
Assert.assertEquals(2, factory.listFiles("tt").size());
}