List of usage examples for java.nio ByteBuffer remaining
public final int remaining()
From source file:org.apache.cassandra.db.context.CounterContextTest.java
@Test public void testCreate() { ByteBuffer context = cc.create(4); assert context.remaining() == stepLength + 4; }
From source file:com.pinterest.terrapin.storage.HFileReader.java
/** * Issues an HFile lookup on the underlying HFile.Reader. This is protected * for testing./*from ww w . java2 s. co m*/ */ protected Pair<ByteBuffer, Pair<ByteBuffer, Throwable>> getValueFromHFile(ByteBuffer key) { try { HFileScanner scanner = reader.getScanner(true, true, false); KeyValue kv = buildKeyValueForLookup(BytesUtil.readBytesFromByteBufferWithoutConsume(key)); int code = scanner.seekTo(kv.getKey()); ByteBuffer value = null; if (code == 0) { value = ByteBuffer.wrap(scanner.getKeyValue().getValue()); if (this.sizeStatsKey != null) { Stats.addMetric(this.sizeStatsKey, value.remaining()); } Stats.addMetric("value-size", value.remaining()); } else { Stats.incr("not-found"); if (this.notFoundStatsKey != null) { Stats.incr(this.notFoundStatsKey); } } return new ImmutablePair(key, new ImmutablePair(value, null)); } catch (Throwable t) { return new ImmutablePair(key, new ImmutablePair(null, t)); } }
From source file:org.apache.nutch.indexer.html.HtmlIndexingFilter.java
private NutchDocument addRawContent(NutchDocument doc, WebPage page, String url) { ByteBuffer raw = page.getContent(); if (raw != null) { if (LOG.isInfoEnabled()) { LOG.info("Html indexing for: " + url.toString()); }//from w w w. j a va2 s.c om ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(raw.array(), raw.arrayOffset() + raw.position(), raw.remaining()); Scanner scanner = new Scanner(arrayInputStream); scanner.useDelimiter("\\Z");//To read all scanner content in one String String data = ""; if (scanner.hasNext()) { data = scanner.next(); } doc.add("rawcontent", StringUtil.cleanField(data)); } return doc; }
From source file:ch.usi.da.paxos.ring.ProposerRole.java
public void deliver(RingManager fromRing, Message m) { /*if(logger.isDebugEnabled()){ logger.debug("proposer " + ring.getNodeID() + " received " + m); }*//*from www . ja va 2s . c o m*/ if (m.getType() == MessageType.Decision) { String ID = m.getValue().getID(); if (proposals.containsKey(ID)) { Proposal p = proposals.get(ID); Value v = p.getValue(); if (m.getValue().equals(v)) { // compared by ID if (v.isBatch()) { ByteBuffer buffer = ByteBuffer.wrap(v.getValue()); while (buffer.remaining() > 0) { try { Message n = Message.fromBuffer(buffer); set_decision(fromRing, n, n.getValue()); } catch (Exception e) { logger.error("Proposer could not de-serialize batch message!" + e); } } } else { set_decision(fromRing, m, v); } } else { logger.error("Proposer received Decision with different values for same instance " + m.getInstance() + "!"); } proposals.remove(ID); } } }
From source file:Base64Encoder.java
/** * Flushes this encoder.//from ww w . j ava 2s . co m * * <p> The default implementation of this method does nothing, and always * returns {@link CoderResult#UNDERFLOW}. This method should be overridden * by encoders that may need to write final bytes to the output buffer * once the entire input sequence has been read. </p> * * @param out * The output byte buffer * * @return A coder-result object, either {@link CoderResult#UNDERFLOW} or * {@link CoderResult#OVERFLOW} */ protected java.nio.charset.CoderResult implFlush(java.nio.ByteBuffer out) { if (encState != 0 && encState != 4) throw new IllegalArgumentException("Base-64 text ends prematurely"); if (excessByte == null) { implReset(); return CoderResult.UNDERFLOW; } if (out.remaining() > 0) { out.put(excessByte.byteValue()); implReset(); return CoderResult.UNDERFLOW; } else return CoderResult.OVERFLOW; }
From source file:org.apache.beam.sdk.io.AvroSource.java
/** * Reads the {@link AvroMetadata} from the header of an Avro file. * * <p>This method parses the header of an Avro * <a href="https://avro.apache.org/docs/1.7.7/spec.html#Object+Container+Files"> * Object Container File</a>.//from ww w.j a v a 2 s . c o m * * @throws IOException if the file is an invalid format. */ @VisibleForTesting static AvroMetadata readMetadataFromFile(ResourceId fileResource) throws IOException { String codec = null; String schemaString = null; byte[] syncMarker; try (InputStream stream = Channels.newInputStream(FileSystems.open(fileResource))) { BinaryDecoder decoder = DecoderFactory.get().binaryDecoder(stream, null); // The header of an object container file begins with a four-byte magic number, followed // by the file metadata (including the schema and codec), encoded as a map. Finally, the // header ends with the file's 16-byte sync marker. // See https://avro.apache.org/docs/1.7.7/spec.html#Object+Container+Files for details on // the encoding of container files. // Read the magic number. byte[] magic = new byte[DataFileConstants.MAGIC.length]; decoder.readFixed(magic); if (!Arrays.equals(magic, DataFileConstants.MAGIC)) { throw new IOException("Missing Avro file signature: " + fileResource); } // Read the metadata to find the codec and schema. ByteBuffer valueBuffer = ByteBuffer.allocate(512); long numRecords = decoder.readMapStart(); while (numRecords > 0) { for (long recordIndex = 0; recordIndex < numRecords; recordIndex++) { String key = decoder.readString(); // readBytes() clears the buffer and returns a buffer where: // - position is the start of the bytes read // - limit is the end of the bytes read valueBuffer = decoder.readBytes(valueBuffer); byte[] bytes = new byte[valueBuffer.remaining()]; valueBuffer.get(bytes); if (key.equals(DataFileConstants.CODEC)) { codec = new String(bytes, "UTF-8"); } else if (key.equals(DataFileConstants.SCHEMA)) { schemaString = new String(bytes, "UTF-8"); } } numRecords = decoder.mapNext(); } if (codec == null) { codec = DataFileConstants.NULL_CODEC; } // Finally, read the sync marker. syncMarker = new byte[DataFileConstants.SYNC_SIZE]; decoder.readFixed(syncMarker); } checkState(schemaString != null, "No schema present in Avro file metadata %s", fileResource); return new AvroMetadata(syncMarker, codec, schemaString); }
From source file:org.apache.james.smtpserver.DataLineJamesMessageHookHandler.java
public Response onLine(SMTPSession session, ByteBuffer lineByteBuffer, LineHandler<SMTPSession> next) { byte[] line = new byte[lineByteBuffer.remaining()]; lineByteBuffer.get(line, 0, line.length); MimeMessageInputStreamSource mmiss = (MimeMessageInputStreamSource) session .getAttachment(SMTPConstants.DATA_MIMEMESSAGE_STREAMSOURCE, State.Transaction); try {/*from w w w . j a v a 2s . c o m*/ OutputStream out = mmiss.getWritableOutputStream(); // 46 is "." // Stream terminated if (line.length == 3 && line[0] == 46) { out.flush(); out.close(); @SuppressWarnings("unchecked") List<MailAddress> recipientCollection = (List<MailAddress>) session .getAttachment(SMTPSession.RCPT_LIST, State.Transaction); MailAddress mailAddress = (MailAddress) session.getAttachment(SMTPSession.SENDER, State.Transaction); List<org.apache.mailet.MailAddress> rcpts = new ArrayList<org.apache.mailet.MailAddress>(); for (MailAddress address : recipientCollection) { rcpts.add(new MailetMailAddressAdapter(address)); } MailetMailAddressAdapter mailetMailAddressAdapter = null; if (mailAddress != MailAddress.nullSender()) { mailetMailAddressAdapter = new MailetMailAddressAdapter(mailAddress); } MailImpl mail = new MailImpl(MailImpl.getId(), mailetMailAddressAdapter, rcpts); // store mail in the session so we can be sure it get disposed later session.setAttachment(SMTPConstants.MAIL, mail, State.Transaction); MimeMessageCopyOnWriteProxy mimeMessageCopyOnWriteProxy = null; try { mimeMessageCopyOnWriteProxy = new MimeMessageCopyOnWriteProxy(mmiss); mail.setMessage(mimeMessageCopyOnWriteProxy); Response response = processExtensions(session, mail); session.popLineHandler(); return response; } catch (MessagingException e) { // TODO probably return a temporary problem session.getLogger().info("Unexpected error handling DATA stream", e); return new SMTPResponse(SMTPRetCode.LOCAL_ERROR, "Unexpected error handling DATA stream."); } finally { LifecycleUtil.dispose(mimeMessageCopyOnWriteProxy); LifecycleUtil.dispose(mmiss); LifecycleUtil.dispose(mail); } // DotStuffing. } else if (line[0] == 46 && line[1] == 46) { out.write(line, 1, line.length - 1); // Standard write } else { // TODO: maybe we should handle the Header/Body recognition here // and if needed let a filter to cache the headers to apply some // transformation before writing them to output. out.write(line); } } catch (IOException e) { LifecycleUtil.dispose(mmiss); SMTPResponse response = new SMTPResponse(SMTPRetCode.LOCAL_ERROR, DSNStatus.getStatus(DSNStatus.TRANSIENT, DSNStatus.UNDEFINED_STATUS) + " Error processing message: " + e.getMessage()); session.getLogger().error("Unknown error occurred while processing DATA.", e); return response; } catch (AddressException e) { LifecycleUtil.dispose(mmiss); SMTPResponse response = new SMTPResponse(SMTPRetCode.LOCAL_ERROR, DSNStatus.getStatus(DSNStatus.TRANSIENT, DSNStatus.UNDEFINED_STATUS) + " Error processing message: " + e.getMessage()); session.getLogger().error("Invalid email address while processing DATA.", e); return response; } return null; }
From source file:fr.letroll.ttorrentandroid.common.Torrent.java
public boolean isPieceValid(@Nonnegative int index, @Nonnull ByteBuffer data) { if (data.remaining() != getPieceLength(index)) throw new IllegalArgumentException("Validating piece " + index + " expected " + getPieceLength(index) + ", not " + data.remaining()); MessageDigest digest = DigestUtils.getSha1Digest(); digest.update(data);//from w w w.jav a 2 s. c om return Arrays.equals(digest.digest(), getPieceHash(index)); }
From source file:org.apache.cassandra.index.sasi.plan.Expression.java
public boolean isSatisfiedBy(ByteBuffer value) { if (!TypeUtil.isValid(value, validator)) { int size = value.remaining(); if ((value = TypeUtil.tryUpcast(value, validator)) == null) { logger.error("Can't cast value for {} to size accepted by {}, value size is {}.", index.getColumnName(), validator, FBUtilities.prettyPrintMemory(size)); return false; }// w ww.j a va2 s . co m } if (lower != null) { // suffix check if (isLiteral) { if (!validateStringValue(value, lower.value)) return false; } else { // range or (not-)equals - (mainly) for numeric values int cmp = validator.compare(lower.value, value); // in case of (NOT_)EQ lower == upper if (operation == Op.EQ || operation == Op.NOT_EQ) return cmp == 0; if (cmp > 0 || (cmp == 0 && !lower.inclusive)) return false; } } if (upper != null && lower != upper) { // string (prefix or suffix) check if (isLiteral) { if (!validateStringValue(value, upper.value)) return false; } else { // range - mainly for numeric values int cmp = validator.compare(upper.value, value); if (cmp < 0 || (cmp == 0 && !upper.inclusive)) return false; } } // as a last step let's check exclusions for the given field, // this covers EQ/RANGE with exclusions. for (ByteBuffer term : exclusions) { if (isLiteral && validateStringValue(value, term)) return false; else if (validator.compare(term, value) == 0) return false; } return true; }
From source file:com.offbynull.portmapper.pcp.MapPcpRequest.java
/** * Constructs a {@link MapPcpRequest} object. * @param mappingNonce random value used to map requests to responses * @param protocol IANA protocol number ({@code 0} is valid, see Javadoc header) * @param internalPort internal port ({@code 0} is valid, see Javadoc header) * @param suggestedExternalPort suggested external port ({@code 0} for no preference) * @param suggestedExternalIpAddress suggested external IP address ({@code ::} for no preference) * @param lifetime requested lifetime in seconds * @param options PCP options to use//ww w . j a v a2 s .c o m * @throws NullPointerException if any argument is {@code null} or contains {@code null} * @throws IllegalArgumentException if any numeric argument is negative, or if {@code protocol > 255}, or if * {@code internalPort > 65535}, or if {@code suggestedExternalPort > 65535}, or if {@code mappingNonce} does not have {@code 12} bytes * remaining, or if {@code protocol == 0} but {@code internalPort != 0}, or if {@code internalPort == 0} but {@code lifetime != 0} */ public MapPcpRequest(ByteBuffer mappingNonce, int protocol, int internalPort, int suggestedExternalPort, InetAddress suggestedExternalIpAddress, long lifetime, PcpOption... options) { super(1, lifetime, options); Validate.notNull(mappingNonce); Validate.isTrue(mappingNonce.remaining() == 12); Validate.inclusiveBetween(0, 255, protocol); Validate.inclusiveBetween(0, 65535, internalPort); Validate.inclusiveBetween(0, 65535, suggestedExternalPort); Validate.notNull(suggestedExternalIpAddress); if (protocol == 0) { Validate.isTrue(internalPort == 0); } if (internalPort == 0) { Validate.isTrue(super.getLifetime() == 0L); } this.mappingNonce = ByteBufferUtils.copyContents(mappingNonce).asReadOnlyBuffer(); this.protocol = protocol; this.internalPort = internalPort; this.suggestedExternalPort = suggestedExternalPort; this.suggestedExternalIpAddress = suggestedExternalIpAddress; // for any ipv4 must be ::ffff:0:0, for any ipv6 must be :: }