@Test public void substringWithInvalidBounds() throws Exception { ByteString byteString = factory.encodeUtf8("Hello, World!"); try { byteString.substring(-1); fail(); } catch (IllegalArgumentException expected) { } try { byteString.substring(0, 14); fail(); } catch (IllegalArgumentException expected) { } try { byteString.substring(8, 7); fail(); } catch (IllegalArgumentException expected) { } }
@Test public void substring() throws Exception { ByteString byteString = factory.encodeUtf8("Hello, World!"); assertEquals(byteString.substring(0), byteString); assertEquals(byteString.substring(0, 5), ByteString.encodeUtf8("Hello")); assertEquals(byteString.substring(7), ByteString.encodeUtf8("World!")); assertEquals(byteString.substring(6, 6), ByteString.encodeUtf8("")); }
@Override public void onMessage(WebSocket webSocket, ByteString bytes) { try { byte streamID = bytes.getByte(0); ByteString byteString = bytes.substring(1); if (byteString.size() > 0) { switch (streamID) { case 1: if (out != null) { out.write(byteString.toByteArray()); } break; case 2: if (err != null) { err.write(byteString.toByteArray()); } break; case 3: if (errChannel != null) { errChannel.write(byteString.toByteArray()); } break; default: throw new IOException("Unknown stream ID " + streamID); } } } catch (IOException e) { throw KubernetesClientException.launderThrowable(e); } }
/** * Returns a byte string that is a substring of this byte string, beginning at the specified * index until the end of this string. Returns this byte string if {@code beginIndex} is 0. */ public ByteString substring(int beginIndex) { return substring(beginIndex, data.length); }
@Override public ByteString substring(int beginIndex) { return toByteString().substring(beginIndex); }
@Override public ByteString substring(int beginIndex, int endIndex) { return toByteString().substring(beginIndex, endIndex); }
@Override public void onMessage(WebSocket socket, ByteString message) { /** * https://godoc.org/k8s.io/kubernetes/pkg/util/wsstream The Websocket * subprotocol "channel.k8s.io" prepends each binary message with a byte * indicating the channel number (zero indexed) the message was sent on. * Messages in both directions should prefix their messages with this channel * byte. When used for remote execution, the channel numbers are by convention * defined to match the POSIX file-descriptors assigned to STDIN, STDOUT, and * STDERR (0, 1, and 2). No other conversion is performed on the raw subprotocol * - writes are sent as they are received by the server. */ int channel = message.getByte(0); String msg = message.substring(1).utf8(); deliver(channel, msg); }
@Override public void onMessage(WebSocket socket, ByteString message) { /** * https://godoc.org/k8s.io/kubernetes/pkg/util/wsstream The Websocket * subprotocol "channel.k8s.io" prepends each binary message with a byte * indicating the channel number (zero indexed) the message was sent on. * Messages in both directions should prefix their messages with this channel * byte. When used for remote execution, the channel numbers are by convention * defined to match the POSIX file-descriptors assigned to STDIN, STDOUT, and * STDERR (0, 1, and 2). No other conversion is performed on the raw subprotocol * - writes are sent as they are received by the server. */ int channel = message.getByte(0); String msg = message.substring(1).utf8(); deliver(channel, msg); }
@Override public void onMessage(WebSocket webSocket, ByteString bytes) { try { byte streamID = bytes.getByte(0); ByteString byteString = bytes.substring(1); if (byteString.size() > 0) { switch (streamID) { case 1: if (out != null) { out.write(byteString.toByteArray()); } break; case 2: if (err != null) { err.write(byteString.toByteArray()); } break; case 3: if (err != null) { err.write(byteString.toByteArray()); } break; default: throw new IOException("Unknown stream ID " + streamID); } } } catch (IOException e) { throw KubernetesClientException.launderThrowable(e); } }