/** * Convert a UTF-8 byte array into a string. */ public static String toStringUTF8(byte[] bytes) { if (bytes == null) { return null; } return toStringUTF8(bytes, 0, bytes.length); }
/** * Get the unmasked payload as a text. * * @return * A string constructed by interrupting the payload * as a UTF-8 bytes. */ public String getPayloadText() { if (mPayload == null) { return null; } return Misc.toStringUTF8(mPayload); }
/** * Parse the third and subsequent bytes of the payload as a close reason. * * <p> * If any payload is not set or the length of the payload is less than 3, * this method returns {@code null}. * </p> * * <p> * The value returned from this method is meaningless if this frame * is not a close frame. * </p> * * @return * The close reason. */ public String getCloseReason() { if (mPayload == null || mPayload.length < 3) { return null; } return Misc.toStringUTF8(mPayload, 2, mPayload.length - 2); }
/** * Call {@link WebSocketListener#onTextMessage(WebSocket, String) * onTextMessage} method of the listeners. */ private void callOnTextMessage(byte[] data) { if (mWebSocket.isDirectTextMessage()) { mWebSocket.getListenerManager().callOnTextMessage(data); return; } try { // Interpret the byte array as a string. // OutOfMemoryError may happen when the size of data is too big. String message = Misc.toStringUTF8(data); // Call onTextMessage() method of the listeners. callOnTextMessage(message); } catch (Throwable t) { // Failed to convert payload data into a string. WebSocketException wse = new WebSocketException( WebSocketError.TEXT_MESSAGE_CONSTRUCTION_ERROR, "Failed to convert payload data into a string: " + t.getMessage(), t); // Notify the listeners that text message construction failed. callOnError(wse); callOnTextMessageError(wse, data); } }
/** * Convert a UTF-8 byte array into a string. */ public static String toStringUTF8(byte[] bytes) { if (bytes == null) { return null; } return toStringUTF8(bytes, 0, bytes.length); }
/** * Get the unmasked payload as a text. * * @return * A string constructed by interrupting the payload * as a UTF-8 bytes. */ public String getPayloadText() { if (mPayload == null) { return null; } return Misc.toStringUTF8(mPayload); }
/** * Parse the third and subsequent bytes of the payload as a close reason. * * <p> * If any payload is not set or the length of the payload is less than 3, * this method returns {@code null}. * </p> * * <p> * The value returned from this method is meaningless if this frame * is not a close frame. * </p> * * @return * The close reason. */ public String getCloseReason() { if (mPayload == null || mPayload.length < 3) { return null; } return Misc.toStringUTF8(mPayload, 2, mPayload.length - 2); }
/** * Call {@link WebSocketListener#onTextMessage(WebSocket, String) * onTextMessage} method of the listeners. */ private void callOnTextMessage(byte[] data) { if (mWebSocket.isDirectTextMessage()) { mWebSocket.getListenerManager().callOnTextMessage(data); return; } try { // Interpret the byte array as a string. // OutOfMemoryError may happen when the size of data is too big. String message = Misc.toStringUTF8(data); // Call onTextMessage() method of the listeners. callOnTextMessage(message); } catch (Throwable t) { // Failed to convert payload data into a string. WebSocketException wse = new WebSocketException( WebSocketError.TEXT_MESSAGE_CONSTRUCTION_ERROR, "Failed to convert payload data into a string: " + t.getMessage(), t); // Notify the listeners that text message construction failed. callOnError(wse); callOnTextMessageError(wse, data); } }