List of usage examples for java.nio ByteBuffer remaining
public final int remaining()
From source file:hivemall.mf.BPRMatrixFactorizationUDTF.java
protected void beforeTrain(final long rowNum, final int u, final int i, final int j) throws HiveException { if (inputBuf != null) { assert (fileIO != null); final ByteBuffer buf = inputBuf; int remain = buf.remaining(); if (remain < RECORD_BYTES) { writeBuffer(buf, fileIO, lastWritePos); this.lastWritePos = rowNum; }/* w ww .ja v a2 s . c o m*/ buf.putInt(u); buf.putInt(i); buf.putInt(j); } }
From source file:io.lightlink.output.JSONResponseStream.java
public void writeFromReader(Reader reader) { write('"');//from ww w . j a va 2 s . c om char[] buffer = new char[16000]; long count = 0; int n = 0; try { while (-1 != (n = reader.read(buffer))) { ByteBuffer bbuffer = CHARSET.encode(CharBuffer.wrap(buffer, 0, n)); write(bbuffer.array(), bbuffer.remaining()); count += n; } write('"'); reader.close(); } catch (IOException e) { throw new RuntimeException(e.toString(), e); } }
From source file:com.p2p.peercds.common.Torrent.java
private static String hashFiles(List<File> files) throws InterruptedException, IOException { int threads = getHashingThreadsCount(); ExecutorService executor = Executors.newFixedThreadPool(threads); ByteBuffer buffer = ByteBuffer.allocate(PIECE_LENGTH); List<Future<String>> results = new LinkedList<Future<String>>(); StringBuilder hashes = new StringBuilder(); long length = 0L; int pieces = 0; long start = System.nanoTime(); for (File file : files) { logger.info("Hashing data from {} with {} threads ({} pieces)...", new Object[] { file.getName(), threads, (int) (Math.ceil((double) file.length() / PIECE_LENGTH)) }); length += file.length();/*from w ww .j ava 2 s . c o m*/ FileInputStream fis = new FileInputStream(file); FileChannel channel = fis.getChannel(); int step = 10; try { while (channel.read(buffer) > 0) { if (buffer.remaining() == 0) { buffer.clear(); results.add(executor.submit(new CallableChunkHasher(buffer))); } if (results.size() >= threads) { pieces += accumulateHashes(hashes, results); } if (channel.position() / (double) channel.size() * 100f > step) { logger.info(" ... {}% complete", step); step += 10; } } } finally { channel.close(); fis.close(); } } // Hash the last bit, if any if (buffer.position() > 0) { buffer.limit(buffer.position()); buffer.position(0); results.add(executor.submit(new CallableChunkHasher(buffer))); } pieces += accumulateHashes(hashes, results); // Request orderly executor shutdown and wait for hashing tasks to // complete. executor.shutdown(); while (!executor.isTerminated()) { Thread.sleep(10); } long elapsed = System.nanoTime() - start; int expectedPieces = (int) (Math.ceil((double) length / PIECE_LENGTH)); logger.info("Hashed {} file(s) ({} bytes) in {} pieces ({} expected) in {}ms.", new Object[] { files.size(), length, pieces, expectedPieces, String.format("%.1f", elapsed / 1e6), }); return hashes.toString(); }
From source file:org.apache.mele.embedded.HadoopQueueEmbedded.java
public void writeByteBuffers(Collection<ByteBuffer> messages) throws IOException { if (!isWritable()) { throw new IOException("Not open for writing."); }//from w ww .j a v a 2s. c om _writeLock.lock(); try { int bufSize = 1024; byte[] buf = new byte[bufSize]; for (ByteBuffer message : messages) { int remaining = message.remaining(); _queueOutputStream.writeInt(remaining); while (remaining > 0) { int len = Math.min(remaining, bufSize); message.get(buf, 0, len); _queueOutputStream.write(buf, 0, len); remaining -= len; } } flushAndSync(_queueOutputStream); _producer.set(_queueOutputStream.getPos()); checkForWriteClosing(); } finally { _writeLock.unlock(); } }
From source file:io.lightlink.output.JSONResponseStream.java
private void writeEscapedString(String valueStr) { char[] chars = valueStr.toCharArray(); StringBuffer sb = new StringBuffer(valueStr); for (int i = chars.length - 1; i >= 0; i--) { char ch = chars[i]; switch (ch) { case '"': sb.replace(i, i + 1, STR_QUOT); break; case '\\': sb.replace(i, i + 1, STR_BACKSLASH); break; case '\b': sb.replace(i, i + 1, STR_B); break; case '\f': sb.replace(i, i + 1, STR_F); break; case '\n': sb.replace(i, i + 1, STR_N); break; case '\r': sb.replace(i, i + 1, STR_R); break; case '\t': sb.replace(i, i + 1, STR_T); break; case '/': sb.replace(i, i + 1, STR_SLASH); break; default:/* w w w . j ava2 s . c om*/ //Reference: http://www.unicode.org/versions/Unicode5.1.0/ if ((ch >= '\u0000' && ch <= '\u001F') || (ch >= '\u007F' && ch <= '\u009F') || (ch >= '\u2000' && ch <= '\u20FF')) { StringBuilder encoded = new StringBuilder(); String ss = Integer.toHexString(ch).toUpperCase(); encoded.append(STR_SLASH_U); for (int k = 0; k < 4 - ss.length(); k++) { encoded.append('0'); } encoded.append(ss.toUpperCase()); sb.replace(i, i + 1, encoded.toString()); } } } ByteBuffer buffer = CHARSET.encode(sb.toString()); write(buffer.array(), buffer.remaining()); }
From source file:com.l2jfree.network.mmocore.ReadWriteThread.java
private boolean tryReadPacket2(T con, ByteBuffer buf) { // check if header could be processed if (buf.remaining() >= 2) { // parse all headers and get expected packet size final int size = (buf.getChar() - PACKET_HEADER_SIZE); // do we got enough bytes for the packet? if (size <= buf.remaining()) { // avoid parsing dummy packets (packets without body) if (size > 0) { int pos = buf.position(); parseClientPacket(buf, size, con); buf.position(pos + size); } else { // let's report error to trigger protection getMMOController().report(ErrorMode.EMPTY_PACKET, con, null, null); }/*from w w w . ja v a 2 s. c o m*/ return true; } else { // we dont have enough bytes for the packet so we need to read and revert the header buf.position(buf.position() - PACKET_HEADER_SIZE); return false; } } else { // we dont have enough data for header so we need to read return false; } }
From source file:dotaSoundEditor.Controls.EditorPanel.java
protected void revertButtonActionPerformed(ActionEvent evt, Path vpkToRevert) { //TODO: See if we can abstract away some of this functionality if (currentTree.getSelectionRows().length != 0 && ((TreeNode) currentTree.getSelectionPath().getLastPathComponent()).isLeaf()) { DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) currentTree.getSelectionPath() .getLastPathComponent(); String selectedWaveString = ((DefaultMutableTreeNode) selectedNode).getUserObject().toString(); String selectedWaveParentString = ((DefaultMutableTreeNode) ((DefaultMutableTreeNode) selectedNode) .getParent()).getUserObject().toString(); selectedNode = (DefaultMutableTreeNode) this.getTreeNodeFromWavePath(selectedWaveString); //First go in and delete the sound in customSounds deleteSoundFileByWaveString(selectedWaveString); //Get the relevant wavestring from the internal scriptfile VPKArchive vpk = new VPKArchive(); try {/*from www.j a va 2 s. co m*/ vpk.load(new File(vpkToRevert.toString())); } catch (IOException ex) { ex.printStackTrace(); } String scriptDir = getCurrentScriptString(); scriptDir = scriptDir.replace(Paths.get(installDir, "/dota/").toString(), ""); scriptDir = scriptDir.replace("\\", "/"); //Match internal forward slashes scriptDir = scriptDir.substring(1); //Cut off leading slash byte[] bytes = null; VPKEntry entry = vpk.getEntry(scriptDir); try { ByteBuffer scriptBuffer = entry.getData(); bytes = new byte[scriptBuffer.remaining()]; scriptBuffer.get(bytes); } catch (IOException ex) { ex.printStackTrace(); } String scriptFileString = new String(bytes, Charset.forName("UTF-8")); ArrayList<String> wavePathList = this.getWavePathsAsList(selectedNode.getParent()); int waveStringIndex = wavePathList.indexOf(selectedWaveString); //Cut off every part of the scriptFileString before we get to the entry describing the relevant hero action, so we don't accidentally get the wrong wavepaths StringBuilder scriptFileStringShortened = new StringBuilder(); Scanner scan = new Scanner(scriptFileString); boolean found = false; while (scan.hasNextLine()) { String curLine = scan.nextLine(); if (curLine.equals(selectedWaveParentString)) { found = true; } if (found == true) { scriptFileStringShortened.append(curLine).append(System.lineSeparator()); } } scriptFileString = scriptFileStringShortened.toString(); ArrayList<String> internalWavePathsList = getWavePathListFromString(scriptFileString); String replacementString = internalWavePathsList.get(waveStringIndex); selectedNode.setUserObject(replacementString); ScriptParser parser = new ScriptParser(this.currentTreeModel); parser.writeModelToFile(getCurrentScriptString()); //Modify the UI treeNode in addition to the backing TreeNode ((DefaultMutableTreeNode) currentTree.getLastSelectedPathComponent()).setUserObject(replacementString); ((DefaultTreeModel) currentTree.getModel()) .nodeChanged((DefaultMutableTreeNode) currentTree.getLastSelectedPathComponent()); } }
From source file:com.offbynull.portmapper.natpmp.NatPmpReceiver.java
/** * Start listening for NAT-PMP events. This method blocks until {@link #stop() } is called. * @param listener listener to notify of events * @throws IOException if socket error occurs * @throws NullPointerException if any argument is {@code null} *//*from ww w . ja va 2s . c om*/ public void start(NatPmpEventListener listener) throws IOException { Validate.notNull(listener); MulticastSocket socket = null; try { final InetAddress group = InetAddress.getByName("224.0.0.1"); // NOPMD final int port = 5350; final InetSocketAddress groupAddress = new InetSocketAddress(group, port); socket = new MulticastSocket(port); if (!currentSocket.compareAndSet(null, socket)) { IOUtils.closeQuietly(socket); return; } socket.setReuseAddress(true); Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface networkInterface = interfaces.nextElement(); Enumeration<InetAddress> addrs = networkInterface.getInetAddresses(); while (addrs.hasMoreElements()) { // make sure atleast 1 ipv4 addr bound to interface InetAddress addr = addrs.nextElement(); try { if (addr instanceof Inet4Address) { socket.joinGroup(groupAddress, networkInterface); } } catch (IOException ioe) { // NOPMD // occurs with certain interfaces // do nothing } } } ByteBuffer buffer = ByteBuffer.allocate(12); DatagramPacket data = new DatagramPacket(buffer.array(), buffer.capacity()); while (true) { buffer.clear(); socket.receive(data); buffer.position(data.getLength()); buffer.flip(); if (!data.getAddress().equals(gatewayAddress)) { // data isn't from our gateway, ignore continue; } if (buffer.remaining() != 12) { // data isn't the expected size, ignore continue; } int version = buffer.get(0); if (version != 0) { // data doesn't have the correct version, ignore continue; } int opcode = buffer.get(1) & 0xFF; if (opcode != 128) { // data doesn't have the correct op, ignore continue; } int resultCode = buffer.getShort(2) & 0xFFFF; switch (resultCode) { case 0: break; default: continue; // data doesn't have a successful result, ignore } listener.publicAddressUpdated(new ExternalAddressNatPmpResponse(buffer)); } } catch (IOException ioe) { if (currentSocket.get() == null) { return; // ioexception caused by interruption/stop, so just return without propogating error up } throw ioe; } finally { IOUtils.closeQuietly(socket); currentSocket.set(null); } }
From source file:org.commoncrawl.hadoop.io.S3GetMetdataJob.java
@org.junit.Test public void testMapper() throws Exception { final ArcFileReader reader = new ArcFileReader(); Thread thread = new Thread(new Runnable() { public void run() { try { while (reader.hasMoreItems()) { ArcFileItem item = new ArcFileItem(); reader.getNextItem(item); map(new Text(item.getUri()), item, null, null); }//from w ww . j a va 2 s .c om LOG.info("NO MORE ITEMS... BYE"); } catch (IOException e) { LOG.error(StringUtils.stringifyException(e)); } } }); // run the thread ... thread.start(); File file = new File("/Users/rana/Downloads/1213886083018_0.arc.gz"); ReadableByteChannel channel = Channels.newChannel(new FileInputStream(file)); try { int totalBytesRead = 0; for (;;) { ByteBuffer buffer = ByteBuffer.allocate(ArcFileReader.DEFAULT_BLOCK_SIZE); int bytesRead = channel.read(buffer); LOG.info("Read " + bytesRead + " From File"); if (bytesRead == -1) { reader.finished(); break; } else { buffer.flip(); totalBytesRead += buffer.remaining(); reader.available(buffer); } } } finally { channel.close(); } // now wait for thread to die ... LOG.info("Done Reading File.... Waiting for ArcFileThread to DIE"); thread.join(); LOG.info("Done Reading File.... ArcFileThread to DIED"); }