List of usage examples for java.nio ByteBuffer array
public final byte[] array()
From source file:org.sglover.nlp.EntityExtracter.java
private String getContent(ReadableByteChannel channel) throws IOException { StringBuilder sb = new StringBuilder(); ByteBuffer bb = ByteBuffer.allocate(2048); int c = -1;/*ww w. ja v a2 s . co m*/ do { c = channel.read(bb); bb.flip(); bb.clear(); sb.append(new String(bb.array(), "UTF-8")); } while (c != -1); String content = sb.toString(); return content; }
From source file:com.acciente.oacc.sql.internal.StrongCleanablePasswordEncryptor.java
private byte[] getCleanedBytes(char[] password) { final ByteBuffer byteBuffer = StandardCharsets.UTF_8 .encode(CharBuffer.wrap(Normalizer.normalizeToNfc(password))); final byte[] byteArray = new byte[byteBuffer.remaining()]; byteBuffer.get(byteArray);/*from ww w. ja v a 2 s . c om*/ Arrays.fill(byteBuffer.array(), (byte) 0); return byteArray; }
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);// ww w. ja v a2 s. 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.unister.semweb.drums.api.SearchForTest.java
/** * Converts the given <code>value</code> into an byte array. The final byte array will have * <code>overallBytes</code>. */// w w w . jav a 2 s . c o m private byte[] convert(int value, int overallBytes) { ByteBuffer buffer = ByteBuffer.allocate(overallBytes); buffer.putInt(overallBytes - 4, value); buffer.flip(); return buffer.array(); }
From source file:net.phoenix.thrift.hello.ThreadedSelectorTest.java
@Test public void testByteBuffer() throws TException, IOException, InterruptedException { LOG.info("Client starting...."); String name = "World"; // TTransport transport = new TNonblockingSocket("localhost", 9804); TTransport transport = new TFramedTransport(new TSocket("localhost", 9804)); transport.open();// w ww . j a v a 2 s . c o m // TTransport transport = new TTransport(socket); TProtocol protocol = new TBinaryProtocol(transport); HelloService.Client client = new HelloService.Client(protocol); try { Hello.HelloRequest.Builder request = Hello.HelloRequest.newBuilder(); request.setName(name); Hello.User.Builder user = Hello.User.newBuilder(); user.setName("hello"); user.setPassword("hello"); request.setUser(user.build()); ByteBuffer requestBuffer = ByteBuffer.wrap(request.build().toByteArray()); ByteBuffer buffer = client.hello(requestBuffer); Hello.HelloResponse response = Hello.HelloResponse .parseFrom(ArrayUtils.subarray(buffer.array(), buffer.position(), buffer.limit())); String message = response.getMessage(); assertEquals(message, "Hello " + name); // System.out.println(message); } finally { transport.close(); } }
From source file:eu.pursuit.core.ItemName.java
public byte[] toByteArray() { int length = 0; if (scopeId != null) { length += scopeId.getLength();/* w w w .j a va 2 s .c om*/ } if (rendezvousId != null) { length += rendezvousId.getId().length; } ByteBuffer buffer = ByteBuffer.allocate(length); if (scopeId != null) { scopeId.fill(buffer); } if (rendezvousId != null) { buffer.put(rendezvousId.getId()); } return buffer.array(); }
From source file:com.baynote.kafka.hadoop.KafkaRecordReader.java
/** * {@inheritDoc}//from w w w. j a v a 2 s . co m */ @Override public boolean nextKeyValue() throws IOException, InterruptedException { if (key == null) { key = new LongWritable(); } if (value == null) { value = new BytesWritable(); } if (continueItr()) { final MessageAndOffset msg = getCurrentMessageItr().next(); final long msgOffset = msg.offset(); final Message message = msg.message(); final ByteBuffer buffer = message.payload(); value.set(buffer.array(), buffer.arrayOffset(), message.payloadSize()); key.set(msgOffset); pos = msgOffset; return true; } return false; }
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 www .j a va2s .c o m*/ } if (log.isDebugEnabled()) log.debug(")getImages=" + map); return map; }
From source file:com.offbynull.portmapper.natpmp.NatPmpReceiver.java
/** * Start listening for NAT-PMP events. This method blocks until {@link #stop() } is called. * @param listener listener to notify of events * @throws IOException if socket error occurs * @throws NullPointerException if any argument is {@code null} *//*from ww w . jav a2s. c o m*/ public void start(NatPmpEventListener listener) throws IOException { Validate.notNull(listener); MulticastSocket socket = null; try { final InetAddress group = InetAddress.getByName("224.0.0.1"); // NOPMD final int port = 5350; final InetSocketAddress groupAddress = new InetSocketAddress(group, port); socket = new MulticastSocket(port); if (!currentSocket.compareAndSet(null, socket)) { IOUtils.closeQuietly(socket); return; } socket.setReuseAddress(true); Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface networkInterface = interfaces.nextElement(); Enumeration<InetAddress> addrs = networkInterface.getInetAddresses(); while (addrs.hasMoreElements()) { // make sure atleast 1 ipv4 addr bound to interface InetAddress addr = addrs.nextElement(); try { if (addr instanceof Inet4Address) { socket.joinGroup(groupAddress, networkInterface); } } catch (IOException ioe) { // NOPMD // occurs with certain interfaces // do nothing } } } ByteBuffer buffer = ByteBuffer.allocate(12); DatagramPacket data = new DatagramPacket(buffer.array(), buffer.capacity()); while (true) { buffer.clear(); socket.receive(data); buffer.position(data.getLength()); buffer.flip(); if (!data.getAddress().equals(gatewayAddress)) { // data isn't from our gateway, ignore continue; } if (buffer.remaining() != 12) { // data isn't the expected size, ignore continue; } int version = buffer.get(0); if (version != 0) { // data doesn't have the correct version, ignore continue; } int opcode = buffer.get(1) & 0xFF; if (opcode != 128) { // data doesn't have the correct op, ignore continue; } int resultCode = buffer.getShort(2) & 0xFFFF; switch (resultCode) { case 0: break; default: continue; // data doesn't have a successful result, ignore } listener.publicAddressUpdated(new ExternalAddressNatPmpResponse(buffer)); } } catch (IOException ioe) { if (currentSocket.get() == null) { return; // ioexception caused by interruption/stop, so just return without propogating error up } throw ioe; } finally { IOUtils.closeQuietly(socket); currentSocket.set(null); } }
From source file:cn.ac.ncic.mastiff.io.coding.DeltaBinaryBitPackingZigZarIntReader.java
public void ensureDecompress() throws IOException { org.apache.hadoop.io.compress.Decompressor decompressor = this.compressAlgo.getDecompressor(); InputStream is = this.compressAlgo.createDecompressionStream(inBuf, decompressor, 0); ByteBuffer buf = ByteBuffer.allocate(decompressedSize); IOUtils.readFully(is, buf.array(), 0, buf.capacity()); is.close();//from w w w . j av a 2s .co m this.compressAlgo.returnDecompressor(decompressor); inBuf.reset(buf.array(), offset, buf.capacity()); }