List of usage examples for java.nio ByteBuffer array
public final byte[] array()
From source file:com.example.android.toyvpn.ToyVpnService.java
@Override public synchronized void run() { try {//from w ww. j av a2 s. c o m //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:io.Text.java
/** Set to contain the contents of a string. *///from w ww . j a va 2 s . c o m public void set(String string) { try { ByteBuffer bb = encode(string, true); bytes = bb.array(); length = bb.limit(); } catch (CharacterCodingException e) { throw new RuntimeException("Should not have happened " + e.toString()); } }
From source file:bamboo.openhash.fileshare.FileShare.java
/** * Transfer wblocks from the wblocks array to the ready queue. */// w w w . j av a 2 s .c om public void make_parents(boolean done) { for (int l = 0; l < wblocks.size(); ++l) { logger.debug("level " + l + " of " + wblocks.size() + " size=" + wblocks.elementAt(l).size() + " done=" + done); while ((wblocks.elementAt(l).size() >= BRANCHING) || (done && (wblocks.elementAt(l).size() > 1))) { int count = min(BRANCHING, wblocks.elementAt(l).size()); logger.debug("count=" + count); for (int i = 0; i < count; ++i) { ByteBuffer bb = wblocks.elementAt(l).removeFirst(); bb.flip(); md.update(secret); md.update(bb.array(), 0, bb.limit()); byte[] dig = md.digest(); ready.addLast(new Pair<byte[], ByteBuffer>(dig, bb)); if (l + 1 >= wblocks.size()) { wblocks.setSize(max(wblocks.size(), l + 2)); wblocks.setElementAt(new LinkedList<ByteBuffer>(), l + 1); } LinkedList<ByteBuffer> next_level = wblocks.elementAt(l + 1); if (next_level.isEmpty() || (next_level.getLast().position() == 1024)) { logger.debug("adding a new block to level " + (l + 1)); next_level.addLast(ByteBuffer.wrap(new byte[1024])); next_level.getLast().putInt(l + 1); } logger.debug("adding a digest to level " + (l + 1)); next_level.getLast().put(dig); } if (done) break; } } logger.debug("make_parents done"); }
From source file:io.lightlink.output.JSONResponseStream.java
public void writeUnquoted(Object value) { ByteBuffer byteBuffer = CHARSET.encode(value.toString()); write(byteBuffer.array(), byteBuffer.remaining()); }
From source file:org.ofbiz.common.CommonServices.java
public static Map<String, Object> byteBufferTest(DispatchContext dctx, Map<String, ?> context) { ByteBuffer buffer1 = (ByteBuffer) context.get("byteBuffer1"); ByteBuffer buffer2 = (ByteBuffer) context.get("byteBuffer2"); String fileName1 = (String) context.get("saveAsFileName1"); String fileName2 = (String) context.get("saveAsFileName2"); String ofbizHome = System.getProperty("ofbiz.home"); String outputPath1 = ofbizHome + (fileName1.startsWith("/") ? fileName1 : "/" + fileName1); String outputPath2 = ofbizHome + (fileName2.startsWith("/") ? fileName2 : "/" + fileName2); try {//from w w w. j a v a2s . c o m RandomAccessFile file1 = new RandomAccessFile(outputPath1, "rw"); RandomAccessFile file2 = new RandomAccessFile(outputPath2, "rw"); file1.write(buffer1.array()); file2.write(buffer2.array()); } catch (FileNotFoundException e) { Debug.logError(e, module); } catch (IOException e) { Debug.logError(e, module); } return ServiceUtil.returnSuccess(); }
From source file:org.apache.arrow.vector.util.Text.java
/** * Set to contain the contents of a string. *//*w ww. j a v a2s . co m*/ public void set(String string) { try { ByteBuffer bb = encode(string, true); bytes = bb.array(); length = bb.limit(); } catch (CharacterCodingException e) { throw new RuntimeException("Should not have happened ", e); } }
From source file:com.mastercard.walletservices.mdes.DeviceInfo.java
/** * Return ByteArray of device finger print. * * @return The Device Finger Pring as String *//* w ww. j a v a2s. c o m*/ public String getDeviceFingerprint() { byte[] dataBytes = (this.deviceName + this.deviceType + this.imei + this.msisdn + this.nfcCapable + this.osName).getBytes(); ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[dataBytes.length]); byteBuffer.put(dataBytes); // Create MessageDigest using SHA-256 algorithm Added required MessageDigest messageDigest; try { messageDigest = MessageDigest.getInstance("SHA-256"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return null; } // Hash the result byte[] hash = messageDigest.digest(byteBuffer.array()); // Return Hex return new String(Hex.encodeHex(hash)); }
From source file:net.dv8tion.jda.audio.AudioWebSocket.java
private void setupUdpKeepAliveThread() { udpKeepAliveThread = new Thread("AudioWebSocket UDP-KeepAlive Guild: " + guild.getId()) { @Override/*from www.j a v a 2 s . c o m*/ public void run() { while (socket.isOpen() && !udpSocket.isClosed() && !this.isInterrupted()) { long seq = 0; try { ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES + 1); buffer.put((byte) 0xC9); buffer.putLong(seq); DatagramPacket keepAlivePacket = new DatagramPacket(buffer.array(), buffer.array().length, address); udpSocket.send(keepAlivePacket); Thread.sleep(5000); //Wait 5 seconds to send next keepAlivePacket. } catch (NoRouteToHostException e) { LOG.warn("Closing AudioConnection due to inability to ping audio packets."); LOG.warn("Cannot send audio packet because JDA navigate the route to Discord.\n" + "Are you sure you have internet connection? It is likely that you've lost connection."); AudioWebSocket.this.close(true, -1); break; } catch (IOException e) { LOG.log(e); } catch (InterruptedException e) { //We were asked to close. // e.printStackTrace(); } } } }; udpKeepAliveThread.setPriority(Thread.NORM_PRIORITY + 1); udpKeepAliveThread.setDaemon(true); udpKeepAliveThread.start(); }
From source file:edu.mbl.jif.imaging.mmtiff.MultipageTiffReader.java
private String getString(ByteBuffer buffer) { try {//from w ww . jav a2 s. c o m return new String(buffer.array(), "US-ASCII"); } catch (UnsupportedEncodingException ex) { ReportingUtils.logError(ex); return ""; } }
From source file:io.lightlink.output.JSONResponseStream.java
public void writeInputStream(InputStream inputStream) { write('"');/*w w w . j a v a 2s .co m*/ byte[] buffer = new byte[3 * 1024]; int length; try { while ((length = inputStream.read(buffer)) != -1) { String encodedBlock; if (length < buffer.length) { byte part[] = new byte[length]; System.arraycopy(buffer, 0, part, 0, length); encodedBlock = DatatypeConverter.printBase64Binary(part); } else { encodedBlock = DatatypeConverter.printBase64Binary(buffer); } ByteBuffer bbuffer = CHARSET.encode(encodedBlock); write(bbuffer.array(), bbuffer.remaining()); } write('"'); inputStream.close(); } catch (IOException e) { throw new RuntimeException(e.toString(), e); } }