List of usage examples for java.nio ByteBuffer remaining
public final int remaining()
From source file:com.netflix.astyanax.thrift.ThriftUtils.java
private static String base64Encode(ByteBuffer bb) { if (bb == null) { return ""; }// w w w.ja v a2s .c o m byte[] nbb = new byte[bb.remaining()]; bb.duplicate().get(nbb, 0, bb.remaining()); return Base64.encodeBase64String(nbb); }
From source file:org.apache.jackrabbit.oak.plugins.document.persistentCache.BroadcastTest.java
private static void listen() throws InterruptedException { String config = "key 123"; ConsoleAppender<ILoggingEvent> ca = new ConsoleAppender<ILoggingEvent>(); LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); ca.setContext(lc);/* w w w .ja v a 2 s . co m*/ PatternLayout pl = new PatternLayout(); pl.setPattern("%msg%n"); pl.setContext(lc); pl.start(); ca.setLayout(pl); ca.start(); ch.qos.logback.classic.Logger logger = (ch.qos.logback.classic.Logger) LoggerFactory .getLogger(TCPBroadcaster.class); logger.addAppender(ca); logger.setLevel(Level.DEBUG); TCPBroadcaster receiver = new TCPBroadcaster(config); receiver.addListener(new Broadcaster.Listener() { @Override public void receive(ByteBuffer buff) { int end = buff.position(); StringBuilder sb = new StringBuilder(); while (buff.remaining() > 0) { char c = (char) buff.get(); if (c >= ' ' && c < 128) { sb.append(c); } else if (c <= 9) { sb.append((char) ('0' + c)); } else { sb.append('.'); } } String dateTime = new Timestamp(System.currentTimeMillis()).toString().substring(0, 19); System.out.println(dateTime + " Received " + sb); buff.position(end); } }); Random r = new Random(); int x = r.nextInt(); System.out.println("Sending " + x); for (int i = 0; i < 10; i++) { Thread.sleep(10); ByteBuffer buff = ByteBuffer.allocate(1024); buff.putInt(0); buff.putInt(x); buff.put(new byte[100]); buff.flip(); receiver.send(buff); if (!receiver.isRunning()) { System.out.println("Did not start or already stopped"); break; } } Thread.sleep(Integer.MAX_VALUE); }
From source file:gobblin.util.io.StreamUtils.java
/** * Reads the full contents of a ByteBuffer and writes them to an OutputStream. The ByteBuffer is * consumed by this operation; eg in.remaining() will be 0 after it completes successfully. * @param in ByteBuffer to write into the OutputStream * @param out Destination stream/*from www.j a va2 s.co m*/ * @throws IOException If there is an error writing into the OutputStream */ public static void byteBufferToOutputStream(ByteBuffer in, OutputStream out) throws IOException { final int BUF_SIZE = 8192; if (in.hasArray()) { out.write(in.array(), in.arrayOffset() + in.position(), in.remaining()); } else { final byte[] b = new byte[Math.min(in.remaining(), BUF_SIZE)]; while (in.remaining() > 0) { int bytesToRead = Math.min(in.remaining(), BUF_SIZE); in.get(b, 0, bytesToRead); out.write(b, 0, bytesToRead); } } }
From source file:com.icloud.framework.core.util.FBUtilities.java
public static void writeShortByteArray(ByteBuffer name, DataOutput out) { int length = name.remaining(); assert 0 <= length && length <= MAX_UNSIGNED_SHORT; try {/*from ww w .ja va 2 s .c o m*/ out.writeByte((length >> 8) & 0xFF); out.writeByte(length & 0xFF); out.write(name.array(), name.position() + name.arrayOffset(), name.remaining()); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:org.apache.spark.network.util.JavaUtils.java
/** * Returns a byte array with the buffer's contents, trying to avoid copying the data if * possible./*from w w w .j a v a 2 s .c o m*/ */ public static byte[] bufferToArray(ByteBuffer buffer) { if (buffer.hasArray() && buffer.arrayOffset() == 0 && buffer.array().length == buffer.remaining()) { return buffer.array(); } else { byte[] bytes = new byte[buffer.remaining()]; buffer.get(bytes); return bytes; } }
From source file:com.ebay.pulsar.analytics.cache.MemcachedCache.java
private static byte[] deserializeValue(NamedKey key, byte[] bytes) { ByteBuffer buf = ByteBuffer.wrap(bytes); final int keyLength = buf.getInt(); byte[] keyBytes = new byte[keyLength]; buf.get(keyBytes);/*from www. j a v a 2 s . c o m*/ byte[] value = new byte[buf.remaining()]; buf.get(value); Preconditions.checkState(Arrays.equals(keyBytes, key.toByteArray()), "Keys do not match, possible hash collision?"); return value; }
From source file:com.amazonaws.services.kinesis.clientlibrary.types.UserRecord.java
/** * This method deaggregates the given list of Amazon Kinesis records into a * list of KPL user records. Any KPL user records whose explicit hash key or * partition key falls outside the range of the startingHashKey and the * endingHashKey are discarded from the resulting list. This method will * then return the resulting list of KPL user records. * /*from ww w . j a va 2 s .co m*/ * @param records * A list of Amazon Kinesis records, each possibly aggregated. * @param startingHashKey * A BigInteger representing the starting hash key that the * explicit hash keys or partition keys of retained resulting KPL * user records must be greater than or equal to. * @param endingHashKey * A BigInteger representing the ending hash key that the the * explicit hash keys or partition keys of retained resulting KPL * user records must be smaller than or equal to. * @return A resulting list of KPL user records whose explicit hash keys or * partition keys fall within the range of the startingHashKey and * the endingHashKey. */ // CHECKSTYLE:OFF NPathComplexity public static List<UserRecord> deaggregate(List<Record> records, BigInteger startingHashKey, BigInteger endingHashKey) { List<UserRecord> result = new ArrayList<>(); byte[] magic = new byte[AGGREGATED_RECORD_MAGIC.length]; byte[] digest = new byte[DIGEST_SIZE]; for (Record r : records) { boolean isAggregated = true; long subSeqNum = 0; ByteBuffer bb = r.getData(); if (bb.remaining() >= magic.length) { bb.get(magic); } else { isAggregated = false; } if (!Arrays.equals(AGGREGATED_RECORD_MAGIC, magic) || bb.remaining() <= DIGEST_SIZE) { isAggregated = false; } if (isAggregated) { int oldLimit = bb.limit(); bb.limit(oldLimit - DIGEST_SIZE); byte[] messageData = new byte[bb.remaining()]; bb.get(messageData); bb.limit(oldLimit); bb.get(digest); byte[] calculatedDigest = md5(messageData); if (!Arrays.equals(digest, calculatedDigest)) { isAggregated = false; } else { try { Messages.AggregatedRecord ar = Messages.AggregatedRecord.parseFrom(messageData); List<String> pks = ar.getPartitionKeyTableList(); List<String> ehks = ar.getExplicitHashKeyTableList(); long aat = r.getApproximateArrivalTimestamp() == null ? -1 : r.getApproximateArrivalTimestamp().getTime(); try { int recordsInCurrRecord = 0; for (Messages.Record mr : ar.getRecordsList()) { String explicitHashKey = null; String partitionKey = pks.get((int) mr.getPartitionKeyIndex()); if (mr.hasExplicitHashKeyIndex()) { explicitHashKey = ehks.get((int) mr.getExplicitHashKeyIndex()); } BigInteger effectiveHashKey = explicitHashKey != null ? new BigInteger(explicitHashKey) : new BigInteger(1, md5(partitionKey.getBytes("UTF-8"))); if (effectiveHashKey.compareTo(startingHashKey) < 0 || effectiveHashKey.compareTo(endingHashKey) > 0) { for (int toRemove = 0; toRemove < recordsInCurrRecord; ++toRemove) { result.remove(result.size() - 1); } break; } ++recordsInCurrRecord; Record record = new Record().withData(ByteBuffer.wrap(mr.getData().toByteArray())) .withPartitionKey(partitionKey).withSequenceNumber(r.getSequenceNumber()) .withApproximateArrivalTimestamp(aat < 0 ? null : new Date(aat)); result.add(new UserRecord(true, record, subSeqNum++, explicitHashKey)); } } catch (Exception e) { StringBuilder sb = new StringBuilder(); sb.append("Unexpected exception during deaggregation, record was:\n"); sb.append("PKS:\n"); for (String s : pks) { sb.append(s).append("\n"); } sb.append("EHKS: \n"); for (String s : ehks) { sb.append(s).append("\n"); } for (Messages.Record mr : ar.getRecordsList()) { sb.append("Record: [hasEhk=").append(mr.hasExplicitHashKeyIndex()).append(", ") .append("ehkIdx=").append(mr.getExplicitHashKeyIndex()).append(", ") .append("pkIdx=").append(mr.getPartitionKeyIndex()).append(", ") .append("dataLen=").append(mr.getData().toByteArray().length).append("]\n"); } sb.append("Sequence number: ").append(r.getSequenceNumber()).append("\n") .append("Raw data: ") .append(javax.xml.bind.DatatypeConverter.printBase64Binary(messageData)) .append("\n"); LOG.error(sb.toString(), e); } } catch (InvalidProtocolBufferException e) { isAggregated = false; } } } if (!isAggregated) { bb.rewind(); result.add(new UserRecord(r)); } } return result; }
From source file:jp.andeb.obbutil.ObbUtilMain.java
private static boolean doAdd(String[] args) { final CommandLine commandLine; try {// w w w. j av a 2 s . c o m final CommandLineParser parser = new GnuParser(); commandLine = parser.parse(OPTIONS_FOR_ADD, args); } catch (MissingArgumentException e) { System.err.println("??????: " + e.getOption().getOpt()); printUsage(PROGNAME); return false; } catch (MissingOptionException e) { System.err.println("??????: " + e.getMissingOptions()); printUsage(PROGNAME); return false; } catch (UnrecognizedOptionException e) { System.err.println("????: " + e.getOption()); printUsage(PROGNAME); return false; } catch (ParseException e) { System.err.println(e.getMessage()); printUsage(PROGNAME); return false; } final String pkgName = commandLine.getOptionValue(PACKAGE_NAME.getOpt()); final String versionStr = commandLine.getOptionValue(OBB_VERSION.getOpt()); final Integer version = toInteger(versionStr); if (version == null) { System.err.println("??????: " + versionStr); printUsage(PROGNAME); return false; } final boolean isOverlay = commandLine.hasOption(OVERLAY_FLAG.getOpt()); final String saltStr = commandLine.getOptionValue(SALT.getOpt()); final byte[] salt; if (saltStr == null) { salt = null; } else { salt = toByteArray(saltStr, ObbInfoV1.SALT_LENGTH); if (salt == null) { System.err.println("????: " + saltStr); printUsage(PROGNAME); return false; } } final String[] nonRecognizedArgs = commandLine.getArgs(); if (nonRecognizedArgs.length == 0) { System.err.println("????????"); printUsage(PROGNAME); return false; } if (nonRecognizedArgs.length != 1) { System.err.println("???????"); printUsage(PROGNAME); return false; } final File targetFile = new File(nonRecognizedArgs[0]); final RandomAccessFile targetRaFile; try { targetRaFile = new RandomAccessFile(targetFile, "rw"); } catch (FileNotFoundException e) { System.err.println("????: " + targetFile.getPath()); return false; } try { try { final ObbInfoV1 info = ObbInfoV1.fromFile(targetRaFile); System.err.println( "?? OBB ???????: " + info.toString()); return false; } catch (IOException e) { System.err .println("????????: " + targetFile.getPath()); return false; } catch (NotObbException e) { // } int flag = 0; if (isOverlay) { flag |= ObbInfoV1.FLAG_OVERLAY; } if (salt != null) { flag |= ObbInfoV1.FLAG_SALTED; } final ObbInfoV1 obbInfo = new ObbInfoV1(flag, salt, pkgName, version.intValue()); final ByteBuffer obbInfoBytes = obbInfo.toBytes(); // ??? targetRaFile.setLength(targetRaFile.length() + obbInfoBytes.remaining()); targetRaFile.seek(targetRaFile.length() - obbInfoBytes.remaining()); targetRaFile.write(obbInfoBytes.array(), obbInfoBytes.arrayOffset(), obbInfoBytes.remaining()); } catch (IOException e) { System.err.println("OBB ?????????: " + targetFile.getPath()); return false; } finally { try { targetRaFile.close(); } catch (IOException e) { System.err.println("OBB ?????????: " + targetFile.getPath()); return false; } } System.err.println("OBB ??????????: " + targetFile.getPath()); return true; }
From source file:com.buaa.cfs.utils.IOUtils.java
/** * Write a ByteBuffer to a FileChannel at a given offset, handling short writes. * * @param fc The FileChannel to write to * @param buf The input buffer//from w w w .j a v a 2 s .c o m * @param offset The offset in the file to start writing at * * @throws IOException On I/O error */ public static void writeFully(FileChannel fc, ByteBuffer buf, long offset) throws IOException { do { offset += fc.write(buf, offset); } while (buf.remaining() > 0); }
From source file:com.icloud.framework.core.util.FBUtilities.java
public static void writeByteArray(ByteBuffer bytes, DataOutput out) throws IOException { out.writeInt(bytes.remaining()); out.write(bytes.array(), bytes.position() + bytes.arrayOffset(), bytes.remaining()); }