Java Code Examples for com.esotericsoftware.kryo.KryoException

The following examples show how to use com.esotericsoftware.kryo.KryoException. These examples are extracted from open source projects. 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 Project: Bats   Source File: FastPublisher.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void writeChar(char value) throws KryoException
{
  switch (writeBuffer.remaining()) {
    case 0:
      advanceWriteBuffer();
      writeBuffer.put((byte)(value >>> 8));
      writeBuffer.put((byte)value);
      break;

    case 1:
      writeBuffer.put((byte)(value >>> 8));
      advanceWriteBuffer();
      writeBuffer.put((byte)value);
      break;

    default:
      writeBuffer.put((byte)(value >>> 8));
      writeBuffer.put((byte)value);
      break;
  }
}
 
Example 2
Source Project: cuba   Source File: KryoSerialization.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Object read(Kryo kryo, Input input, Class type) {
    try {
        ObjectMap graphContext = kryo.getGraphContext();
        ObjectInputStream objectStream = (ObjectInputStream) graphContext.get(this);
        if (objectStream == null) {
            objectStream = new ObjectInputStream(input) {
                @Override
                protected Class<?> resolveClass(ObjectStreamClass desc) throws ClassNotFoundException {
                    return ClassUtils.getClass(KryoSerialization.class.getClassLoader(), desc.getName());
                }
            };
            graphContext.put(this, objectStream);
        }
        return objectStream.readObject();
    } catch (Exception ex) {
        throw new KryoException("Error during Java deserialization.", ex);
    }
}
 
Example 3
Source Project: Flink-CEPplus   Source File: KryoUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Tries to copy the given record from using the provided Kryo instance. If this fails, then
 * the record from is copied by serializing it into a byte buffer and deserializing it from
 * there.
 *
 * @param from Element to copy
 * @param reuse Reuse element for the deserialization
 * @param kryo Kryo instance to use
 * @param serializer TypeSerializer which is used in case of a Kryo failure
 * @param <T> Type of the element to be copied
 * @return Copied element
 */
public static <T> T copy(T from, T reuse, Kryo kryo, TypeSerializer<T> serializer) {
	try {
		return kryo.copy(from);
	} catch (KryoException ke) {
		// Kryo could not copy the object --> try to serialize/deserialize the object
		try {
			byte[] byteArray = InstantiationUtil.serializeToByteArray(serializer, from);

			return InstantiationUtil.deserializeFromByteArray(serializer, reuse, byteArray);
		} catch (IOException ioe) {
			throw new RuntimeException("Could not copy object by serializing/deserializing" +
				" it.", ioe);
		}
	}
}
 
Example 4
Source Project: phrasal   Source File: IOTools.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Deserialize an object.
 * Only supports BIN and BIN_GZ SerializationMode.
 * 
 * If the specified file does not exist or is empty, then this call returns null.
 * 
 * @param inputStream
 * @param type
 * @param mode
 * @return
 * @throws IOException
 * @throws ClassNotFoundException
 */
public static <T> T deserialize(InputStream inStream, Class<T> type, SerializationMode mode) {
  try {
    T object;
    if (mode == SerializationMode.BIN || mode == SerializationMode.BIN_GZ) {
      Kryo kryo = new Kryo();
      kryo.setReferences(false);
      Input input = new Input(mode == SerializationMode.BIN_GZ ? 
          new GZIPInputStream(inStream) : inStream);
      object = kryo.readObject(input, type);
      
    } else {
      throw new UnsupportedOperationException();
    }

    return object;
    
  } catch (KryoException  | IOException e) {
    logger.error("Unable to deserialize {} (mode: {})", mode);
    logger.error("Deserialization exception", e);
    throw new RuntimeException(e);
  }
}
 
Example 5
Source Project: flink   Source File: JavaSerializer.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"unchecked", "rawtypes"})
@Override
public T read(Kryo kryo, Input input, Class aClass) {
	try {
		ObjectMap graphContext = kryo.getGraphContext();
		ObjectInputStream objectStream = (ObjectInputStream)graphContext.get(this);
		if (objectStream == null) {
			// make sure we use Kryo's classloader
			objectStream = new InstantiationUtil.ClassLoaderObjectInputStream(input, kryo.getClassLoader());
			graphContext.put(this, objectStream);
		}
		return (T) objectStream.readObject();
	} catch (Exception ex) {
		throw new KryoException("Error during Java deserialization.", ex);
	}
}
 
Example 6
Source Project: flink   Source File: KryoUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Tries to copy the given record from using the provided Kryo instance. If this fails, then
 * the record from is copied by serializing it into a byte buffer and deserializing it from
 * there.
 *
 * @param from Element to copy
 * @param reuse Reuse element for the deserialization
 * @param kryo Kryo instance to use
 * @param serializer TypeSerializer which is used in case of a Kryo failure
 * @param <T> Type of the element to be copied
 * @return Copied element
 */
public static <T> T copy(T from, T reuse, Kryo kryo, TypeSerializer<T> serializer) {
	try {
		return kryo.copy(from);
	} catch (KryoException ke) {
		// Kryo could not copy the object --> try to serialize/deserialize the object
		try {
			byte[] byteArray = InstantiationUtil.serializeToByteArray(serializer, from);

			return InstantiationUtil.deserializeFromByteArray(serializer, reuse, byteArray);
		} catch (IOException ioe) {
			throw new RuntimeException("Could not copy object by serializing/deserializing" +
				" it.", ioe);
		}
	}
}
 
Example 7
Source Project: flink   Source File: NoFetchingInput.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Require makes sure that at least required number of bytes are kept in the buffer. If not, then
 * it will load exactly the difference between required and currently available number of bytes.
 * Thus, it will only load the data which is required and never prefetch data.
 *
 * @param required the number of bytes being available in the buffer
 * @return the number of bytes remaining, which is equal to required
 * @throws KryoException
 */
@Override
protected int require(int required) throws KryoException {
	if(required > capacity) {
		throw new KryoException("Buffer too small: capacity: " + capacity + ", " +
				"required: " + required);
	}

	position = 0;
	int bytesRead = 0;
	int count;
	while(true){
		count = fill(buffer, bytesRead, required - bytesRead);

		if(count == -1){
			throw new KryoException(new EOFException("No more bytes left."));
		}

		bytesRead += count;
		if(bytesRead == required){
			break;
		}
	}
	limit = required;
	return required;
}
 
Example 8
Source Project: flink   Source File: KryoUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Tries to copy the given record from using the provided Kryo instance. If this fails, then
 * the record from is copied by serializing it into a byte buffer and deserializing it from
 * there.
 *
 * @param from Element to copy
 * @param kryo Kryo instance to use
 * @param serializer TypeSerializer which is used in case of a Kryo failure
 * @param <T> Type of the element to be copied
 * @return Copied element
 */
public static <T> T copy(T from, Kryo kryo, TypeSerializer<T> serializer) {
	try {
		return kryo.copy(from);
	} catch (KryoException ke) {
		// Kryo could not copy the object --> try to serialize/deserialize the object
		try {
			byte[] byteArray = InstantiationUtil.serializeToByteArray(serializer, from);

			return InstantiationUtil.deserializeFromByteArray(serializer, byteArray);
		} catch (IOException ioe) {
			throw new RuntimeException("Could not copy object by serializing/deserializing" +
				" it.", ioe);
		}
	}
}
 
Example 9
Source Project: turbo-rpc   Source File: FastClassResolver.java    License: Apache License 2.0 6 votes vote down vote up
public Registration readClass(Input input) {
	int classID = input.readVarInt(true);
	switch (classID) {
	case Kryo.NULL:
		if (TRACE || (DEBUG && kryo.getDepth() == 1))
			log("Read", null);
		return null;
	case NAME + 2: // Offset for NAME and NULL.
		return readName(input);
	}
	if (classID == memoizedClassId)
		return memoizedClassIdValue;
	Registration registration = idToRegistration.get(classID - 2);
	if (registration == null)
		throw new KryoException("Encountered unregistered class ID: " + (classID - 2));
	if (TRACE)
		trace("kryo", "Read class " + (classID - 2) + ": " + className(registration.getType()));
	memoizedClassId = classID;
	memoizedClassIdValue = registration;
	return registration;
}
 
Example 10
Source Project: flink   Source File: JavaSerializer.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"unchecked", "rawtypes"})
@Override
public T read(Kryo kryo, Input input, Class aClass) {
	try {
		ObjectMap graphContext = kryo.getGraphContext();
		ObjectInputStream objectStream = (ObjectInputStream)graphContext.get(this);
		if (objectStream == null) {
			// make sure we use Kryo's classloader
			objectStream = new InstantiationUtil.ClassLoaderObjectInputStream(input, kryo.getClassLoader());
			graphContext.put(this, objectStream);
		}
		return (T) objectStream.readObject();
	} catch (Exception ex) {
		throw new KryoException("Error during Java deserialization.", ex);
	}
}
 
Example 11
Source Project: Bats   Source File: FastPublisher.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void write(int value) throws KryoException
{
  if (!writeBuffer.hasRemaining()) {
    advanceWriteBuffer();
  }
  writeBuffer.put((byte)value);
}
 
Example 12
Source Project: Bats   Source File: FastPublisher.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void write(byte[] bytes) throws KryoException
{
  int remaining = writeBuffer.remaining();
  if (bytes.length > remaining) {
    writeBuffer.put(bytes, 0, remaining);
    advanceWriteBuffer();
    write(bytes, remaining, bytes.length - remaining);
  } else {
    writeBuffer.put(bytes);
  }
}
 
Example 13
public boolean readBoolean() throws EOFException {
    try {
        return input.readBoolean();
    } catch (KryoException e) {
        throw maybeEndOfStream(e);
    }
}
 
Example 14
Source Project: Bats   Source File: FastPublisher.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void writeByte(int value) throws KryoException
{
  if (!writeBuffer.hasRemaining()) {
    advanceWriteBuffer();
  }
  writeBuffer.put((byte)value);
}
 
Example 15
Source Project: dubbox   Source File: KryoObjectInput.java    License: Apache License 2.0 5 votes vote down vote up
public short readShort() throws IOException {
    try {
        return input.readShort();
    } catch (KryoException e) {
        throw new IOException(e);
    }
}
 
Example 16
public int readInt() throws EOFException {
    try {
        return input.readInt();
    } catch (KryoException e) {
        throw maybeEndOfStream(e);
    }
}
 
Example 17
Source Project: dubbox   Source File: KryoObjectInput.java    License: Apache License 2.0 5 votes vote down vote up
public String readUTF() throws IOException {
        // TODO
        try {
//            return kryo.readObject(input, String.class);
            return input.readString();
        } catch (KryoException e) {
            throw new IOException(e);
        }
    }
 
Example 18
public void readBytes(byte[] buffer, int offset, int count) throws EOFException {
    try {
        input.readBytes(buffer, offset, count);
    } catch (KryoException e) {
        throw maybeEndOfStream(e);
    }
}
 
Example 19
Source Project: dubbox   Source File: KryoObjectInput.java    License: Apache License 2.0 5 votes vote down vote up
public int readInt() throws IOException {
    try {
        return input.readInt();
    } catch (KryoException e) {
        throw new IOException(e);
    }
}
 
Example 20
Source Project: dubbox   Source File: KryoObjectInput.java    License: Apache License 2.0 5 votes vote down vote up
public String readUTF() throws IOException {
        // TODO
        try {
//            return kryo.readObject(input, String.class);
            return input.readString();
        } catch (KryoException e) {
            throw new IOException(e);
        }
    }
 
Example 21
public byte readByte() throws EOFException {
    try {
        return input.readByte();
    } catch (KryoException e) {
        throw maybeEndOfStream(e);
    }
}
 
Example 22
Source Project: dubbox-hystrix   Source File: KryoObjectInput.java    License: Apache License 2.0 5 votes vote down vote up
public short readShort() throws IOException {
    try {
        return input.readShort();
    } catch (KryoException e) {
        throw new IOException(e);
    }
}
 
Example 23
Source Project: attic-apex-core   Source File: FastPublisher.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void write(int value) throws KryoException
{
  if (!writeBuffer.hasRemaining()) {
    advanceWriteBuffer();
  }
  writeBuffer.put((byte)value);
}
 
Example 24
Source Project: dubbox   Source File: KryoObjectInput.java    License: Apache License 2.0 5 votes vote down vote up
public Object readObject() throws IOException, ClassNotFoundException {
        // TODO
//        throw new UnsupportedOperationException();
        try {
            return kryo.readClassAndObject(input);
        } catch (KryoException e) {
            throw new IOException(e);
        }
    }
 
Example 25
Source Project: dubbox   Source File: KryoObjectInput.java    License: Apache License 2.0 5 votes vote down vote up
public byte[] readBytes() throws IOException {
    try {
        int len = input.readInt();
        if (len < 0) {
            return null;
        } else if (len == 0) {
            return new byte[]{};
        } else {
            return input.readBytes(len);
        }
    } catch (KryoException e) {
        throw new IOException(e);
    }
}
 
Example 26
Source Project: dubbo-2.6.5   Source File: KryoObjectInput.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean readBool() throws IOException {
    try {
        return input.readBoolean();
    } catch (KryoException e) {
        throw new IOException(e);
    }
}
 
Example 27
public String readNullableString() throws EOFException {
    try {
        return input.readString();
    } catch (KryoException e) {
        throw maybeEndOfStream(e);
    }
}
 
Example 28
Source Project: dubbo-2.6.5   Source File: KryoObjectInput.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public float readFloat() throws IOException {
    try {
        return input.readFloat();
    } catch (KryoException e) {
        throw new IOException(e);
    }
}
 
Example 29
Source Project: dubbox-hystrix   Source File: KryoObjectInput.java    License: Apache License 2.0 5 votes vote down vote up
public double readDouble() throws IOException {
    try {
        return input.readDouble();
    } catch (KryoException e) {
        throw new IOException(e);
    }
}
 
Example 30
Source Project: dubbox   Source File: KryoObjectInput.java    License: Apache License 2.0 5 votes vote down vote up
public float readFloat() throws IOException {
    try {
        return input.readFloat();
    } catch (KryoException e) {
        throw new IOException(e);
    }
}