@Override public void close() throws IOException { socket.sendBinary(new byte[]{}, true); } }
/** * Sends a fragment of a complete message. * * @param bytes the binary fragment to send. * @param last boolean indicating if this message fragment is the last. * @return {@link Future} which could be used to control/check the sending completion state. */ public Future<Frame> sendBinary(byte[] bytes, boolean last) { return sendBinary(bytes, 0, bytes.length, last); }
@Override public void close() throws IOException { socket.sendBinary(new byte[]{}, true); } }
/** * Sends a fragment of a complete message. * * @param bytes the binary fragment to send. * @param last boolean indicating if this message fragment is the last. * @return {@link Future} which could be used to control/check the sending completion state. */ public Future<Frame> sendBinary(byte[] bytes, boolean last) { return sendBinary(bytes, 0, bytes.length, last); }
@Override public void write(byte b[], int off, int len) throws IOException { if (b == null) { throw new NullPointerException(); } else if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) { throw new IndexOutOfBoundsException(); } else if (len == 0) { return; } final Future<?> future = socket.sendBinary(b, off, len, false); try { future.get(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } catch (ExecutionException e) { if (e.getCause() instanceof IOException) { throw (IOException) e.getCause(); } else { throw new IOException(e.getCause()); } } }
@Override public void write(byte b[], int off, int len) throws IOException { if (b == null) { throw new NullPointerException(); } else if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) { throw new IndexOutOfBoundsException(); } else if (len == 0) { return; } final Future<?> future = socket.sendBinary(b, off, len, false); try { future.get(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } catch (ExecutionException e) { if (e.getCause() instanceof IOException) { throw (IOException) e.getCause(); } else { throw new IOException(e.getCause()); } } }
@SuppressWarnings("unchecked") void sendSyncObject(Object o, SendHandler handler) { if (o instanceof String) { webSocket.sendText((String) o, handler); } else { Object toSend = null; try { toSend = endpointWrapper.doEncode(session, o); } catch (final Exception e) { handler.onResult(new SendResult(e)); } if (toSend instanceof String) { webSocket.sendText((String) toSend, handler); } else if (toSend instanceof ByteBuffer) { webSocket.sendBinary(Utils.getRemainingArray((ByteBuffer) toSend), handler); } else if (toSend instanceof StringWriter) { StringWriter writer = (StringWriter) toSend; StringBuffer sb = writer.getBuffer(); webSocket.sendText(sb.toString(), handler); } else if (toSend instanceof ByteArrayOutputStream) { ByteArrayOutputStream baos = (ByteArrayOutputStream) toSend; webSocket.sendBinary(baos.toByteArray(), handler); } } }
@SuppressWarnings("unchecked") void sendSyncObject(Object o, SendHandler handler) { if (o instanceof String) { webSocket.sendText((String) o, handler); } else { Object toSend = null; try { toSend = endpointWrapper.doEncode(session, o); } catch (final Exception e) { handler.onResult(new SendResult(e)); } if (toSend instanceof String) { webSocket.sendText((String) toSend, handler); } else if (toSend instanceof ByteBuffer) { webSocket.sendBinary(Utils.getRemainingArray((ByteBuffer) toSend), handler); } else if (toSend instanceof StringWriter) { StringWriter writer = (StringWriter) toSend; StringBuffer sb = writer.getBuffer(); webSocket.sendText(sb.toString(), handler); } else if (toSend instanceof ByteArrayOutputStream) { ByteArrayOutputStream baos = (ByteArrayOutputStream) toSend; webSocket.sendBinary(baos.toByteArray(), handler); } } }
return webSocket.sendText((String) toSend); } else if (toSend instanceof ByteBuffer) { return webSocket.sendBinary(Utils.getRemainingArray((ByteBuffer) toSend)); } else if (toSend instanceof StringWriter) { StringWriter writer = (StringWriter) toSend; } else if (toSend instanceof ByteArrayOutputStream) { ByteArrayOutputStream baos = (ByteArrayOutputStream) toSend; return webSocket.sendBinary(baos.toByteArray());
return webSocket.sendText((String) toSend); } else if (toSend instanceof ByteBuffer) { return webSocket.sendBinary(Utils.getRemainingArray((ByteBuffer) toSend)); } else if (toSend instanceof StringWriter) { StringWriter writer = (StringWriter) toSend; } else if (toSend instanceof ByteArrayOutputStream) { ByteArrayOutputStream baos = (ByteArrayOutputStream) toSend; return webSocket.sendBinary(baos.toByteArray());
/** * Sends the message asynchronously. * <p> * IMPORTANT NOTE: There is no need to start new thread here. All writer are by default asynchronous, only * difference between sync and async writer are that sync send should wait for future.get(). * * @param message message to be sent * @param handler message sending callback handler * @param type message type */ private void sendAsync(final Object message, final SendHandler handler, final AsyncMessageType type) { switch (type) { case TEXT: webSocket.sendText((String) message, handler); break; case BINARY: webSocket.sendBinary(Utils.getRemainingArray((ByteBuffer) message), handler); break; case OBJECT: sendSyncObject(message, handler); break; } }
/** * Sends the message asynchronously. * <p> * IMPORTANT NOTE: There is no need to start new thread here. All writer are by default asynchronous, only * difference between sync and async writer are that sync send should wait for future.get(). * * @param message message to be sent * @param handler message sending callback handler * @param type message type */ private void sendAsync(final Object message, final SendHandler handler, final AsyncMessageType type) { switch (type) { case TEXT: webSocket.sendText((String) message, handler); break; case BINARY: webSocket.sendBinary(Utils.getRemainingArray((ByteBuffer) message), handler); break; case OBJECT: sendSyncObject(message, handler); break; } }
@Override public void sendBinary(ByteBuffer partialByte, boolean isLast) throws IOException { checkNotNull(partialByte, "partialByte"); session.getDebugContext().appendLogMessage(LOGGER, Level.FINEST, DebugContext.Type.MESSAGE_OUT, "Sending partial binary message"); final Future<?> future = webSocket.sendBinary(Utils.getRemainingArray(partialByte), isLast); try { processFuture(future); } finally { session.restartIdleTimeoutExecutor(); } }
@Override public void sendBinary(ByteBuffer data) throws IOException { checkNotNull(data, "data"); session.getDebugContext() .appendLogMessage(LOGGER, Level.FINEST, DebugContext.Type.MESSAGE_OUT, "Sending binary message"); final Future<?> future = webSocket.sendBinary(Utils.getRemainingArray(data)); try { processFuture(future); } finally { session.restartIdleTimeoutExecutor(); } }
@Override public void sendBinary(ByteBuffer data) throws IOException { checkNotNull(data, "data"); session.getDebugContext() .appendLogMessage(LOGGER, Level.FINEST, DebugContext.Type.MESSAGE_OUT, "Sending binary message"); final Future<?> future = webSocket.sendBinary(Utils.getRemainingArray(data)); try { processFuture(future); } finally { session.restartIdleTimeoutExecutor(); } }
@Override public void sendBinary(ByteBuffer partialByte, boolean isLast) throws IOException { checkNotNull(partialByte, "partialByte"); session.getDebugContext().appendLogMessage(LOGGER, Level.FINEST, DebugContext.Type.MESSAGE_OUT, "Sending partial binary message"); final Future<?> future = webSocket.sendBinary(Utils.getRemainingArray(partialByte), isLast); try { processFuture(future); } finally { session.restartIdleTimeoutExecutor(); } }
session.getDebugContext().appendLogMessage( LOGGER, Level.FINEST, DebugContext.Type.MESSAGE_OUT, "Sending binary message"); result = webSocket.sendBinary(Utils.getRemainingArray((ByteBuffer) message)); break;
session.getDebugContext().appendLogMessage( LOGGER, Level.FINEST, DebugContext.Type.MESSAGE_OUT, "Sending binary message"); result = webSocket.sendBinary(Utils.getRemainingArray((ByteBuffer) message)); break;