int encodedLength(ByteString bytes) { long len = 0; for (int i = 0; i < bytes.size(); i++) { int b = bytes.getByte(i) & 0xFF; len += CODE_LENGTHS[b]; } return (int) ((len + 7) >> 3); }
/** * An HTTP/2 response cannot contain uppercase header characters and must be treated as * malformed. */ static ByteString checkLowercase(ByteString name) throws IOException { for (int i = 0, length = name.size(); i < length; i++) { byte c = name.getByte(i); if (c >= 'A' && c <= 'Z') { throw new IOException("PROTOCOL_ERROR response malformed: mixed case name: " + name.utf8()); } } return name; } }
int encodedLength(ByteString bytes) { long len = 0; for (int i = 0; i < bytes.size(); i++) { int b = bytes.getByte(i) & 0xFF; len += CODE_LENGTHS[b]; } return (int) ((len + 7) >> 3); }
/** * An HTTP/2 response cannot contain uppercase header characters and must be treated as * malformed. */ static ByteString checkLowercase(ByteString name) throws IOException { for (int i = 0, length = name.size(); i < length; i++) { byte c = name.getByte(i); if (c >= 'A' && c <= 'Z') { throw new IOException("PROTOCOL_ERROR response malformed: mixed case name: " + name.utf8()); } } return name; } }
void encode(ByteString data, BufferedSink sink) throws IOException { long current = 0; int n = 0; for (int i = 0; i < data.size(); i++) { int b = data.getByte(i) & 0xFF; int code = CODES[b]; int nbits = CODE_LENGTHS[b]; current <<= nbits; current |= code; n += nbits; while (n >= 8) { n -= 8; sink.writeByte(((int) (current >> n))); } } if (n > 0) { current <<= (8 - n); current |= (0xFF >>> n); sink.writeByte((int) current); } }
void encode(ByteString data, BufferedSink sink) throws IOException { long current = 0; int n = 0; for (int i = 0; i < data.size(); i++) { int b = data.getByte(i) & 0xFF; int code = CODES[b]; int nbits = CODE_LENGTHS[b]; current <<= nbits; current |= code; n += nbits; while (n >= 8) { n -= 8; sink.writeByte(((int) (current >> n))); } } if (n > 0) { current <<= (8 - n); current |= (0xFF >>> n); sink.writeByte((int) current); } }
@Test public void getByteOutOfBounds() throws Exception { ByteString byteString = factory.decodeHex("ab12"); try { byteString.getByte(2); fail(); } catch (IndexOutOfBoundsException expected) { } }
@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); } }
int encodedLength(ByteString bytes) { long len = 0; for (int i = 0; i < bytes.size(); i++) { int b = bytes.getByte(i) & 0xFF; len += CODE_LENGTHS[b]; } return (int) ((len + 7) >> 3); }
@Override public int compareTo(ByteString byteString) { int sizeA = size(); int sizeB = byteString.size(); for (int i = 0, size = Math.min(sizeA, sizeB); i < size; i++) { int byteA = getByte(i) & 0xff; int byteB = byteString.getByte(i) & 0xff; if (byteA == byteB) continue; return byteA < byteB ? -1 : 1; } if (sizeA == sizeB) return 0; return sizeA < sizeB ? -1 : 1; }
int encodedLength(ByteString bytes) { long len = 0; for (int i = 0; i < bytes.size(); i++) { int b = bytes.getByte(i) & 0xFF; len += CODE_LENGTHS[b]; } return (int) ((len + 7) >> 3); }
/** * An HTTP/2 response cannot contain uppercase header characters and must be treated as * malformed. */ private static ByteString checkLowercase(ByteString name) throws IOException { for (int i = 0, length = name.size(); i < length; i++) { byte c = name.getByte(i); if (c >= 'A' && c <= 'Z') { throw new IOException("PROTOCOL_ERROR response malformed: mixed case name: " + name.utf8()); } } return name; } }
/** * An HTTP/2 response cannot contain uppercase header characters and must * be treated as malformed. */ private static ByteString checkLowercase(ByteString name) throws IOException { for (int i = 0, length = name.size(); i < length; i++) { byte c = name.getByte(i); if (c >= 'A' && c <= 'Z') { throw new IOException("PROTOCOL_ERROR response malformed: mixed case name: " + name.utf8()); } } return name; } }
/** * An HTTP/2 response cannot contain uppercase header characters and must * be treated as malformed. */ private static ByteString checkLowercase(ByteString name) throws IOException { for (int i = 0, length = name.size(); i < length; i++) { byte c = name.getByte(i); if (c >= 'A' && c <= 'Z') { throw new IOException("PROTOCOL_ERROR response malformed: mixed case name: " + name.utf8()); } } return name; } }
/** * An HTTP/2 response cannot contain uppercase header characters and must be treated as * malformed. */ static ByteString checkLowercase(ByteString name) throws IOException { for (int i = 0, length = name.size(); i < length; i++) { byte c = name.getByte(i); if (c >= 'A' && c <= 'Z') { throw new IOException("PROTOCOL_ERROR response malformed: mixed case name: " + name.utf8()); } } return name; } }
boolean rangeEquals(long offset, ByteString bytes) { int byteCount = bytes.size(); if (size - offset < byteCount) { return false; } for (int i = 0; i < byteCount; i++) { if (getByte(offset + i) != bytes.getByte(i)) { return false; } } return true; }
@Override public long indexOf(ByteString bytes, long fromIndex) throws IOException { if (bytes.size() == 0) throw new IllegalArgumentException("bytes is empty"); while (true) { fromIndex = indexOf(bytes.getByte(0), fromIndex); if (fromIndex == -1) { return -1; } if (rangeEquals(fromIndex, bytes)) { return fromIndex; } fromIndex++; } }
@Override public long indexOf(ByteString bytes, long fromIndex) throws IOException { if (bytes.size() == 0) throw new IllegalArgumentException("bytes is empty"); while (true) { fromIndex = indexOf(bytes.getByte(0), fromIndex); if (fromIndex == -1) { return -1; } if (rangeEquals(fromIndex, bytes)) { return fromIndex; } fromIndex++; } }
@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); }