本文整理汇总了Java中java.nio.ByteBuffer.allocateDirect方法的典型用法代码示例。如果您正苦于以下问题:Java ByteBuffer.allocateDirect方法的具体用法?Java ByteBuffer.allocateDirect怎么用?Java ByteBuffer.allocateDirect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.ByteBuffer
的用法示例。
在下文中一共展示了ByteBuffer.allocateDirect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: toArrayDirectByteBuffer
import java.nio.ByteBuffer; //导入方法依赖的package包/类
@Test
public void toArrayDirectByteBuffer() {
byte[] input = {0, 1, 2, 3, 4};
ByteBuffer buffer = ByteBuffer.allocateDirect(5);
buffer.put(input);
buffer.rewind();
assertArrayEquals(input, Utils.toArray(buffer));
assertEquals(0, buffer.position());
assertArrayEquals(new byte[] {1, 2}, Utils.toArray(buffer, 1, 2));
assertEquals(0, buffer.position());
buffer.position(2);
assertArrayEquals(new byte[] {2, 3, 4}, Utils.toArray(buffer));
assertEquals(2, buffer.position());
}
示例2: getFloatBuffer
import java.nio.ByteBuffer; //导入方法依赖的package包/类
public static Buffer getFloatBuffer(float[] array) {
// Each float takes 4 bytes
ByteBuffer byteBuffer = ByteBuffer.allocateDirect((Float.SIZE / Byte.SIZE) * array.length);
byteBuffer.order(ByteOrder.nativeOrder());
for (float d : array)
byteBuffer.putFloat(d);
byteBuffer.rewind();
return byteBuffer;
}
示例3: writeBytes
import java.nio.ByteBuffer; //导入方法依赖的package包/类
@Override
public int writeBytes(ScatteringByteChannel ch, int max) throws IOException {
ByteBuffer tmp = ByteBuffer.allocateDirect(max);
int length = ch.read(tmp);
writeBytes(tmp);
return length;
}
示例4: allocate
import java.nio.ByteBuffer; //导入方法依赖的package包/类
@Override
public ByteBuffer allocate(int bufferLen) {
if (bufferLen > overallBuffer.capacity() - overallBuffer.position()) {
// If no available space for the requested length, then allocate new
return isUsingDirect() ? ByteBuffer.allocateDirect(bufferLen) :
ByteBuffer.allocate(bufferLen);
}
overallBuffer.limit(overallBuffer.position() + bufferLen);
ByteBuffer result = overallBuffer.slice();
overallBuffer.position(overallBuffer.position() + bufferLen);
return result;
}
示例5: getRenderBufferBitmap
import java.nio.ByteBuffer; //导入方法依赖的package包/类
private Bitmap getRenderBufferBitmap() {
ByteBuffer buffer = ByteBuffer.allocateDirect(renderBufferWidth * renderBufferHeight * 4);
GLES20.glReadPixels(0, 0, renderBufferWidth, renderBufferHeight, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buffer);
Bitmap bitmap = Bitmap.createBitmap(renderBufferWidth, renderBufferHeight, Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(buffer);
return bitmap;
}
示例6: pubKeyTweakAdd
import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
* libsecp256k1 PubKey Tweak-Add - Tweak pubkey by adding to it
*
* @param tweak some bytes to tweak with
* @param pubkey 32-byte seckey
*/
public static byte[] pubKeyTweakAdd(byte[] pubkey, byte[] tweak) throws AssertFailException {
Preconditions.checkArgument(pubkey.length == 33 || pubkey.length == 65);
ByteBuffer byteBuff = nativeECDSABuffer.get();
if (byteBuff == null || byteBuff.capacity() < pubkey.length + tweak.length) {
byteBuff = ByteBuffer.allocateDirect(pubkey.length + tweak.length);
byteBuff.order(ByteOrder.nativeOrder());
nativeECDSABuffer.set(byteBuff);
}
byteBuff.rewind();
byteBuff.put(pubkey);
byteBuff.put(tweak);
byte[][] retByteArray;
r.lock();
try {
retByteArray = secp256k1_pubkey_tweak_add(byteBuff, Secp256k1Context.getContext(), pubkey.length);
} finally {
r.unlock();
}
byte[] pubArr = retByteArray[0];
int pubLen = (byte) new BigInteger(new byte[] { retByteArray[1][0] }).intValue() & 0xFF;
int retVal = new BigInteger(new byte[] { retByteArray[1][1] }).intValue();
assertEquals(pubArr.length, pubLen, "Got bad pubkey length.");
assertEquals(retVal, 1, "Failed return value check.");
return pubArr;
}
示例7: getAllocateDirect
import java.nio.ByteBuffer; //导入方法依赖的package包/类
public void getAllocateDirect() {
ByteBuffer buffer = ByteBuffer.allocateDirect(4096);
byte[] b = new byte[1024];
int count = 1000000;
System.currentTimeMillis();
long t1 = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
buffer.position(0);
buffer.get(b);
}
long t2 = System.currentTimeMillis();
System.out.println("take time:" + (t2 - t1) + " ms.(Get:allocateDirect)");
}
示例8: AprServletOutputStream
import java.nio.ByteBuffer; //导入方法依赖的package包/类
public AprServletOutputStream(SocketWrapper<Long> wrapper,
int asyncWriteBufferSize, AprEndpoint endpoint) {
super(asyncWriteBufferSize);
this.endpoint = endpoint;
this.wrapper = wrapper;
this.socket = wrapper.getSocket().longValue();
if (endpoint.isSSLEnabled()) {
sslOutputBuffer = ByteBuffer.allocateDirect(SSL_OUTPUT_BUFFER_SIZE);
sslOutputBuffer.position(SSL_OUTPUT_BUFFER_SIZE);
} else {
sslOutputBuffer = null;
}
}
示例9: doTestWithSeparatedBuffer
import java.nio.ByteBuffer; //导入方法依赖的package包/类
private void doTestWithSeparatedBuffer(int offset,
AlgorithmParameters params) throws Exception {
// prepare AAD byte buffers to test
byte[] AAD = Helper.generateBytes(AADLength);
ByteBuffer AAD_Buf = ByteBuffer.allocate(AADLength);
AAD_Buf.put(AAD, 0, AAD.length);
AAD_Buf.flip();
// prepare text byte buffer to encrypt/decrypt
Cipher c = createCipher(Cipher.ENCRYPT_MODE, params);
int outputLength = c.getOutputSize(textLength);
int outputBufSize = outputLength + offset;
byte[] inputText = Helper.generateBytes(outputBufSize);
ByteBuffer plainTextBB = ByteBuffer.allocateDirect(inputText.length);
plainTextBB.put(inputText);
plainTextBB.position(offset);
plainTextBB.limit(offset + textLength);
// do test
runGCMWithSeparateBuffers(Cipher.ENCRYPT_MODE, AAD_Buf, plainTextBB, offset,
textLength, params);
int tagLength = c.getParameters()
.getParameterSpec(GCMParameterSpec.class).getTLen() / 8;
plainTextBB.position(offset);
plainTextBB.limit(offset + textLength + tagLength);
runGCMWithSeparateBuffers(Cipher.DECRYPT_MODE, AAD_Buf, plainTextBB, offset,
textLength + tagLength, params);
}
示例10: snapshotToBuffer
import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
* Convert the timeseries snapshot into a run of (x, y, z) bytes.
*/
private FloatBuffer snapshotToBuffer(TimeSeriesSnapshot snapshot) {
float[] asFloats = snapshotToFloat(snapshot);
// Create buffer to store result.
ByteBuffer bb = ByteBuffer.allocateDirect(snapshot.length * VERTEX_STRIDE);
bb.order(ByteOrder.nativeOrder());
// Write to buffer usign floats:
FloatBuffer vertexBuffer = bb.asFloatBuffer();
vertexBuffer.put(asFloats);
vertexBuffer.position(0);
return vertexBuffer;
}
示例11: takeScreenSnapshot
import java.nio.ByteBuffer; //导入方法依赖的package包/类
public Bitmap takeScreenSnapshot(GL10 gl) {
int width = (int) mdChartWindowWidth;
int height = (int) mdChartWindowHeight;
int screenshotSize = width * height;
int pixelsBuffer[] = new int[screenshotSize];
try {
ByteBuffer bb = ByteBuffer.allocateDirect(screenshotSize * 4);
bb.order(ByteOrder.nativeOrder());
// this statement does not work in the landscape mode of galaxy express. Seems that it cannot support so large height (lines).
//gl.glReadPixels(0, 0, width, height, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, bb);
gl.glReadPixels(0, 0, width, height/2, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, bb);
bb.asIntBuffer().get(pixelsBuffer);
gl.glReadPixels(0, height/2, width, height/2, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, bb);
int pixelsBufferBottom[] = new int[screenshotSize];
bb.asIntBuffer().get(pixelsBufferBottom);
for (int idx = screenshotSize/2; idx < screenshotSize; idx ++) {
pixelsBuffer[idx] = pixelsBufferBottom[idx - screenshotSize/2];
}
bb = null;
} catch (OutOfMemoryError e) {
// ByteBuffer.allocateDirect(screenshotSize * 4); could be out of memory
// in general, OutOfMemoryError should not be handled and developer should let JVM die.
// however, in this case, we know what statement causes the out of memory error
// and other threads are not affected. So it is ok to catch it.
}
for (int i = 0; i < screenshotSize; ++i) {
// The alpha and green channels' positions are preserved while the red and blue are swapped
pixelsBuffer[i] = ((pixelsBuffer[i] & 0xff00ff00)) | ((pixelsBuffer[i] & 0x000000ff) << 16) | ((pixelsBuffer[i] & 0x00ff0000) >> 16);
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixelsBuffer, screenshotSize-width, -width, 0, 0, width, height);
return bitmap;
}
示例12: createShortBuffer
import java.nio.ByteBuffer; //导入方法依赖的package包/类
public static ShortBuffer createShortBuffer(short[] coords) {
ByteBuffer bb = ByteBuffer.allocateDirect(coords.length * SIZEOF_SHORT);
bb.order(ByteOrder.nativeOrder());
ShortBuffer ib = bb.asShortBuffer();
ib.put(coords);
ib.position(0);
return ib;
}
示例13: ZlibCompressor
import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
* Creates a new compressor using the specified compression level.
* Compressed data will be generated in ZLIB format.
*
* @param level Compression level #CompressionLevel
* @param strategy Compression strategy #CompressionStrategy
* @param header Compression header #CompressionHeader
* @param directBufferSize Size of the direct buffer to be used.
*/
public ZlibCompressor(CompressionLevel level, CompressionStrategy strategy,
CompressionHeader header, int directBufferSize) {
this.level = level;
this.strategy = strategy;
this.windowBits = header;
stream = init(this.level.compressionLevel(),
this.strategy.compressionStrategy(),
this.windowBits.windowBits());
this.directBufferSize = directBufferSize;
uncompressedDirectBuf = ByteBuffer.allocateDirect(directBufferSize);
compressedDirectBuf = ByteBuffer.allocateDirect(directBufferSize);
compressedDirectBuf.position(directBufferSize);
}
示例14: testTensorToImgDirect
import java.nio.ByteBuffer; //导入方法依赖的package包/类
/** Tests the img<Type>Direct(Tensor) functions */
@Test
public void testTensorToImgDirect() {
final long[] shape = new long[] { 20, 10, 3 };
final int[] mapping = new int[] { 0, 1, 2 };
final int n = shape.length;
final int size = 600;
// Get some points to mark
List<Point> points = createTestPoints(n);
// Create Tensors of different type and convert them to images
ByteBuffer dataByte = ByteBuffer.allocateDirect(size);
execForPointsWithBufferIndex(shape, mapping, points, (i, v) -> dataByte.put(i, (byte) (int) v));
Tensor tensorByte = Tensor.create(DataType.UINT8, shape, dataByte);
Img<ByteType> imgByte = Tensors.imgByteDirect(tensorByte);
DoubleBuffer dataDouble = DoubleBuffer.allocate(size);
execForPointsWithBufferIndex(shape, mapping, points, (i, v) -> dataDouble.put(i, v));
Tensor tensorDouble = Tensor.create(shape, dataDouble);
Img<DoubleType> imgDouble = Tensors.imgDoubleDirect(tensorDouble);
FloatBuffer dataFloat = FloatBuffer.allocate(size);
execForPointsWithBufferIndex(shape, mapping, points, (i, v) -> dataFloat.put(i, v));
Tensor tensorFloat = Tensor.create(shape, dataFloat);
Img<FloatType> imgFloat = Tensors.imgFloatDirect(tensorFloat);
IntBuffer dataInt = IntBuffer.allocate(size);
execForPointsWithBufferIndex(shape, mapping, points, (i, v) -> dataInt.put(i, v));
Tensor tensorInt = Tensor.create(shape, dataInt);
Img<IntType> imgInt = Tensors.imgIntDirect(tensorInt);
LongBuffer dataLong = LongBuffer.allocate(size);
execForPointsWithBufferIndex(shape, mapping, points, (i, v) -> dataLong.put(i, v));
Tensor tensorLong = Tensor.create(shape, dataLong);
Img<LongType> imgLong = Tensors.imgLongDirect(tensorLong);
// Check all created images
checkImage(imgByte, n, shape, points);
checkImage(imgDouble, n, shape, points);
checkImage(imgFloat, n, shape, points);
checkImage(imgInt, n, shape, points);
checkImage(imgLong, n, shape, points);
}
示例15: Sink
import java.nio.ByteBuffer; //导入方法依赖的package包/类
Sink(AsynchronousByteChannel channel) {
this.channel = channel;
int size = 1024 + rand.nextInt(10000);
this.readBuffer = (rand.nextBoolean()) ?
ByteBuffer.allocateDirect(size) : ByteBuffer.allocate(size);
}