Java Code Examples for io.vertx.core.buffer.Buffer#getInt()

The following examples show how to use io.vertx.core.buffer.Buffer#getInt() . 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: PojoEventBusCodec.java    From raml-module-builder with Apache License 2.0 6 votes vote down vote up
@Override
public Object decodeFromWire(int position, Buffer buffer) {

  int _pos = position;

  // Length
  int clazzNameLength = buffer.getInt(_pos);
  String clazz = buffer.getString(_pos+=4, _pos+=clazzNameLength);

  // Jump 4 because getInt() == 4 bytes
  int dataLength = buffer.getInt(_pos+=4);

  String data = buffer.getString(_pos+=4, _pos+=dataLength);

  Object obj = null;
  try {
    obj = MAPPER.readValue(data, Class.forName(clazz));
  } catch (Exception e) {
    log.error(e.getMessage(), e);
  }

  // We can finally create custom message object
  return obj;
}
 
Example 2
Source File: Pac4jUser.java    From vertx-pac4j with Apache License 2.0 6 votes vote down vote up
@Override
public int readFromBuffer(int pos, Buffer buffer) {
    int posLocal = super.readFromBuffer(pos, buffer);
    final int jsonByteCount = buffer.getInt(posLocal);
    posLocal += 4;
    final byte[] jsonBytes = buffer.getBytes(posLocal, posLocal + jsonByteCount);
    posLocal += jsonByteCount;

    final String json = new String(jsonBytes, StandardCharsets.UTF_8);
    final JsonObject profiles = new JsonObject(json);

    final Map<String, CommonProfile> decodedUserProfiles = profiles.stream()
            .filter(e -> e.getValue() instanceof JsonObject)
            .map(e -> new MappedPair<>(e.getKey(),
                    (CommonProfile) DefaultJsonConverter.getInstance().decodeObject(e.getValue())))
            .collect(toMap(e -> e.key, e -> e.value));

    setUserProfiles(decodedUserProfiles);
    return posLocal;
}
 
Example 3
Source File: SharedDataSessionImpl.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Override
public int readFromBuffer(int pos, Buffer buffer) {
  int len = buffer.getInt(pos);
  pos += 4;
  byte[] bytes = buffer.getBytes(pos, pos + len);
  pos += len;
  setId(new String(bytes, UTF8));
  setTimeout(buffer.getLong(pos));
  pos += 8;
  setLastAccessed(buffer.getLong(pos));
  pos += 8;
  setVersion(buffer.getInt(pos));
  pos += 4;
  int start = pos;
  pos = readDataFromBuffer(pos, buffer);
  int end = pos;
  return pos;
}
 
Example 4
Source File: ServiceExceptionMessageCodec.java    From vertx-service-proxy with Apache License 2.0 6 votes vote down vote up
@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 5
Source File: VertxBufferImpl.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@Override
public int readFromBuffer(int pos, Buffer buffer) {
  int len = buffer.getInt(pos);
  Buffer b = buffer.getBuffer(pos + 4, pos + 4 + len);
  this.buffer = b.getByteBuf();
  return pos + 4 + len;
}
 
Example 6
Source File: SerializationSupport.java    From vertx-vaadin with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> int readFromBuffer(int pos, Buffer buffer, Consumer<T> objectConsumer) {
    int size = buffer.getInt(pos);
    pos += 4;
    int end = pos + size;
    try (ObjectInputStream is = new ObjectInputStream(new ByteArrayInputStream(buffer.getBytes(pos, end)))) {
        Object object = is.readObject();
        objectConsumer.accept((T) object);
    } catch (Exception ex) {
        logger.error("Error deserializing object", ex);
    }
    return end;
}
 
Example 7
Source File: SerializationSupport.java    From vertx-vaadin with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> int readFromBuffer(int pos, Buffer buffer, Consumer<T> objectConsumer) {
    int size = buffer.getInt(pos);
    pos += 4;
    int end = pos + size;
    try (ObjectInputStream is = new ObjectInputStream(new ByteArrayInputStream(buffer.getBytes(pos, end)))) {
        Object object = is.readObject();
        objectConsumer.accept((T) object);
    } catch (Exception ex) {
        logger.error("Error deserializing object", ex);
    }
    return end;
}
 
Example 8
Source File: VertxBufferImpl.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Override
public int readFromBuffer(int pos, Buffer buffer) {
    int len = buffer.getInt(pos);
    Buffer b = buffer.getBuffer(pos + 4, pos + 4 + len);
    this.buffer = b.getByteBuf();
    return pos + 4 + len;
}
 
Example 9
Source File: JacksonCodec.java    From kyoko with MIT License 5 votes vote down vote up
@Override
public T decodeFromWire(final int pos, final Buffer buffer) {
    try {
        final int length = buffer.getInt(pos);
        final String rawJson = buffer.getString(pos + 4, pos + 4 + length);
        return JsonUtil.fromJSON(rawJson, type);
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}
 
Example 10
Source File: Block.java    From sfs with Apache License 2.0 5 votes vote down vote up
public static Optional<Frame<byte[]>> decodeFrame(Buffer buffer, boolean validateChecksum) {
    int length = buffer.length();

    final byte[] frame;
    final byte[] expectedChecksum;
    try {
        int frameSize = buffer.getInt(FRAME_LENGTH_OFFSET);
        Preconditions.checkArgument(frameSize >= 0 && frameSize < length, "Frame size was %s, expected 0 to %s", frameSize, length);
        frame = buffer.getBytes(FRAME_DATA_OFFSET, FRAME_DATA_OFFSET + frameSize);
        expectedChecksum = buffer.getBytes(FRAME_HASH_OFFSET, FRAME_HASH_OFFSET + FRAME_HASH_SIZE);
    } catch (Throwable e) {
        return Optional.absent();
    }

    Frame<byte[]> f = new Frame<byte[]>(expectedChecksum, frame) {

        @Override
        public boolean isChecksumValid() {
            return Arrays.equals(expectedChecksum, checksum(frame));
        }
    };

    if (validateChecksum) {
        if (!f.isChecksumValid()) {
            Preconditions.checkState(
                    false,
                    "Checksum was %s, expected %s",
                    BaseEncoding.base64().encode(checksum(frame)),
                    BaseEncoding.base64().encode(expectedChecksum));
        }
    }

    return Optional.of(f);
}
 
Example 11
Source File: UserHolder.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@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 12
Source File: AbstractUser.java    From vertx-auth with Apache License 2.0 5 votes vote down vote up
private int readStringSet(Buffer buffer, Set<String> set, int pos) {
  int num = buffer.getInt(pos);
  pos += 4;
  for (int i = 0; i < num; i++) {
    int len = buffer.getInt(pos);
    pos += 4;
    byte[] bytes = buffer.getBytes(pos, pos + len);
    pos += len;
    set.add(new String(bytes, StandardCharsets.UTF_8));
  }
  return pos;
}
 
Example 13
Source File: CustomMessageCodec.java    From skywalking with Apache License 2.0 4 votes vote down vote up
@Override
public CustomMessage decodeFromWire(int position, Buffer buffer) {
    int length = buffer.getInt(position);
    JsonObject jsonMessage = new JsonObject(buffer.getString(position += 4, position + length));
    return new CustomMessage(jsonMessage.getString("message"));
}
 
Example 14
Source File: SharedDataSessionImpl.java    From vertx-web with Apache License 2.0 4 votes vote down vote up
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);
  }
}