List of usage examples for java.nio ByteBuffer allocateDirect
public static ByteBuffer allocateDirect(int capacity)
From source file:de.ailis.threedee.utils.BufferUtils.java
/** * Converts the specified integer buffer to native endian and returns this * new buffer. If buffer is already in correct endian format then it is * returned right away./*from w ww .j a v a 2 s .c o m*/ * * @param buffer * The integer buffer to convert * @return The converted integer buffer or the source buffer if no * conversion is needed */ public static IntBuffer convertToNativeEndian(final IntBuffer buffer) { if (buffer.order() == ByteOrder.nativeOrder()) return buffer; final ByteBuffer bytes = ByteBuffer.allocateDirect(buffer.capacity()); bytes.order(ByteOrder.nativeOrder()); final IntBuffer ints = bytes.asIntBuffer(); ints.put(buffer).rewind(); return ints; }
From source file:org.neo4j.io.pagecache.impl.SingleFilePageSwapperTest.java
@Test public void swappingInMustFillPageWithData() throws IOException { byte[] bytes = new byte[] { 1, 2, 3, 4 }; StoreChannel channel = getFs().create(getFile()); channel.writeAll(wrap(bytes));//w w w .jav a 2 s. com channel.close(); PageSwapperFactory factory = swapperFactory(); PageSwapper swapper = createSwapper(factory, getFile(), 4, null, false); ByteBuffer target = ByteBuffer.allocateDirect(4); ByteBufferPage page = new ByteBufferPage(target); swapper.read(0, page); assertThat(array(target), byteArray(bytes)); }
From source file:io.hops.erasure_coding.NativeReedSolomonCode.java
@Override public void decodeBulk(byte[][] readBufs, byte[][] writeBufs, int[] erasedLocations, int[] locationsToRead, int[] locationsNotToRead) { ByteBuffer[] breadBufs = new ByteBuffer[readBufs.length]; ByteBuffer[] bwriteBufs = new ByteBuffer[locationsNotToRead.length]; int size = readBufs[0].length; int[] inputOffsets = new int[readBufs.length]; int[] outputOffsets = new int[locationsNotToRead.length]; //init parities for (int i = 0; i < paritySize; i++) { breadBufs[i + stripeSize] = ByteBuffer.allocateDirect(size); for (int j = 0; j < size; j++) { breadBufs[i + stripeSize].put(readBufs[i][j]); }//from ww w. j a v a 2s . co m breadBufs[i + stripeSize].flip(); } //init data for (int i = 0; i < stripeSize; i++) { breadBufs[i] = ByteBuffer.allocateDirect(size); for (int j = 0; j < size; j++) { breadBufs[i].put(readBufs[i + paritySize][j]); } breadBufs[i].flip(); } int[] modifiedLocationsNotRead = new int[locationsNotToRead.length]; for (int i = 0; i < locationsNotToRead.length; i++) { int location = locationsNotToRead[i]; if (location < paritySize) { //then this is parity breadBufs[location + stripeSize] = null; modifiedLocationsNotRead[i] = location + stripeSize; } else { //then this is data breadBufs[location - paritySize] = null; modifiedLocationsNotRead[i] = location - paritySize; } } Arrays.sort(modifiedLocationsNotRead); //allocate writebufs for (int i = 0; i < modifiedLocationsNotRead.length; i++) { bwriteBufs[i] = ByteBuffer.allocateDirect(size); } decoder.performDecodeImpl(breadBufs, inputOffsets, size, modifiedLocationsNotRead, bwriteBufs, outputOffsets); for (int i = 0; i < writeBufs.length; i++) { for (int j = 0; j < size; j++) { writeBufs[i][j] = bwriteBufs[i].get(); } } }
From source file:com.hadoop.compression.fourmc.zstd.ZstdStreamDecompressor.java
/** * Creates a new ZstdStreamDecompressor. * */// w w w .j a va2s . c o m public ZstdStreamDecompressor() { dStream = createDStream(); /* * Notice for developers: * Is a direct buffer pool needed here? * The iBuffSize and oBuffSize is about ~128K, which is way smaller than 4MB. Take the community Lz4Compressor * for reference, allocateDirect shall not trigger a java.lang.OutOfMemoryError: Direct Buffer Memory exception. * * We can use a direct buffer pool if needed. */ iBuff = ByteBuffer.allocateDirect(iBuffSize); oBuff = ByteBuffer.allocateDirect(oBuffSize); reset(); }
From source file:xbird.util.nio.RemoteMemoryMappedFile.java
public RemoteMemoryMappedFile(int port, String filePath, int pageSize, boolean alloc, boolean bigEndian) { this._sockAddr = new InetSocketAddress(NetUtils.getLocalHost(), port); this._filePath = filePath; this._pageSize = pageSize; this._bigEndian = bigEndian; this._maxBulkFetchSize = pageSize * (MemoryMappedDocumentTable.CACHED_PAGES / 4); this._rcvbuf = alloc ? ByteBuffer.allocateDirect(pageSize) : null; }
From source file:org.apache.xmlgraphics.util.io.Base64Test.java
/** * Returns true if the contents of <tt>is1</tt> match the contents of * <tt>is2</tt>/*from w ww .ja v a 2 s. c o m*/ * * @throws IOException */ public static boolean compareStreams(final InputStream i1, final InputStream i2, final boolean skipws) throws IOException { try (final ReadableByteChannel ch1 = Channels.newChannel(i1)) { try (final ReadableByteChannel ch2 = Channels.newChannel(i2)) { final ByteBuffer buf1 = ByteBuffer.allocateDirect(1024); final ByteBuffer buf2 = ByteBuffer.allocateDirect(1024); while (true) { final int n1 = ch1.read(buf1); final int n2 = ch2.read(buf2); if (n1 == -1 || n2 == -1) { return n1 == n2; } buf1.flip(); buf2.flip(); for (int i = 0; i < Math.min(n1, n2); ++i) { if (buf1.get() != buf2.get()) { return false; } } buf1.compact(); buf2.compact(); } } } }
From source file:com.github.c77.base_driver.kobuki.KobukiBaseDevice.java
private void updateReceivedData(final byte[] bytes) { int readBytes = bytes.length; packetReader.newPacket(ByteBuffer.allocateDirect(readBytes).put(bytes, 0, readBytes)); baseStatus = packetParser.parseBaseStatus(packetReader.getSensorPacket()); odometryStatus.update(baseStatus);/*from www . j av a2 s . com*/ }
From source file:uk.co.real_logic.aeron.tools.perf_tools.AeronLatencyUnderLoadPublisher.java
public AeronLatencyUnderLoadPublisher(final String[] args) { try {//from ww w . j av a2 s .c o m parseArgs(args); } catch (final ParseException e) { throw new RuntimeException(e); } final Aeron.Context ctx = new Aeron.Context().newConnectionHandler(this::connectionHandler); fragmentHandler = new FragmentAssemblyAdapter(this::msgHandler); final Aeron aeron = Aeron.connect(ctx); System.out.println("Reflect: " + reflectChannel + " Pub: " + pubChannel); pub = aeron.addPublication(pubChannel, pubStreamId); sub = aeron.addSubscription(reflectChannel, subStreamId); connectionLatch = new CountDownLatch(1); final IdleStrategy idle = new BusySpinIdleStrategy(); bufferClaim = new BufferClaim(); final List<RateControllerInterval> intervals = new ArrayList<>(); intervals.add(new MessagesAtMessagesPerSecondInterval(100, 10)); intervals.add(new MessagesAtMessagesPerSecondInterval(1000, 100)); intervals.add(new MessagesAtMessagesPerSecondInterval(10000, 1000)); intervals.add(new MessagesAtMessagesPerSecondInterval(100000, 10000)); intervals.add(new MessagesAtMessagesPerSecondInterval(1000000, 100000)); intervals.add(new MessagesAtMessagesPerSecondInterval(10000000, 1000000)); intervals.add(new MessagesAtMessagesPerSecondInterval(30000000, 3000000)); buffer = new UnsafeBuffer(ByteBuffer.allocateDirect(msgLen)); msgCount = 0; RateController rateController = null; try { rateController = new RateController(this, intervals); } catch (final Exception ex) { throw new RuntimeException(ex); } final Runnable task = () -> { while (running) { while (sub.poll(fragmentHandler, 1) <= 0 && running) { } } System.out.println("Done"); }; final Thread subThread = new Thread(task); subThread.start(); try { connectionLatch.await(); } catch (final InterruptedException e) { e.printStackTrace(); } final int warmUpMsgs = 100000; for (int i = 0; i < warmUpMsgs; i++) { while (pub.tryClaim(buffer.capacity(), bufferClaim) < 0L) { idle.idle(1); } final MutableDirectBuffer buffer = bufferClaim.buffer(); final int offset = bufferClaim.offset(); buffer.putByte(offset, (byte) 'w'); bufferClaim.commit(); } try { Thread.sleep(1000); } catch (final InterruptedException e) { e.printStackTrace(); } System.out.println("warmup msgs received: " + warmups); final int start = (int) System.currentTimeMillis(); while (rateController.next()) { } final int total = (int) (System.currentTimeMillis() - start) / 1000; buffer.putByte(0, (byte) 'q'); while (pub.offer(buffer, 0, buffer.capacity()) < 0L) { idle.idle(0); } System.out.println("Duration: " + total + " seconds"); try { Thread.sleep(1000); } catch (final InterruptedException e) { e.printStackTrace(); } running = false; try { subThread.join(); } catch (final InterruptedException e) { e.printStackTrace(); } aeron.close(); try { computeStats(); } catch (IOException e) { e.printStackTrace(); } }
From source file:com.bennavetta.appsite.file.ResourceService.java
private void copy(ReadableByteChannel src, WritableByteChannel dest) throws IOException { ByteBuffer buffer = ByteBuffer.allocateDirect(bufferSize.get()); while (src.read(buffer) != -1 || buffer.position() != 0) { buffer.flip();//from w w w. ja v a 2 s . c om dest.write(buffer); buffer.compact(); } }
From source file:com.alvermont.terraj.fracplanet.geom.TriangleBufferArray.java
/** Creates a new instance of TriangleBufferArray */ public TriangleBufferArray() { this.buffer = ByteBuffer.allocateDirect(DEFAULT_CAPACITY * INTS_PER_ENTRY * SIZEOF_INT) .order(ByteOrder.nativeOrder()).asIntBuffer(); this.buffer.limit(0); }