List of usage examples for java.nio ByteBuffer remaining
public final int remaining()
From source file:org.apache.cassandra.db.marshal.UUIDType.java
public int compare(ByteBuffer b1, ByteBuffer b2) { // Compare for length if ((b1 == null) || (b1.remaining() < 16)) { return ((b2 == null) || (b2.remaining() < 16)) ? 0 : -1; }// w ww . j a v a 2s . c om if ((b2 == null) || (b2.remaining() < 16)) { return 1; } int s1 = b1.position(); int s2 = b2.position(); // Compare versions int v1 = (b1.get(s1 + 6) >> 4) & 0x0f; int v2 = (b2.get(s2 + 6) >> 4) & 0x0f; if (v1 != v2) { return v1 - v2; } // Compare timestamps for version 1 if (v1 == 1) { // if both time-based, compare as timestamps int c = compareTimestampBytes(b1, b2); if (c != 0) { return c; } } // Compare the two byte arrays starting from the first // byte in the sequence until an inequality is // found. This should provide equivalent results // to the comparison performed by the RFC 4122 // Appendix A - Sample Implementation. // Note: java.util.UUID.compareTo is not a lexical // comparison for (int i = 0; i < 16; i++) { int c = ((b1.get(s1 + i)) & 0xFF) - ((b2.get(s2 + i)) & 0xFF); if (c != 0) { return c; } } return 0; }
From source file:com.github.cambierr.lorawanpacket.semtech.PushData.java
public PushData(byte[] _randoms, ByteBuffer _raw) throws MalformedPacketException { super(_randoms, PacketType.PUSH_DATA); _raw.order(ByteOrder.LITTLE_ENDIAN); if (_raw.remaining() < 8) { throw new MalformedPacketException("too short"); }/*from w w w.j a v a2s.c o m*/ gatewayEui = new byte[8]; _raw.get(gatewayEui); byte[] json = new byte[_raw.remaining()]; _raw.get(json); JSONObject jo; try { jo = new JSONObject(new String(json)); } catch (JSONException ex) { throw new MalformedPacketException("malformed json"); } stats = new ArrayList<>(); rxpks = new ArrayList<>(); if (jo.has("rxpk")) { if (!jo.get("rxpk").getClass().equals(JSONArray.class)) { throw new MalformedPacketException("malformed json (rxpk)"); } JSONArray rxpk = jo.getJSONArray("rxpk"); for (int i = 0; i < rxpk.length(); i++) { rxpks.add(new Rxpk(rxpk.getJSONObject(i))); } } if (jo.has("stat")) { if (!jo.get("stat").getClass().equals(JSONArray.class)) { throw new MalformedPacketException("malformed json (stat)"); } JSONArray stat = jo.getJSONArray("stat"); for (int i = 0; i < stat.length(); i++) { stats.add(new Stat(stat.getJSONObject(i))); } } }
From source file:byps.test.servlet.MyRemoteStreams.java
@Override public TreeMap<Integer, InputStream> getImages() throws RemoteException { if (log.isDebugEnabled()) log.debug("getImages("); Map<Integer, ByteBuffer> mapStreamBytes = this.mapStreamBytes; if (mapStreamBytes == null) return null; TreeMap<Integer, InputStream> map = new TreeMap<Integer, InputStream>(); for (Integer k : mapStreamBytes.keySet()) { ByteBuffer buf = mapStreamBytes.get(k); InputStream istrm = new ByteArrayInputStream(buf.array(), buf.position(), buf.remaining()); map.put(k, istrm);/*from w w w .j av a 2 s .c o m*/ } if (log.isDebugEnabled()) log.debug(")getImages=" + map); return map; }
From source file:gridool.communication.transport.tcp.GridSocketPoolingClient.java
public void sendMessage(SocketAddress sockAddr, GridCommunicationMessage msg) throws GridException { final ByteBuffer buf = toBuffer(msg); final ByteChannel channel = sockPool.borrowObject(sockAddr); if (LOG.isDebugEnabled()) { LOG.debug("Sending a message [" + msg.getMessageId() + " (" + buf.remaining() + " bytes)] to a node [" + sockAddr + "] using a channel [" + channel + ']'); }//from w w w . j a va 2 s . com final int written; try { written = NIOUtils.countingWriteFully(channel, buf); sockPool.returnObject(sockAddr, channel); } catch (IOException ioe) { IOUtils.closeQuietly(channel); final String errmsg = "Failed to send a GridCommunicationMessage [" + msg + "] to host [" + sockAddr + ']'; LOG.error(errmsg, ioe); throw new GridException(errmsg, ioe); } catch (Throwable e) { IOUtils.closeQuietly(channel); LOG.fatal(e.getMessage(), e); throw new GridException("Unexpected exception was caused", e); } if (LOG.isDebugEnabled()) { LOG.debug("Succeeded to send a GridCommunicationMessage (" + written + " bytes) to host [" + sockAddr + "]"); } }
From source file:com.navercorp.pinpoint.common.server.bo.serializer.trace.v1.SpanBoTest.java
@Test public void serialize2_V1() { SpanBo spanBo = new SpanBo(); spanBo.setAgentId("agent"); String service = createString(5); spanBo.setApplicationId(service);// w w w . j a va2s . c o m String endPoint = createString(127); spanBo.setEndPoint(endPoint); String rpc = createString(255); spanBo.setRpc(rpc); spanBo.setServiceType(ServiceType.STAND_ALONE.getCode()); spanBo.setApplicationServiceType(ServiceType.UNKNOWN.getCode()); final ByteBuffer bytes = spanSerializer.writeColumnValue(spanBo); SpanBo newSpanBo = new SpanBo(); Buffer valueBuffer = new OffsetFixedBuffer(bytes.array(), bytes.arrayOffset(), bytes.remaining()); int i = spanDecoder.readSpan(newSpanBo, valueBuffer); logger.debug("length:{}", i); Assert.assertEquals(bytes.limit(), i); Assert.assertEquals(spanBo.getServiceType(), spanBo.getServiceType()); Assert.assertEquals(spanBo.getApplicationServiceType(), spanBo.getApplicationServiceType()); }
From source file:com.taobao.common.tfs.comm.TfsClient.java
public void invokeAsync(final BasePacket packet, final long timeout, ResponseListener listener) { if (isDebugEnabled) { log.debug("send request [" + packet.getChid() + "] async,time is:" + System.currentTimeMillis()); }//ww w . jav a 2 s . c o m if (minTimeout > timeout) { minTimeout = timeout; } final ResponseCallbackTask callbackTask = new ResponseCallbackTask(packet.getSeqId(), listener, timeout); callbackTasks.put(packet.getChid(), callbackTask); ByteBuffer bb = packet.getByteBuffer(); bb.flip(); byte[] data = new byte[bb.remaining()]; bb.get(data); WriteFuture writeFuture = session.write(data); writeFuture.addListener(new IoFutureListener() { public void operationComplete(IoFuture future) { WriteFuture wfuture = (WriteFuture) future; if (wfuture.isWritten()) { return; } String error = "send message to tfs server error [" + packet.getChid() + "], tfs server: " + session.getRemoteAddress() + ", maybe because this connection closed: " + !session.isConnected(); callbackTask.setResponse(new TfsException(error)); // close this session if (session.isConnected()) { session.close(); } else { TfsClientFactory.getInstance().removeClient(key); } } }); }
From source file:org.apache.cassandra.dht.AbstractByteOrderedPartitioner.java
/** * Convert a byte array containing the most significant of 'sigbytes' bytes * representing a big-endian magnitude into a BigInteger. *//*from w ww . j av a 2s .c o m*/ private BigInteger bigForBytes(ByteBuffer bytes, int sigbytes) { byte[] b = new byte[sigbytes]; Arrays.fill(b, (byte) 0); // append zeros ByteBufferUtil.arrayCopy(bytes, bytes.position(), b, 0, bytes.remaining()); return new BigInteger(1, b); }
From source file:org.apache.james.protocols.smtp.core.DataLineMessageHookHandler.java
private byte[] readBytes(ByteBuffer line) { line.rewind();/* w w w. j a v a 2 s . c o m*/ byte[] bline; if (line.hasArray()) { bline = line.array(); } else { bline = new byte[line.remaining()]; line.get(bline); } return bline; }
From source file:com.taobao.tair.comm.TairClient.java
public void invokeAsync(final BasePacket packet, final long timeout, ResponseListener listener) { if (isDebugEnabled) { LOGGER.debug("send request [" + packet.getChid() + "] async,time is:" + System.currentTimeMillis()); }/* w w w . ja v a 2s . c o m*/ if (minTimeout > timeout) { minTimeout = timeout; } final ResponseCallbackTask callbackTask = new ResponseCallbackTask(packet.getChid(), listener, this.session, timeout); callbackTasks.put(packet.getChid(), callbackTask); ByteBuffer bb = packet.getByteBuffer(); bb.flip(); byte[] data = new byte[bb.remaining()]; bb.get(data); WriteFuture writeFuture = session.write(data); writeFuture.addListener(new IoFutureListener() { public void operationComplete(IoFuture future) { WriteFuture wfuture = (WriteFuture) future; if (wfuture.isWritten()) { return; } String error = "send message to tair server error [" + packet.getChid() + "], tair server: " + session.getRemoteAddress() + ", maybe because this connection closed :" + !session.isConnected(); LOGGER.warn(error); callbackTask.setResponse(new TairClientException(error)); // close this session if (session.isConnected()) session.close(); else clientFactory.removeClient(key); } }); }
From source file:org.apache.maven.plugin.surefire.report.Utf8RecodingDeferredFileOutputStream.java
public synchronized void write(byte[] buf, int off, int len) throws IOException { if (closed) { return;//from w w w. j a va 2s . co m } if (!Charset.defaultCharset().equals(UTF8)) { CharBuffer decodedFromDefaultCharset = Charset.defaultCharset().decode(ByteBuffer.wrap(buf, off, len)); ByteBuffer utf8Encoded = UTF8.encode(decodedFromDefaultCharset); if (utf8Encoded.hasArray()) { byte[] convertedBytes = utf8Encoded.array(); deferredFileOutputStream.write(convertedBytes, utf8Encoded.position(), utf8Encoded.remaining()); } else { byte[] convertedBytes = new byte[utf8Encoded.remaining()]; utf8Encoded.get(convertedBytes, 0, utf8Encoded.remaining()); deferredFileOutputStream.write(convertedBytes, 0, convertedBytes.length); } } else { deferredFileOutputStream.write(buf, off, len); } }