Package com.esotericsoftware.kryo

Examples of com.esotericsoftware.kryo.Context


    if (object == null) throw new IllegalArgumentException("object cannot be null.");
    SocketAddress address = udpRemoteAddress;
    if (address == null && udp != null) address = udp.connectedAddress;
    if (address == null && isConnected) throw new IllegalStateException("Connection is not connected via UDP.");

    Context context = Kryo.getContext();
    context.put("connection", this);
    context.put("connectionID", id);
    try {
      if (address == null) throw new SocketException("Connection is closed.");

      int length = udp.send(this, object, address);
      if (length == 0) {
View Full Code Here


    int startPosition = readBuffer.position();
    int oldLimit = readBuffer.limit();
    readBuffer.limit(startPosition + length);

    Context context = Kryo.getContext();
    context.put("connection", connection);
    context.setRemoteEntityID(connection.id);
    Object object = kryo.readClassAndObject(readBuffer);

    readBuffer.limit(oldLimit);
    if (readBuffer.position() - startPosition != length)
      throw new SerializationException("Incorrect number of bytes (" + (startPosition + length - readBuffer.position())
View Full Code Here

    synchronized (writeLock) {
      tempWriteBuffer.clear();
      tempWriteBuffer.position(5); // Allow room for the data length.

      // Write data.
      Context context = Kryo.getContext();
      context.put("connection", connection);
      context.setRemoteEntityID(connection.id);
      try {
        kryo.writeClassAndObject(tempWriteBuffer, object);
      } catch (SerializationException ex) {
        throw new SerializationException("Unable to serialize object of type: " + object.getClass().getName(), ex);
      }
View Full Code Here

  }

  public Object readObject (Connection connection) {
    readBuffer.flip();
    try {
      Context context = Kryo.getContext();
      context.put("connection", connection);
      if (connection != null) context.setRemoteEntityID(connection.id);
      Object object = kryo.readClassAndObject(readBuffer);
      if (readBuffer.hasRemaining())
        throw new SerializationException("Incorrect number of bytes (" + readBuffer.remaining()
          + " remaining) used to deserialize object: " + object);
      return object;
View Full Code Here

  public int send (Connection connection, Object object, SocketAddress address) throws IOException {
    DatagramChannel datagramChannel = this.datagramChannel;
    if (datagramChannel == null) throw new SocketException("Connection is closed.");
    synchronized (writeLock) {
      try {
        Context context = Kryo.getContext();
        context.put("connection", connection);
        context.setRemoteEntityID(connection.id);
        kryo.writeClassAndObject(writeBuffer, object);
        writeBuffer.flip();
        int length = writeBuffer.limit();
        datagramChannel.send(writeBuffer, address);
View Full Code Here

    super(serializer, bufferSize);
    keySpec = new SecretKeySpec(key, "Blowfish");
  }

  public void compress (ByteBuffer inputBuffer, Object object, ByteBuffer outputBuffer) {
    Context context = Kryo.getContext();
    Cipher encrypt = (Cipher)context.get(this, "encryptCipher");
    try {
      if (encrypt == null) {
        encrypt = Cipher.getInstance("Blowfish");
        encrypt.init(Cipher.ENCRYPT_MODE, keySpec);
        context.put(this, "encryptCipher", encrypt);
      }
      encrypt.doFinal(inputBuffer, outputBuffer);
    } catch (GeneralSecurityException ex) {
      throw new SerializationException(ex);
    }
View Full Code Here

      throw new SerializationException(ex);
    }
  }

  public void decompress (ByteBuffer inputBuffer, Class type, ByteBuffer outputBuffer) {
    Context context = Kryo.getContext();
    Cipher decrypt = (Cipher)context.get(this, "decryptCipher");
    try {
      if (decrypt == null) {
        decrypt = Cipher.getInstance("Blowfish");
        decrypt.init(Cipher.DECRYPT_MODE, keySpec);
        context.put(this, "decryptCipher", decrypt);
      }
      decrypt.doFinal(inputBuffer, outputBuffer);
    } catch (GeneralSecurityException ex) {
      throw new SerializationException(ex);
    }
View Full Code Here

  }

  public void compress (ByteBuffer newData, Object object, ByteBuffer outputBuffer) {
    int start = newData.position();

    Context context = Kryo.getContext();
    int remoteID = context.getRemoteEntityID();
    ByteBuffer remoteData = contextToRemoteData.get(remoteID);

    Delta delta = (Delta)context.get(this, "delta");
    if (delta == null) {
      delta = new Delta(bufferSize, chunkSize);
      context.put(this, "delta", delta);
    }
    delta.compress(remoteData, newData, outputBuffer);

    if (remoteData == null) {
      remoteData = ByteBuffer.allocate(bufferSize);
View Full Code Here

    remoteData.put(newData);
    remoteData.flip();
  }

  public void decompress (ByteBuffer deltaData, Class type, ByteBuffer outputBuffer) {
    Context context = Kryo.getContext();
    int remoteID = context.getRemoteEntityID();
    ByteBuffer localData = contextToLocalData.get(remoteID);

    Delta delta = (Delta)context.get(this, "delta");
    if (delta == null) {
      delta = new Delta(bufferSize, chunkSize);
      context.put(this, "delta", delta);
    }
    delta.decompress(localData, deltaData, outputBuffer);

    if (localData == null) {
      localData = ByteBuffer.allocate(bufferSize);
View Full Code Here

    this.setFieldsAsAccessible = setFieldsAsAccessible;
    rebuildCachedFields();
  }

  public void writeObjectData (ByteBuffer buffer, Object object) {
    Context context = Kryo.getContext();
    if (context.getTemp(this, "schemaWritten") == null) {
      context.putTemp(this, "schemaWritten", Boolean.TRUE);
      if (TRACE) trace("kryo", "Writing " + fields.length + " field names.");
      IntSerializer.put(buffer, fields.length, true);
      for (int i = 0, n = fields.length; i < n; i++)
        StringSerializer.put(buffer, fields[i].field.getName());
    }

    for (int i = 0, n = fields.length; i < n; i++) {
      CachedField cachedField = fields[i];
      try {
        if (TRACE) trace("kryo", "Writing field: " + cachedField + " (" + object.getClass().getName() + ")");

        Object value = cachedField.get(object);
        if (value == null) {
          kryo.writeClass(buffer, null);
          continue;
        }

        int start = buffer.position();
        try {
          buffer.position(start + 1);
        } catch (IllegalArgumentException ex) {
          new BufferOverflowException();
        }

        Serializer serializer = cachedField.serializer;
        if (cachedField.fieldClass == null) {
          RegisteredClass registeredClass = kryo.writeClass(buffer, value.getClass());
          if (serializer == null) serializer = registeredClass.getSerializer();
          serializer.writeObjectData(buffer, value);
        } else {
          if (serializer == null)
            cachedField.serializer = serializer = kryo.getRegisteredClass(cachedField.fieldClass).getSerializer();
          if (!cachedField.canBeNull)
            serializer.writeObjectData(buffer, value);
          else
            serializer.writeObject(buffer, value);
        }

        int dataLength = buffer.position() - start - 1;
        if (dataLength <= 127) {
          // Ideally it fits in one byte.
          buffer.put(start, (byte)dataLength);
        } else {
          // Shift the data over to make room for the length.
          byte[] temp = context.getByteArray(dataLength);
          buffer.position(start + 1);
          buffer.get(temp);
          buffer.position(start);
          IntSerializer.put(buffer, dataLength, true);
          buffer.put(temp);
View Full Code Here

TOP

Related Classes of com.esotericsoftware.kryo.Context

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.