Java Code Examples for io.vertx.core.buffer.Buffer#getByte()
The following examples show how to use
io.vertx.core.buffer.Buffer#getByte() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Packet.java From besu with Apache License 2.0 | 6 votes |
public static Packet decode(final Buffer message) { checkGuard( message.length() >= PACKET_DATA_INDEX, PeerDiscoveryPacketDecodingException::new, "Packet too short: expected at least %s bytes, got %s", PACKET_DATA_INDEX, message.length()); final byte type = message.getByte(PACKET_TYPE_INDEX); final PacketType packetType = PacketType.forByte(type) .orElseThrow( () -> new PeerDiscoveryPacketDecodingException("Unrecognized packet type: " + type)); final PacketType.Deserializer<?> deserializer = packetType.getDeserializer(); final PacketData packetData; try { packetData = deserializer.deserialize(RLP.input(message, PACKET_DATA_INDEX)); } catch (final RLPException e) { throw new PeerDiscoveryPacketDecodingException("Malformed packet of type: " + packetType, e); } return new Packet(packetType, packetData, Bytes.wrapBuffer(message)); }
Example 2
Source File: ZModem.java From vertx-redis-client with Apache License 2.0 | 6 votes |
/** * Convert a buffer into a redis slot hash. * @param buffer buffer * @return hash */ public static int generate(Buffer buffer) { int ch; int i = 0; int start = -1; int result = 0; int resultHash = 0; int length = buffer.length(); while (i < length) { ch = buffer.getByte(i++); if (start == -1) { if (ch == 0x7B) { start = i; } } else if (ch != 0x7D) { resultHash = lookup[(ch ^ (resultHash >> 8)) & 0xFF] ^ (resultHash << 8); } else if (i - 1 != start) { return resultHash & 0x3FFF; } result = lookup[(ch ^ (result >> 8)) & 0xFF] ^ (result << 8); } return result & 0x3FFF; }
Example 3
Source File: ServiceExceptionMessageCodec.java From vertx-service-proxy with Apache License 2.0 | 6 votes |
@Override public ServiceException decodeFromWire(int pos, Buffer buffer) { int failureCode = buffer.getInt(pos); pos += 4; boolean isNull = buffer.getByte(pos) == (byte)0; pos++; String message; if (!isNull) { int strLength = buffer.getInt(pos); pos += 4; byte[] bytes = buffer.getBytes(pos, pos + strLength); message = new String(bytes, CharsetUtil.UTF_8); pos += strLength; } else { message = null; } JsonObject debugInfo = new JsonObject(); debugInfo.readFromBuffer(pos, buffer); return new ServiceException(failureCode, message, debugInfo); }
Example 4
Source File: DockerModuleHandle.java From okapi with Apache License 2.0 | 5 votes |
private void logHandler(Buffer b) { if (logSkip == 0 && b.getByte(0) < 3) { logSkip = 8; } if (b.length() > logSkip) { logBuffer.append(b.getString(logSkip, b.length())); logSkip = 0; } else { logSkip = logSkip - b.length(); } if (logBuffer.length() > 0 && logBuffer.charAt(logBuffer.length() - 1) == '\n') { logger.info("{} {}", () -> id, () -> logBuffer.substring(0, logBuffer.length() - 1)); logBuffer.setLength(0); } }
Example 5
Source File: UserHolder.java From vertx-web with Apache License 2.0 | 5 votes |
@Override public int readFromBuffer(int pos, Buffer buffer) { byte b = buffer.getByte(pos++); if (b == (byte)1) { int len = buffer.getInt(pos); pos += 4; byte[] bytes = buffer.getBytes(pos, pos + len); pos += len; String className = new String(bytes, StandardCharsets.UTF_8); try { Class<?> clazz = Utils.getClassLoader().loadClass(className); if (!ClusterSerializable.class.isAssignableFrom(clazz)) { throw new ClassCastException(className + " is not ClusterSerializable"); } ClusterSerializable obj = (ClusterSerializable) clazz.getDeclaredConstructor().newInstance(); pos = obj.readFromBuffer(pos, buffer); synchronized (this) { user = (User) obj; context = null; } } catch (Exception e) { throw new VertxException(e); } } else { synchronized (this) { user = null; context = null; } } return pos; }
Example 6
Source File: PersonCodec.java From vertx-camel-bridge with Apache License 2.0 | 4 votes |
@Override public Person decodeFromWire(int pos, Buffer buffer) { int i = (int) buffer.getByte(pos); Buffer extracted = buffer.getBuffer(pos + 1, pos + i); return Json.decodeValue(extracted.toString(), Person.class); }
Example 7
Source File: FrameParser.java From vertx-stomp with Apache License 2.0 | 4 votes |
private boolean isEmpty(Buffer buffer) { return buffer.toString().length() == 0 || buffer.toString().equals(CARRIAGE_RETURN) || buffer.length() == 1 && buffer.getByte(0) == 0; }
Example 8
Source File: SharedDataSessionImpl.java From vertx-web with Apache License 2.0 | 4 votes |
private int readDataFromBuffer(int pos, Buffer buffer) { try { int entries = buffer.getInt(pos); pos += 4; if (entries > 0) { final Map<String, Object> data = new ConcurrentHashMap<>(entries); for (int i = 0; i < entries; i++) { int keylen = buffer.getInt(pos); pos += 4; byte[] keyBytes = buffer.getBytes(pos, pos + keylen); pos += keylen; String key = new String(keyBytes, UTF8); byte type = buffer.getByte(pos++); Object val; switch (type) { case TYPE_LONG: val = buffer.getLong(pos); pos += 8; break; case TYPE_INT: val = buffer.getInt(pos); pos += 4; break; case TYPE_SHORT: val = buffer.getShort(pos); pos += 2; break; case TYPE_BYTE: val = buffer.getByte(pos); pos++; break; case TYPE_FLOAT: val = buffer.getFloat(pos); pos += 4; break; case TYPE_DOUBLE: val = buffer.getDouble(pos); pos += 8; break; case TYPE_CHAR: short s = buffer.getShort(pos); pos += 2; val = (char) s; break; case TYPE_BOOLEAN: byte b = buffer.getByte(pos); pos++; val = b == 1; break; case TYPE_STRING: int len = buffer.getInt(pos); pos += 4; byte[] bytes = buffer.getBytes(pos, pos + len); val = new String(bytes, UTF8); pos += len; break; case TYPE_BUFFER: len = buffer.getInt(pos); pos += 4; bytes = buffer.getBytes(pos, pos + len); val = Buffer.buffer(bytes); pos += len; break; case TYPE_BYTES: len = buffer.getInt(pos); pos += 4; val = buffer.getBytes(pos, pos + len); pos += len; break; case TYPE_CLUSTER_SERIALIZABLE: int classNameLen = buffer.getInt(pos); pos += 4; byte[] classNameBytes = buffer.getBytes(pos, pos + classNameLen); pos += classNameLen; String className = new String(classNameBytes, UTF8); Class<?> clazz = Utils.getClassLoader().loadClass(className); if (!ClusterSerializable.class.isAssignableFrom(clazz)) { throw new ClassCastException(new String(classNameBytes) + " is not assignable from ClusterSerializable"); } ClusterSerializable obj = (ClusterSerializable) clazz.getDeclaredConstructor().newInstance(); pos = obj.readFromBuffer(pos, buffer); val = obj; break; default: throw new IllegalStateException("Invalid serialized type: " + type); } data.put(key, val); } setData(data); } return pos; } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException e) { throw new VertxException(e); } }