List of usage examples for io.netty.buffer Unpooled copiedBuffer
public static ByteBuf copiedBuffer(byte[] array, int offset, int length)
From source file:com.athena.dolly.websocket.client.test.WebSocketClient.java
License:Apache License
public void sendFile(File file) throws Exception { // Send 10 messages and wait for responses System.out.println("WebSocket Client sending file ->" + file.getAbsolutePath()); //for (int i = 0; i < 10; i++) { // ch.writeAndFlush(new TextWebSocketFrame("Message #" + i)); //}//w ww . java 2s .c om ch.writeAndFlush(new TextWebSocketFrame(file.getAbsolutePath())); // Binary File Data Send byte[] buf = new byte[65536]; int len = 0; BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); while ((len = bis.read(buf)) != -1) { ch.write(new BinaryWebSocketFrame(Unpooled.copiedBuffer(buf, 0, len))); } ch.flush(); System.out.println("File send succeed"); bis.close(); // Ping System.out.println("WebSocket Client sending ping"); ch.writeAndFlush(new PingWebSocketFrame(Unpooled.copiedBuffer(new byte[] { 1, 2, 3, 4, 5, 6 }))); }
From source file:com.mesosphere.mesos.rx.java.recordio.RecordIOOperatorTest.java
License:Apache License
@Test public void correctlyAbleToReadEventsFromEventsBinFile() throws Exception { final InputStream inputStream = this.getClass().getResourceAsStream("/events.bin"); final List<ByteBuf> chunks = new ArrayList<>(); final byte[] bytes = new byte[100]; int read;//w w w .j av a 2 s .co m while ((read = inputStream.read(bytes)) != -1) { chunks.add(Unpooled.copiedBuffer(bytes, 0, read)); } final List<Event> events = runTestOnChunks(chunks); final List<Event.Type> eventTypes = CollectionUtils.listMap(events, Event::getType); assertThat(eventTypes).isEqualTo(newArrayList(Event.Type.SUBSCRIBED, Event.Type.HEARTBEAT, Event.Type.OFFERS, Event.Type.OFFERS, Event.Type.OFFERS, Event.Type.HEARTBEAT, Event.Type.OFFERS, Event.Type.OFFERS, Event.Type.OFFERS, Event.Type.HEARTBEAT, Event.Type.OFFERS, Event.Type.OFFERS, Event.Type.HEARTBEAT, Event.Type.OFFERS, Event.Type.OFFERS, Event.Type.HEARTBEAT, Event.Type.OFFERS, Event.Type.OFFERS, Event.Type.OFFERS, Event.Type.HEARTBEAT, Event.Type.OFFERS, Event.Type.OFFERS, Event.Type.HEARTBEAT, Event.Type.OFFERS, Event.Type.OFFERS, Event.Type.OFFERS, Event.Type.HEARTBEAT, Event.Type.OFFERS, Event.Type.OFFERS, Event.Type.OFFERS, Event.Type.HEARTBEAT, Event.Type.OFFERS, Event.Type.OFFERS, Event.Type.HEARTBEAT, Event.Type.OFFERS, Event.Type.OFFERS, Event.Type.OFFERS, Event.Type.HEARTBEAT, Event.Type.OFFERS, Event.Type.HEARTBEAT, Event.Type.HEARTBEAT, Event.Type.HEARTBEAT)); }
From source file:com.titilink.camel.rest.server.RestletServerHelper.java
License:LGPL
private ByteBuf transferBuffer(Representation represent) { if (null == represent) { return null; }/* w w w .j ava 2 s. c o m*/ InputStream in = null; try { in = represent.getStream(); byte[] content = new byte[0]; byte[] readbytes = new byte[BUFFER_SIZE]; int length = -1; while ((length = in.read(readbytes, 0, BUFFER_SIZE)) != -1) { int preLen = content.length; content = Arrays.copyOf(content, preLen + length); System.arraycopy(readbytes, 0, content, preLen, length); } ByteBuf buf = Unpooled.copiedBuffer(content, 0, content.length); return buf; } catch (Exception e) { LOGGER.error("Failed to read buffer from result entity", e); return null; } finally { if (null != in) { try { in.close(); } catch (Exception e) { LOGGER.error("Failed to close io stream", e); } } } }
From source file:divconq.bus.net.StreamMessage.java
License:Open Source License
public StreamMessage(String op, ByteBuffer buf) { this(op); this.data = Unpooled.copiedBuffer(buf.array(), 0, buf.position()); }