List of usage examples for java.nio ByteBuffer remaining
public final int remaining()
From source file:org.apache.nifi.io.nio.AbstractChannelReader.java
@Override public final void run() { if (!key.isValid() || consumer.isConsumerFinished()) { closeStream();/*from w ww .j av a 2 s . co m*/ return; } if (!key.isReadable()) { return;//there is nothing available to read...or we aren't allow to read due to throttling } ByteBuffer buffer = null; try { buffer = bufferPool.poll(); if (buffer == null) { return; // no buffers available - come back later } final int bytesRead = fillBuffer(key, buffer); buffer.flip(); if (buffer.remaining() > 0) { consumer.addFilledBuffer(buffer); buffer = null; //clear the reference - is now the consumer's responsibility } else { buffer.clear(); bufferPool.returnBuffer(buffer, 0); buffer = null; //clear the reference - is now back to the queue } if (bytesRead < 0) { //we've reached the end closeStream(); } } catch (final Exception ioe) { closeStream(); LOGGER.error("Closed channel reader " + this + " due to " + ioe); } finally { if (buffer != null) { buffer.clear(); bufferPool.returnBuffer(buffer, 0); } } }
From source file:co.cask.cdap.client.rest.RestStreamWriter.java
@Override public ListenableFuture<Void> write(ByteBuffer buffer, Map<String, String> headers) throws IllegalArgumentException { Preconditions.checkArgument(buffer != null, "ByteBuffer parameter is null."); HttpEntity content;//from w w w .j a v a2 s . c o m if (buffer.hasArray()) { content = new ByteArrayEntity(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining()); } else { byte[] bytes = new byte[buffer.remaining()]; buffer.get(bytes); content = new ByteArrayEntity(bytes); } return write(content, headers); }
From source file:org.apache.hadoop.mapred.nativetask.handlers.BufferPushee.java
public boolean collect(InputBuffer buffer) throws IOException { if (closed) { return false; }/*from ww w .jav a 2s.com*/ final ByteBuffer input = buffer.getByteBuffer(); if (null != asideBuffer && asideBuffer.length() > 0) { if (asideBuffer.remaining() > 0) { final byte[] output = asideBuffer.getByteBuffer().array(); final int write = Math.min(asideBuffer.remaining(), input.remaining()); input.get(output, asideBuffer.position(), write); asideBuffer.position(asideBuffer.position() + write); } if (asideBuffer.remaining() == 0 && asideBuffer.position() > 0) { asideBuffer.position(0); write(asideBuffer); asideBuffer.rewind(0, 0); } } if (input.remaining() == 0) { return true; } if (input.remaining() < KV_HEADER_LENGTH) { throw new IOException("incomplete data, input length is: " + input.remaining()); } final int position = input.position(); final int keyLength = input.getInt(); final int valueLength = input.getInt(); input.position(position); final int kvLength = keyLength + valueLength + KV_HEADER_LENGTH; final int remaining = input.remaining(); if (kvLength > remaining) { if (null == asideBuffer || asideBuffer.capacity() < kvLength) { asideBuffer = new InputBuffer(BufferType.HEAP_BUFFER, kvLength); } asideBuffer.rewind(0, kvLength); input.get(asideBuffer.array(), 0, remaining); asideBuffer.position(remaining); } else { write(buffer); } return true; }
From source file:info.dolezel.fatrat.plugins.DownloadPlugin.java
/** * If the captcha image URL depends on the IP address, cookies or other circumstances * that won't exist if that URL is loaded in a different environment or location, * use this method instead of {@link #solveCaptcha}. * @param url URL of the image/* w ww.j ava 2 s.co m*/ * @param cb Callback with the result */ protected void solveCaptchaLoadLocally(String url, final CaptchaListener cb) { fetchPage(url, new PageFetchListener() { @Override public void onCompleted(ByteBuffer buf, Map<String, String> headers) { String type; if (headers.containsKey("content-type")) type = headers.get("content-type"); else type = ""; byte[] b = new byte[buf.remaining()]; buf.get(b); String encodedImage = "data:" + type + ";base64," + Base64.encodeBase64String(b); solveCaptcha(encodedImage, cb); } @Override public void onFailed(String error) { cb.onFailed(); } }); }
From source file:com.facebook.infrastructure.net.TcpConnection.java
private void handleIncompleteWrite(ByteBuffer buffer) { if (buffer.remaining() > 0) { pendingWrites_.add(buffer);//w w w . j ava 2 s. c om if ((key_.interestOps() & SelectionKey.OP_WRITE) == 0) { SelectorManager.getSelectorManager().modifyKeyForWrite(key_); } waitToContinueStreaming(); } }
From source file:com.healthmarketscience.jackcess.PageChannel.java
/** * Write a page (or part of a page) to disk * @param page Page to write//ww w . java2s . c o m * @param pageNumber Page number to write the page to * @param pageOffset offset within the page at which to start writing the * page data */ public void writePage(ByteBuffer page, int pageNumber, int pageOffset) throws IOException { validatePageNumber(pageNumber); page.rewind(); if ((page.remaining() - pageOffset) > getFormat().PAGE_SIZE) { throw new IllegalArgumentException("Page buffer is too large, size " + (page.remaining() - pageOffset)); } ByteBuffer encodedPage = page; if (pageNumber == 0) { // re-mask header applyHeaderMask(page); } else { // re-encode page encodedPage = _codecHandler.encodePage(page, pageNumber, pageOffset); } try { encodedPage.position(pageOffset); _channel.write(encodedPage, (getPageOffset(pageNumber) + pageOffset)); if (_autoSync) { flush(); } } finally { if (pageNumber == 0) { // de-mask header applyHeaderMask(page); } } }
From source file:com.offbynull.peernetic.debug.actornetwork.messages.TransitMessage.java
/** * Constructs a {@link TransitMessage} object. * @param source source//www .ja va 2 s . c om * @param destination destination * @param data contents * @param departTime time packet departed * @param duration duration the packet should stay in transit (before arriving) * @throws NullPointerException if any arguments are {@code null} * @throws IllegalArgumentException if {@code duration} is negative */ public TransitMessage(A source, A destination, ByteBuffer data, Instant departTime, Duration duration) { Validate.notNull(source); Validate.notNull(destination); Validate.notNull(data); Validate.notNull(departTime); Validate.notNull(duration); Validate.isTrue(!duration.isNegative()); this.source = source; this.destination = destination; this.data = ByteBuffer.allocate(data.remaining()); this.data.put(data); this.data.flip(); this.departTime = departTime; this.duration = duration; }
From source file:org.apache.bookkeeper.bookie.EntryLogTest.java
@Test(timeout = 60000) public void testMissingLogId() throws Exception { File tmpDir = createTempDir("entryLogTest", ".dir"); File curDir = Bookie.getCurrentDirectory(tmpDir); Bookie.checkDirectoryStructure(curDir); ServerConfiguration conf = TestBKConfiguration.newServerConfiguration(); conf.setLedgerDirNames(new String[] { tmpDir.toString() }); Bookie bookie = new Bookie(conf); // create some entries int numLogs = 3; int numEntries = 10; long[][] positions = new long[2 * numLogs][]; for (int i = 0; i < numLogs; i++) { positions[i] = new long[numEntries]; EntryLogger logger = new EntryLogger(conf, bookie.getLedgerDirsManager()); for (int j = 0; j < numEntries; j++) { positions[i][j] = logger.addEntry(i, generateEntry(i, j)); }/*from w w w . j a v a 2 s .c o m*/ logger.flush(); } // delete last log id File lastLogId = new File(curDir, "lastId"); lastLogId.delete(); // write another entries for (int i = numLogs; i < 2 * numLogs; i++) { positions[i] = new long[numEntries]; EntryLogger logger = new EntryLogger(conf, bookie.getLedgerDirsManager()); for (int j = 0; j < numEntries; j++) { positions[i][j] = logger.addEntry(i, generateEntry(i, j)); } logger.flush(); } EntryLogger newLogger = new EntryLogger(conf, bookie.getLedgerDirsManager()); for (int i = 0; i < (2 * numLogs + 1); i++) { File logFile = new File(curDir, Long.toHexString(i) + ".log"); assertTrue(logFile.exists()); } for (int i = 0; i < 2 * numLogs; i++) { for (int j = 0; j < numEntries; j++) { String expectedValue = "ledger-" + i + "-" + j; byte[] value = newLogger.readEntry(i, j, positions[i][j]); ByteBuffer buf = ByteBuffer.wrap(value); long ledgerId = buf.getLong(); long entryId = buf.getLong(); byte[] data = new byte[buf.remaining()]; buf.get(data); assertEquals(i, ledgerId); assertEquals(j, entryId); assertEquals(expectedValue, new String(data)); } } }
From source file:ch.usi.da.paxos.ring.LearnerRole.java
public void deliver(RingManager fromRing, Message m) { /*if(logger.isDebugEnabled()){ logger.debug("learner " + ring.getNodeID() + " received " + m); }*///w ww . ja v a2s.c o m if (m.getType() == MessageType.Decision) { Decision d = null; if (learned.get(m.getValue().getID()) != null) { // value was previously learned with an other message d = new Decision(fromRing.getRingID(), m.getInstance(), m.getBallot(), learned.get(m.getValue().getID())); learned.remove(m.getValue().getID()); } else { d = new Decision(fromRing.getRingID(), m.getInstance(), m.getBallot(), m.getValue()); } if (d != null && d.getValue().getValue().length > 0) { if (!recovered && !recovery) { next_instance = d.getInstance(); delivered_instance = d.getInstance() - 1; logger.info("Learner start from instance " + d.getInstance() + " without recovery"); } if (d.getInstance() == next_instance) { delivery.add(d); next_instance++; } else { int pos = findPos(d.getInstance()); if (pos >= 0) { delivery.add(pos, d); if (pos == delivery.size() - 1) { next_instance = d.getInstance().longValue() + 1; } } } // deliver sorted instances Decision head = null; while ((head = delivery.peek()) != null && head.getInstance() - 1 <= delivered_instance) { if (head.getInstance() - 1 == delivered_instance) { recovered = true; Decision de = delivery.poll(); delivered_instance = de.getInstance(); if (auto_trim) { safe_instance = delivered_instance; } deliver_bytes = deliver_bytes + de.getValue().getValue().length; if (de.getValue().isSkip()) { try { if (logger.isTraceEnabled()) { logger.trace("Learner received SKIP " + de.getRing() + " " + de.getInstance() + " " + new String(de.getValue().getValue())); } latencies.addValue(System.currentTimeMillis() - Long.parseLong(d.getValue().getID().split(":")[1])); if (latencies.getN() >= median_window) { latency_to_coordinator = (int) latencies.getPercentile(50); } } catch (Exception e) { } } if (de.getValue().isBatch()) { batch_count++; ByteBuffer buffer = ByteBuffer.wrap(de.getValue().getValue()); while (buffer.remaining() > 0) { try { Message n = Message.fromBuffer(buffer); // decision from a batch will contain same instance number for all entries !!! Decision bd = new Decision(fromRing.getRingID(), m.getInstance(), m.getBallot(), n.getValue()); deliver_count++; if (!delivered.contains(bd.getValue().getID())) { values.add(bd); delivered.add(bd.getValue().getID()); } } catch (Exception e) { logger.error("Learner could not de-serialize batch message!" + e); } } } else { deliver_count++; if (!delivered.contains(de.getValue().getID())) { values.add(de); delivered.add(de.getValue().getID()); } } } else { delivery.poll(); // remove duplicate } } } } else if (m.getType() == MessageType.Safe) { Value v = null; if (m.getValue().getValue().length == 0) { v = new Value(m.getValue().getID(), String.valueOf(safe_instance).getBytes()); } else { String s = new String(m.getValue().getValue()); v = new Value(m.getValue().getID(), (s + ";" + String.valueOf(safe_instance)).getBytes()); } Message n = new Message(m.getInstance(), m.getSender(), m.getReceiver(), m.getType(), m.getBallot(), m.getBallot(), v); n.setVoteCount(m.getVoteCount() + 1); ring.getNetwork().send(n); } else if (m.getType() == MessageType.Trim) { highest_online_instance = m.getInstance() + 1; if (!recovered) { // recover only what is available delivered_instance = highest_online_instance == 0 ? highest_online_instance : highest_online_instance - 1; recovered = true; } logger.debug("Learner notified last highest_online_instance: " + highest_online_instance); } else { if (learned.get(m.getValue().getID()) == null) { learned.put(m.getValue().getID(), m.getValue()); } } }
From source file:org.eclipse.jgit.lfs.server.fs.LfsServerTest.java
private void checkResponseStatus(HttpResponse response) { StatusLine statusLine = response.getStatusLine(); int status = statusLine.getStatusCode(); if (statusLine.getStatusCode() >= 400) { String error;/*from ww w . ja va2 s .co m*/ try { ByteBuffer buf = IO.readWholeStream(new BufferedInputStream(response.getEntity().getContent()), 1024); if (buf.hasArray()) { error = new String(buf.array(), buf.arrayOffset() + buf.position(), buf.remaining(), UTF_8); } else { final byte[] b = new byte[buf.remaining()]; buf.duplicate().get(b); error = new String(b, UTF_8); } } catch (IOException e) { error = statusLine.getReasonPhrase(); } throw new RuntimeException("Status: " + status + " " + error); } assertEquals(200, status); }