List of usage examples for java.nio ByteBuffer clear
public final Buffer clear()
From source file:com.owncloud.android.lib.common.network.FileRequestEntity.java
@Override public void writeRequest(final OutputStream out) throws IOException { //byte[] tmp = new byte[4096]; ByteBuffer tmp = ByteBuffer.allocate(4096); int readResult = 0; // globally in some fashionable manner RandomAccessFile raf = new RandomAccessFile(mFile, "r"); FileChannel channel = raf.getChannel(); Iterator<OnDatatransferProgressListener> it = null; long transferred = 0; long size = mFile.length(); if (size == 0) size = -1;/* w ww . j av a 2s. c o m*/ try { while ((readResult = channel.read(tmp)) >= 0) { out.write(tmp.array(), 0, readResult); tmp.clear(); transferred += readResult; synchronized (mDataTransferListeners) { it = mDataTransferListeners.iterator(); while (it.hasNext()) { it.next().onTransferProgress(readResult, transferred, size, mFile.getAbsolutePath()); } } } } catch (IOException io) { Log_OC.e("FileRequestException", io.getMessage()); throw new RuntimeException( "Ugly solution to workaround the default policy of retries when the server falls while uploading ; temporal fix; really", io); } finally { channel.close(); raf.close(); } }
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 w w . j a v a 2 s .com 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:gda.device.scannable.keyence.Keyence.java
/** * Send command to the server.//w w w . j a v a 2 s. c om * * @param msg * an unterminated command * @param expectedReplyItems * @return the reply string. * @throws DeviceException */ public Object[] processImageRequest(String msg, int expectedReplyItems) throws DeviceException { if (expectedReplyItems < 2) throw new IllegalArgumentException("need at least two values for images (length definition and data)"); String command = msg + '\r'; Object reply[] = new Object[expectedReplyItems + 1]; logger.debug(getName() + ": sent command: " + msg); synchronized (socketAccessLock) { try { if (!isConnected()) { throw new DeviceException("not connected"); } cleanPipe(); socketChannel.write(encoder.encode(CharBuffer.wrap(command))); ByteBuffer singleByte = ByteBuffer.allocate(1); StringBuilder sb = new StringBuilder(); int argCounter = 0; while (argCounter < expectedReplyItems) { singleByte.clear(); socketChannel.socket().setSoTimeout(socketTimeOut); socketChannel.configureBlocking(true); while (singleByte.position() == 0) socketChannel.read(singleByte); singleByte.flip(); String c = decoder.decode(singleByte).toString(); logger.debug(c.toString()); if (c.equals(",")) { reply[argCounter] = sb.toString(); sb = new StringBuilder(); argCounter++; } else if (c.equals("\r")) { throw new DeviceException( "sendCommand: not enough data for image received - suspect an error"); } else { sb.append(c); } } int imageLength = Integer.parseInt(reply[expectedReplyItems - 1].toString()); byte[] imageData = new byte[imageLength]; ByteBuffer bybu = ByteBuffer.wrap(imageData); while (bybu.remaining() != 0) { socketChannel.read(bybu); } reply[expectedReplyItems] = imageData; } catch (SocketTimeoutException ex) { throw new DeviceException("sendCommand read timeout " + ex.getMessage(), ex); } catch (IOException ex) { // treat as fatal connected = false; throw new DeviceException("sendCommand: " + ex.getMessage(), ex); } } return reply; }
From source file:MSUmpire.SpectrumParser.mzXMLReadUnit.java
private void ParsePeakString(ScanData scan, String peakString) throws IOException, DataFormatException { int offset;//from w w w . j a v a 2 s .c o m peakString = peakString.replaceAll("\n", ""); byte[] decoded = Base64.decodeBase64(peakString.getBytes()); if ("zlib".equals(scan.compressionType)) { decoded = ZlibUncompressBuffer(decoded); } switch (scan.precision) { case (32): { offset = 0; for (int i = 0; i < scan.PeaksCountString; i++) { byte[] mz = new byte[] { decoded[offset], decoded[offset + 1], decoded[offset + 2], decoded[offset + 3] }; byte[] intensity = new byte[] { decoded[offset + 4], decoded[offset + 5], decoded[offset + 6], decoded[offset + 7] }; ByteBuffer mzBuffer = ByteBuffer.wrap(mz); ByteBuffer intBuffer = ByteBuffer.wrap(intensity); float intensityfloat = intBuffer.getFloat(); float mzfloat = mzBuffer.getFloat(); if (intensityfloat > 0f) { scan.AddPoint(mzfloat, intensityfloat); } mz = null; intensity = null; mzBuffer.clear(); intBuffer.clear(); mzBuffer = null; intBuffer = null; offset += 8; } break; } case (64): { offset = 0; for (int i = 0; i < scan.PeaksCountString; i++) { byte[] mz = new byte[] { decoded[offset], decoded[offset + 1], decoded[offset + 2], decoded[offset + 3], decoded[offset + 4], decoded[offset + 5], decoded[offset + 6], decoded[offset + 7] }; byte[] intensity = new byte[] { decoded[offset + 8], decoded[offset + 9], decoded[offset + 10], decoded[offset + 11], decoded[offset + 12], decoded[offset + 13], decoded[offset + 14], decoded[offset + 15] }; ByteBuffer mzBuffer = ByteBuffer.wrap(mz); ByteBuffer intBuffer = ByteBuffer.wrap(intensity); float intensityfloat = (float) intBuffer.getDouble(); float mzfloat = (float) mzBuffer.getDouble(); if (intensityfloat > 0f) { scan.AddPoint(mzfloat, intensityfloat); } mz = null; intensity = null; mzBuffer.clear(); intBuffer.clear(); mzBuffer = null; intBuffer = null; offset += 16; } break; } } peakString = null; decoded = null; }
From source file:net.cellcloud.talk.stuff.PrimitiveSerializer.java
/** ?? */// w ww . ja v a 2 s . c o m public static void write(OutputStream stream, Primitive primitive) { /* ?? [version]{sutff}...{stuff}[dialect@tracker] [01.00]{sub=cloud:string}{pre=add:string}[Action@Ambrose] */ try { // stream.write((int) TOKEN_OPEN_BRACKET); byte[] version = { '0', '1', TOKEN_POINT, '0', '0' }; stream.write(version); stream.write((int) TOKEN_CLOSE_BRACKET); ByteBuffer buf = ByteBuffer.allocate(BLOCK); int bufLength = 0; // List<SubjectStuff> subjects = primitive.subjects(); if (null != subjects) { Iterator<SubjectStuff> iter = subjects.iterator(); while (iter.hasNext()) { SubjectStuff stuff = iter.next(); stream.write((int) TOKEN_OPEN_BRACE); stream.write(STUFFTYPE_SUBJECT_BYTES); stream.write((int) TOKEN_OPERATE_ASSIGN); bufLength = reviseValue(buf, stuff.value.getBytes(Charset.forName("UTF-8"))); buf.flip(); byte[] d = new byte[bufLength]; buf.get(d, 0, bufLength); stream.write(d); buf.clear(); stream.write((int) TOKEN_OPERATE_DECLARE); stream.write(parseLiteralBase(stuff.literalBase)); stream.write((int) TOKEN_CLOSE_BRACE); } } List<PredicateStuff> predicates = primitive.predicates(); if (null != predicates) { Iterator<PredicateStuff> iter = predicates.iterator(); while (iter.hasNext()) { PredicateStuff stuff = iter.next(); stream.write((int) TOKEN_OPEN_BRACE); stream.write(STUFFTYPE_PREDICATE_BYTES); stream.write((int) TOKEN_OPERATE_ASSIGN); bufLength = reviseValue(buf, stuff.value.getBytes(Charset.forName("UTF-8"))); buf.flip(); byte[] d = new byte[bufLength]; buf.get(d, 0, bufLength); stream.write(d); buf.clear(); stream.write((int) TOKEN_OPERATE_DECLARE); stream.write(parseLiteralBase(stuff.literalBase)); stream.write((int) TOKEN_CLOSE_BRACE); } } List<ObjectiveStuff> objectives = primitive.objectives(); if (null != objectives) { Iterator<ObjectiveStuff> iter = objectives.iterator(); while (iter.hasNext()) { ObjectiveStuff stuff = iter.next(); stream.write((int) TOKEN_OPEN_BRACE); stream.write(STUFFTYPE_OBJECTIVE_BYTES); stream.write((int) TOKEN_OPERATE_ASSIGN); bufLength = reviseValue(buf, stuff.value.getBytes(Charset.forName("UTF-8"))); buf.flip(); byte[] d = new byte[bufLength]; buf.get(d, 0, bufLength); stream.write(d); buf.clear(); stream.write((int) TOKEN_OPERATE_DECLARE); stream.write(parseLiteralBase(stuff.literalBase)); stream.write((int) TOKEN_CLOSE_BRACE); } } List<AdverbialStuff> adverbials = primitive.adverbials(); if (null != adverbials) { Iterator<AdverbialStuff> iter = adverbials.iterator(); while (iter.hasNext()) { AdverbialStuff stuff = iter.next(); stream.write((int) TOKEN_OPEN_BRACE); stream.write(STUFFTYPE_ADVERBIAL_BYTES); stream.write((int) TOKEN_OPERATE_ASSIGN); bufLength = reviseValue(buf, stuff.value.getBytes(Charset.forName("UTF-8"))); buf.flip(); byte[] d = new byte[bufLength]; buf.get(d, 0, bufLength); stream.write(d); buf.clear(); stream.write((int) TOKEN_OPERATE_DECLARE); stream.write(parseLiteralBase(stuff.literalBase)); stream.write((int) TOKEN_CLOSE_BRACE); } } List<AttributiveStuff> attributives = primitive.attributives(); if (null != attributives) { Iterator<AttributiveStuff> iter = attributives.iterator(); while (iter.hasNext()) { AttributiveStuff stuff = iter.next(); stream.write((int) TOKEN_OPEN_BRACE); stream.write(STUFFTYPE_ATTRIBUTIVE_BYTES); stream.write((int) TOKEN_OPERATE_ASSIGN); bufLength = reviseValue(buf, stuff.value.getBytes(Charset.forName("UTF-8"))); buf.flip(); byte[] d = new byte[bufLength]; buf.get(d, 0, bufLength); stream.write(d); buf.clear(); stream.write((int) TOKEN_OPERATE_DECLARE); stream.write(parseLiteralBase(stuff.literalBase)); stream.write((int) TOKEN_CLOSE_BRACE); } } List<ComplementStuff> complements = primitive.complements(); if (null != complements) { Iterator<ComplementStuff> iter = complements.iterator(); while (iter.hasNext()) { ComplementStuff stuff = iter.next(); stream.write((int) TOKEN_OPEN_BRACE); stream.write(STUFFTYPE_COMPLEMENT_BYTES); stream.write((int) TOKEN_OPERATE_ASSIGN); bufLength = reviseValue(buf, stuff.value.getBytes(Charset.forName("UTF-8"))); buf.flip(); byte[] d = new byte[bufLength]; buf.get(d, 0, bufLength); stream.write(d); buf.clear(); stream.write((int) TOKEN_OPERATE_DECLARE); stream.write(parseLiteralBase(stuff.literalBase)); stream.write((int) TOKEN_CLOSE_BRACE); } } // Dialect dialect = primitive.getDialect(); if (null != dialect) { stream.write(TOKEN_OPEN_BRACKET); stream.write(dialect.getName().getBytes(Charset.forName("UTF-8"))); stream.write(TOKEN_AT); stream.write(dialect.getTracker().getBytes(Charset.forName("UTF-8"))); stream.write(TOKEN_CLOSE_BRACKET); } stream.flush(); } catch (IOException e) { Logger.log(PrimitiveSerializer.class, e, LogLevel.ERROR); } }
From source file:cn.ac.ncic.mastiff.io.coding.DeltaBinaryBitPackingZigZarIntReader.java
@Override public byte[] ensureDecompressed() throws IOException { FlexibleEncoding.ORC.DynamicByteArray dynamicBuffer = new FlexibleEncoding.ORC.DynamicByteArray(); dynamicBuffer.add(inBuf.getData(), 12, inBuf.getLength() - 12); FlexibleEncoding.Parquet.DeltaBinaryPackingValuesReader reader = new FlexibleEncoding.Parquet.DeltaBinaryPackingValuesReader(); ByteBuffer byteBuf = ByteBuffer.allocate(dynamicBuffer.size()); dynamicBuffer.setByteBuffer(byteBuf, 0, dynamicBuffer.size()); byteBuf.flip();/*w ww . j a va 2 s. c om*/ reader.initFromPage(numPairs, byteBuf.array(), 0); DataOutputBuffer decoding = new DataOutputBuffer(); decoding.writeInt(decompressedSize); decoding.writeInt(numPairs); decoding.writeInt(startPos); for (int i = 0; i < numPairs; i++) { int tmp = reader.readInteger(); decoding.writeInt(tmp); } byteBuf.clear(); inBuf.close(); return decoding.getData(); }
From source file:cn.ac.ncic.mastiff.io.coding.DeltaBinaryBitPackingZigZarIntReader.java
public byte[] CompressensureDecompressed() throws IOException { FlexibleEncoding.ORC.DynamicByteArray dynamicBuffer = new FlexibleEncoding.ORC.DynamicByteArray(); dynamicBuffer.add(inBuf.getData(), 0, inBuf.getLength()); FlexibleEncoding.Parquet.DeltaBinaryPackingValuesReader reader = new FlexibleEncoding.Parquet.DeltaBinaryPackingValuesReader(); ByteBuffer byteBuf = ByteBuffer.allocate(dynamicBuffer.size()); dynamicBuffer.setByteBuffer(byteBuf, 0, dynamicBuffer.size()); byteBuf.flip();/* w w w .j a v a2s . co m*/ reader.initFromPage(numPairs, byteBuf.array(), 0); DataOutputBuffer decoding = new DataOutputBuffer(); decoding.writeInt(decompressedSize); decoding.writeInt(numPairs); decoding.writeInt(startPos); for (int i = 0; i < numPairs; i++) { int tmp = reader.readInteger(); decoding.writeInt(tmp); } byteBuf.clear(); inBuf.close(); return decoding.getData(); }
From source file:hornet.framework.clamav.service.ClamAVCheckService.java
/** * Lecture du fichier et envoi sur la socket. * * @param resultat/*from ww w . jav a 2 s . c o m*/ * resultat * @param fileForTestSize * fileForTestSize * @param channel * channel * @param bufFileForTestRead * bufFileForTestRead * @throws IOException * IOException */ protected void readAndSendFile(final StringBuilder resultat, final long fileForTestSize, final SocketChannel channel, final MappedByteBuffer bufFileForTestRead) throws IOException { // Envoi de la commande final ByteBuffer writeReadBuffer = ByteBuffer.allocate(BUFFER_SIZE); writeReadBuffer.put(ClamAVCheckService.COMMANDE.getBytes(UTF_8)); writeReadBuffer.put(this.intToByteArray((int) fileForTestSize)); writeReadBuffer.flip(); channel.write(writeReadBuffer); // Envoi du fichier long size = fileForTestSize; // envoi du fichier while (size > 0) { size -= channel.write(bufFileForTestRead); } final ByteBuffer writeBuffer = ByteBuffer.allocate(4); writeBuffer.put(new byte[] { 0, 0, 0, 0 }); writeBuffer.flip(); channel.write(writeBuffer); // lecture de la rponse ByteBuffer readBuffer; readBuffer = ByteBuffer.allocate(BUFFER_SIZE); // lecture de la rponse readBuffer.clear(); boolean readLine = false; while (!readLine) { final int numReaden = channel.read(readBuffer); if (numReaden > 0) { readLine = readBuffer.get(numReaden - 1) == '\n'; resultat.append(new String(readBuffer.array(), 0, numReaden, UTF_8)); readBuffer.clear(); } else { if (numReaden == -1) { readLine = true; readBuffer.clear(); } } } }
From source file:com.l2jfree.network.mmocore.ReadWriteThread.java
private void readPacket(SelectionKey key) { @SuppressWarnings("unchecked") T con = (T) key.attachment();//from w w w.j a v a 2 s . c o m ByteBuffer buf = con.getReadBuffer(); if (buf == null) { buf = getReadBuffer(); buf.clear(); } int readPackets = 0; int readBytes = 0; for (;;) { final int remainingFreeSpace = buf.remaining(); int result = -2; try { result = con.getReadableByteChannel().read(buf); } catch (IOException e) { //error handling goes bellow } switch (result) { case -2: // IOException { closeConnectionImpl(con, true); return; } case -1: // EOS { closeConnectionImpl(con, false); return; } default: { buf.flip(); // try to read as many packets as possible for (;;) { final int startPos = buf.position(); if (readPackets >= getMaxIncomingPacketsPerPass() || readBytes >= getMaxIncomingBytesPerPass()) break; if (!tryReadPacket2(con, buf)) break; readPackets++; readBytes += (buf.position() - startPos); } break; } } // stop reading, if we have reached a config limit if (readPackets >= getMaxIncomingPacketsPerPass() || readBytes >= getMaxIncomingBytesPerPass()) break; // if the buffer wasn't filled completely, we should stop trying as the input channel is empty if (remainingFreeSpace > result) break; // compact the buffer for reusing the remaining bytes if (buf.hasRemaining()) buf.compact(); else buf.clear(); } // check if there are some more bytes in buffer and allocate/compact to prevent content lose. if (buf.hasRemaining()) { if (buf == getReadBuffer()) { con.setReadBuffer(getPooledBuffer().put(getReadBuffer())); } else { buf.compact(); } } else { if (buf == getReadBuffer()) { // no additional buffers used } else { con.setReadBuffer(null); recycleBuffer(buf); } } }
From source file:org.archive.modules.recrawl.wbm.WbmPersistLoadProcessor.java
protected HashMap<String, Object> getLastCrawl(InputStream is) throws IOException { // read CDX lines, save most recent (at the end) hash. ByteBuffer buffer = ByteBuffer.allocate(32); ByteBuffer tsbuffer = ByteBuffer.allocate(14); int field = 0; int c;// w w w. j a v a2 s . c o m do { c = is.read(); if (field == 1) { // 14-digits timestamp tsbuffer.clear(); while (Character.isDigit(c) && tsbuffer.remaining() > 0) { tsbuffer.put((byte) c); c = is.read(); } if (c != ' ' || tsbuffer.position() != 14) { tsbuffer.clear(); } // fall through to skip the rest } else if (field == 5) { buffer.clear(); while ((c >= 'A' && c <= 'Z' || c >= '0' && c <= '9') && buffer.remaining() > 0) { buffer.put((byte) c); c = is.read(); } if (c != ' ' || buffer.position() != 32) { buffer.clear(); } // fall through to skip the rest } while (true) { if (c == -1) { break; } else if (c == '\n') { field = 0; break; } else if (c == ' ') { field++; break; } c = is.read(); } } while (c != -1); HashMap<String, Object> info = new HashMap<String, Object>(); if (buffer.remaining() == 0) { info.put(RecrawlAttributeConstants.A_CONTENT_DIGEST, contentDigestScheme + new String(buffer.array())); } if (tsbuffer.remaining() == 0) { try { long ts = DateUtils.parse14DigitDate(new String(tsbuffer.array())).getTime(); // A_TIMESTAMP has been used for sorting history long before A_FETCH_BEGAN_TIME // field was introduced. Now FetchHistoryProcessor fails if A_FETCH_BEGAN_TIME is // not set. We could stop storing A_TIMESTAMP and sort by A_FETCH_BEGAN_TIME. info.put(FetchHistoryHelper.A_TIMESTAMP, ts); info.put(CoreAttributeConstants.A_FETCH_BEGAN_TIME, ts); } catch (ParseException ex) { } } return info.isEmpty() ? null : info; }