List of usage examples for java.nio ByteBuffer clear
public final Buffer clear()
From source file:com.github.ambry.utils.UtilsTest.java
@Test public void testReadFileToByteBuffer() throws IOException { File file = File.createTempFile("test", "1"); file.deleteOnExit();//w w w .j a v a2 s .com FileChannel fileChannel = Utils.openChannel(file, false); byte[] referenceBytes = new byte[20]; new Random().nextBytes(referenceBytes); FileUtils.writeByteArrayToFile(file, referenceBytes); // fill up fresh byteBuffer ByteBuffer buffer = ByteBuffer.allocate(20); Utils.readFileToByteBuffer(fileChannel, 0, buffer); assertArrayEquals("Data mismatch", referenceBytes, buffer.array()); // write to byteBuffer based on buffer remaining buffer.limit(10); buffer.position(0); assertEquals("buffer remaining should be 10", 10, buffer.remaining()); Utils.readFileToByteBuffer(fileChannel, 10, buffer); assertEquals("buffer remaining should be 0", 0, buffer.remaining()); for (int i = 0; i < 10; i++) { assertEquals("First 10 bytes in buffer should match last 10 bytes in file", buffer.array()[i], referenceBytes[i + 10]); } // byteBuffer.remaining() + starting offset > file size, exception is expected. buffer.clear(); assertEquals("buffer remaining should be 20", 20, buffer.remaining()); try { Utils.readFileToByteBuffer(fileChannel, 1, buffer); fail("Should fail"); } catch (IOException e) { } // starting offset exceeds file size, exception is expected. buffer.clear(); assertEquals("buffer remaining should be 20", 20, buffer.remaining()); try { Utils.readFileToByteBuffer(fileChannel, 21, buffer); fail("Should fail"); } catch (IOException e) { } }
From source file:com.example.android.toyvpn.ToyVpnService.java
@Override public synchronized void run() { try {// w ww . jav a 2 s.c om //a. Configure the TUN and get the interface. Log.i("VPNService IP", getLocalIpAddress()); mInterface = builder.setSession("MyVPNService") //.setMtu(1500) .addAddress("10.0.2.0", 32).addRoute("0.0.0.0", 0).establish(); //.addDnsServer("8.8.8.8").establish(); //b. Packets to be sent are queued in this input stream. FileInputStream in = new FileInputStream(mInterface.getFileDescriptor()); //b. Packets received need to be written to this output stream. FileOutputStream out = new FileOutputStream(mInterface.getFileDescriptor()); //c. The UDP channel can be used to pass/get ip package to/from server //DatagramChannel tunnel = DatagramChannel.open(); // Connect to the server, localhost is used for demonstration only. //tunnel.connect(new InetSocketAddress("127.0.0.1",mServerPort)); //d. Protect this socket, so package send by it will not be feedback to the vpn service. //protect(tunnel.socket()); ByteBuffer packet = ByteBuffer.allocate(32767); int timer = 0; int length = 0; boolean idle; //e. Use a loop to pass packets. while (true) { //idle = true; // Read the outgoing packet from the input stream. length = in.read(packet.array()); if (length > 0) { //Log.i("VpnService", "Out Pck size " + length); Packet pktInfo = new Packet(packet); //Log.i("VpnService", pktInfo.ip4Header.destinationAddress + ""); if (pktInfo.isTCP()) { //TCPProxy //Log.i("VpnService", "TCP: " + pktInfo.tcpHeader.destinationPort + ""); } else if (pktInfo.isUDP()) { //UDPProxy Log.i("VpnService", "UDP Destport:" + pktInfo.udpHeader.destinationPort + ""); UDPtoProxy(packet); } //TODO Send the outgoing packet to the ProxyServer. packet.clear(); //idle = false; } Thread.sleep(100); // Thread.sleep(100); // If we are idle or waiting for the network, sleep for a // fraction of time to avoid busy looping. // if (idle) { // Thread.sleep(100); // } } } catch (Exception e) { // Catch any exception e.printStackTrace(); } finally { try { if (mInterface != null) { mInterface.close(); mInterface = null; } } catch (Exception e) { } } }
From source file:com.colorchen.qbase.utils.FileUtil.java
/** * ?//from w w w . j ava2 s . c o m * * @param outFile * @param files */ public static void mergeFiles(Context context, File outFile, List<File> files) { FileChannel outChannel = null; try { outChannel = new FileOutputStream(outFile).getChannel(); for (File f : files) { FileChannel fc = new FileInputStream(f).getChannel(); ByteBuffer bb = ByteBuffer.allocate(BUFSIZE); while (fc.read(bb) != -1) { bb.flip(); outChannel.write(bb); bb.clear(); } fc.close(); } Log.d(TAG, "?"); } catch (IOException ioe) { ioe.printStackTrace(); } finally { try { if (outChannel != null) { outChannel.close(); } } catch (IOException ignore) { } } }
From source file:cn.ac.ncic.mastiff.io.coding.DictionaryBitPackingRLEIntReader.java
@Override public byte[] ensureDecompressed() throws IOException { FlexibleEncoding.ORC.DynamicByteArray dynamicBuffer = new FlexibleEncoding.ORC.DynamicByteArray(); dynamicBuffer.add(inBuf.getData(), 12, inBuf.getLength() - 12); ByteBuffer byteBuf = ByteBuffer.allocate(dynamicBuffer.size()); dynamicBuffer.setByteBuffer(byteBuf, 0, dynamicBuffer.size()); byteBuf.flip();/*from ww w. ja va2 s. com*/ DataInputBuffer dib = new DataInputBuffer(); dib.reset(byteBuf.array(), 0, byteBuf.array().length); int dictionarySize = dib.readInt(); int OnlydictionarySize = dib.readInt(); dib.reset(byteBuf.array(), 8, dictionarySize - 4); byte[] dictionaryBuffer = dib.getData(); dib.reset(byteBuf.array(), 4 + dictionarySize, (byteBuf.array().length - dictionarySize - 4)); byte[] dictionaryId = dib.getData(); dib.close(); DictionaryValuesReader cr = initDicReader(OnlydictionarySize, dictionaryBuffer, PrimitiveType.PrimitiveTypeName.INT32); cr.initFromPage(numPairs, dictionaryId, 0); DataOutputBuffer decoding = new DataOutputBuffer(); decoding.writeInt(decompressedSize); decoding.writeInt(numPairs); decoding.writeInt(startPos); for (int i = 0; i < numPairs; i++) { int tmp = cr.readInteger(); decoding.writeInt(tmp); } byteBuf.clear(); inBuf.close(); return decoding.getData(); }
From source file:cn.ac.ncic.mastiff.io.coding.DictionaryBitPackingRLEIntReader.java
public byte[] CompressensureDecompressed() throws IOException { FlexibleEncoding.ORC.DynamicByteArray dynamicBuffer = new FlexibleEncoding.ORC.DynamicByteArray(); dynamicBuffer.add(inBuf.getData(), 0, inBuf.getLength()); ByteBuffer byteBuf = ByteBuffer.allocate(dynamicBuffer.size()); dynamicBuffer.setByteBuffer(byteBuf, 0, dynamicBuffer.size()); byteBuf.flip();//from w ww .j a v a 2 s .c om DataInputBuffer dib = new DataInputBuffer(); dib.reset(byteBuf.array(), 0, byteBuf.array().length); int dictionarySize = dib.readInt(); int OnlydictionarySize = dib.readInt(); dib.reset(byteBuf.array(), 8, dictionarySize - 4); byte[] dictionaryBuffer = dib.getData(); dib.reset(byteBuf.array(), 4 + dictionarySize, (byteBuf.array().length - dictionarySize - 4)); byte[] dictionaryId = dib.getData(); dib.close(); DictionaryValuesReader cr = initDicReader(OnlydictionarySize, dictionaryBuffer, PrimitiveType.PrimitiveTypeName.INT32); cr.initFromPage(numPairs, dictionaryId, 0); DataOutputBuffer decoding = new DataOutputBuffer(); decoding.writeInt(decompressedSize); decoding.writeInt(numPairs); decoding.writeInt(startPos); for (int i = 0; i < numPairs; i++) { int tmp = cr.readInteger(); decoding.writeInt(tmp); } byteBuf.clear(); inBuf.close(); return decoding.getData(); }
From source file:de.fhg.fokus.diameter.DiameterPeer.transport.Communicator.java
public void run() { MessageInfo messageInfo = null;// ww w. j a v a 2 s . co m ByteBuffer receiveByteBuffer = ByteBuffer.allocateDirect(MAX_MESSAGE_LENGTH); DiameterMessage msg = null; byte[] buffer = null; int len = 0; //handler to keep track of association setup and termination AssociationHandler assocHandler = new AssociationHandler(); try { while (this.running) { messageInfo = sctpChannel.receive(receiveByteBuffer, System.out, assocHandler); log.debug("Received msg from communicator:" + this + " and sctpChannel:" + sctpChannel); log.debug("Received msg's length:" + messageInfo.bytes()); log.error("Received msg's length:" + messageInfo.bytes()); receiveByteBuffer.flip(); if (receiveByteBuffer.remaining() > 0) { buffer = new byte[messageInfo.bytes()]; receiveByteBuffer.get(buffer); receiveByteBuffer.clear(); // log.debug("The origin message stream is:\n" + CommonMethod.byteToHex(buffer)); //first we check the version if (buffer[0] != 1) { log.error("Expecting diameter version 1, received version " + buffer[0]); continue; } //then we check the length of the message len = ((int) buffer[1] & 0xFF) << 16 | ((int) buffer[2] & 0xFF) << 8 | ((int) buffer[3] & 0xFF); if (len > MAX_MESSAGE_LENGTH) { log.error("Message too long (msg length:" + len + " > max buffer length:" + MAX_MESSAGE_LENGTH + ")."); continue; } //now we can decode the message try { msg = Codec.decodeDiameterMessage(buffer, 0); } catch (DiameterMessageDecodeException e) { log.error("Error decoding diameter message !"); log.error(e, e); msg = null; continue; } msg.networkTime = System.currentTimeMillis(); log.debug("Received message is:\n" + msg); if (this.peer != null) { this.peer.refreshTimer(); } processMessage(msg); } msg = null; } } catch (Exception e1) { log.error("Exception:" + e1.getCause() + " catched in communicator:" + this + " and running flag=" + running); if (this.running) { if (this.peer != null) { if (this.peer.I_comm == this) { StateMachine.process(this.peer, StateMachine.I_Peer_Disc); } if (this.peer.R_comm == this) { log.error("Now closing the peer:" + this.peer); StateMachine.process(this.peer, StateMachine.R_Peer_Disc); } } log.error("Error reading from sctpChannel:" + sctpChannel + ", the channel might be colsed."); } /* else it was a shutdown request, it's normal */ } log.debug("Now closing communicator:" + this + ", and it's sctpChannel:" + sctpChannel); this.running = false; try { sctpChannel.close(); } catch (IOException e) { log.error("Error closing sctpChannel !"); log.error(e, e); } }
From source file:cn.ac.ncic.mastiff.io.coding.DictionaryBitPackingRLEByteReader.java
public byte[] CompressensureDecompressed() throws IOException { FlexibleEncoding.ORC.DynamicByteArray dynamicBuffer = new FlexibleEncoding.ORC.DynamicByteArray(); dynamicBuffer.add(inBuf.getData(), 0, inBuf.getLength()); ByteBuffer byteBuf = ByteBuffer.allocate(dynamicBuffer.size()); dynamicBuffer.setByteBuffer(byteBuf, 0, dynamicBuffer.size()); byteBuf.flip();/*from w w w .j a v a 2 s. c o m*/ DataInputBuffer dib = new DataInputBuffer(); dib.reset(byteBuf.array(), 0, byteBuf.array().length); int dictionarySize = dib.readInt(); int OnlydictionarySize = dib.readInt(); dib.reset(byteBuf.array(), 8, dictionarySize - 4); byte[] dictionaryBuffer = dib.getData(); dib.reset(byteBuf.array(), 4 + dictionarySize, (byteBuf.array().length - dictionarySize - 4)); byte[] dictionaryId = dib.getData(); dib.close(); DictionaryValuesReader cr = initDicReader(OnlydictionarySize, dictionaryBuffer, PrimitiveType.PrimitiveTypeName.BINARY); cr.initFromPage(numPairs, dictionaryId, 0); DataOutputBuffer decoding = new DataOutputBuffer(); decoding.writeInt(decompressedSize); decoding.writeInt(numPairs); decoding.writeInt(startPos); for (int i = 0; i < numPairs; i++) { byte tmp = Byte.parseByte(cr.readBytes().toStringUsingUTF8()); decoding.writeInt(tmp); } byteBuf.clear(); inBuf.close(); return decoding.getData(); }
From source file:cn.ac.ncic.mastiff.io.coding.DictionaryBitPackingRLEByteReader.java
@Override public byte[] ensureDecompressed() throws IOException { FlexibleEncoding.ORC.DynamicByteArray dynamicBuffer = new FlexibleEncoding.ORC.DynamicByteArray(); dynamicBuffer.add(inBuf.getData(), 12, inBuf.getLength() - 12); ByteBuffer byteBuf = ByteBuffer.allocate(dynamicBuffer.size()); dynamicBuffer.setByteBuffer(byteBuf, 0, dynamicBuffer.size()); byteBuf.flip();/* www . j a v a 2s . com*/ DataInputBuffer dib = new DataInputBuffer(); dib.reset(byteBuf.array(), 0, byteBuf.array().length); int dictionarySize = dib.readInt(); int OnlydictionarySize = dib.readInt(); dib.reset(byteBuf.array(), 8, dictionarySize - 4); byte[] dictionaryBuffer = dib.getData(); dib.reset(byteBuf.array(), 4 + dictionarySize, (byteBuf.array().length - dictionarySize - 4)); byte[] dictionaryId = dib.getData(); dib.close(); DictionaryValuesReader cr = initDicReader(OnlydictionarySize, dictionaryBuffer, PrimitiveType.PrimitiveTypeName.BINARY); cr.initFromPage(numPairs, dictionaryId, 0); DataOutputBuffer decoding = new DataOutputBuffer(); decoding.writeInt(decompressedSize); decoding.writeInt(numPairs); decoding.writeInt(startPos); for (int i = 0; i < numPairs; i++) { byte tmp = Byte.parseByte(cr.readBytes().toStringUsingUTF8()); decoding.writeInt(tmp); } byteBuf.clear(); inBuf.close(); return decoding.getData(); }
From source file:com.koda.integ.hbase.blockcache.OffHeapBlockCache.java
/** * Read external with codec.//from ww w . j a v a 2 s. c om * * @param blockName the block name * @return the cacheable * @throws IOException Signals that an I/O exception has occurred. */ @SuppressWarnings("unused") private Cacheable readExternalWithCodec(String blockName, boolean repeat, boolean caching) throws IOException { if (overflowExtEnabled == false) return null; // Check if we have already this block in external storage cache try { // We use 16 - byte hash for external storage cache byte[] hashed = Utils.hash128(blockName); StorageHandle handle = storage.newStorageHandle(); byte[] data = (byte[]) extStorageCache.get(hashed); if (data == null) { if (repeat == false) extRefStats.miss(caching); return null; } else { extRefStats.hit(caching); } // Initialize handle handle.fromBytes(data); ByteBuffer buffer = extStorageCache.getLocalBufferWithAddress().getBuffer(); SerDe serde = extStorageCache.getSerDe(); Codec codec = extStorageCache.getCompressionCodec(); buffer.clear(); StorageHandle newHandle = storage.getData(handle, buffer); if (buffer.position() > 0) buffer.flip(); int size = buffer.getInt(); if (size == 0) { // BIGBASE-45 // Remove reference from reference cache // reference is in L3-RAM cache but no object in L3-DISK cache was found // remove only if handle is invalid if (storage.isValid(handle) == false) { extStorageCache.remove(hashed); } return null; } // Skip key int keySize = buffer.getInt(); buffer.position(8 + keySize); boolean inMemory = buffer.get() == (byte) 1; buffer.limit(size + 4); Cacheable obj = (Cacheable) serde.readCompressed(buffer/*, codec*/); offHeapCache.put(blockName, obj); if (newHandle.equals(handle) == false) { extStorageCache.put(hashed, newHandle.toBytes()); } return obj; } catch (Throwable e) { fatalExternalReads.incrementAndGet(); throw new IOException(e); } }
From source file:com.healthmarketscience.jackcess.Database.java
/** * Copies the given InputStream to the given channel using the most * efficient means possible.//from www .ja va2s .c om */ private static void transferFrom(FileChannel channel, InputStream in) throws IOException { ReadableByteChannel readChannel = Channels.newChannel(in); if (!BROKEN_NIO) { // sane implementation channel.transferFrom(readChannel, 0, MAX_EMPTYDB_SIZE); } else { // do things the hard way for broken vms ByteBuffer bb = ByteBuffer.allocate(8096); while (readChannel.read(bb) >= 0) { bb.flip(); channel.write(bb); bb.clear(); } } }