com.esotericsoftware.kryo.KryoException Java Examples

The following examples show how to use com.esotericsoftware.kryo.KryoException. 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: KryoUtils.java    From flink with 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 #2
Source File: JavaSerializer.java    From flink with 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 #3
Source File: NoFetchingInput.java    From flink with 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 #4
Source File: KryoUtils.java    From Flink-CEPplus with 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 #5
Source File: IOTools.java    From phrasal with 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 #6
Source File: JavaSerializer.java    From flink with 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 #7
Source File: KryoSerialization.java    From cuba with 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 #8
Source File: KryoUtils.java    From flink with 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 File: FastClassResolver.java    From turbo-rpc with 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 File: FastPublisher.java    From Bats with 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 #11
Source File: KryoBackedDecoder.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public String readNullableString() throws EOFException {
    try {
        return input.readString();
    } catch (KryoException e) {
        throw maybeEndOfStream(e);
    }
}
 
Example #12
Source File: KryoObjectInput.java    From dubbox-hystrix with 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 #13
Source File: KryoObjectInput.java    From dubbox with 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 #14
Source File: NoFetchingInput.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public void readBytes(byte[] bytes, int offset, int count) throws KryoException {
	if(bytes == null){
		throw new IllegalArgumentException("bytes cannot be null.");
	}

	try{
		int bytesRead = 0;
		int c;

		while(true){
			c = inputStream.read(bytes, offset+bytesRead, count-bytesRead);

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

			bytesRead += c;

			if(bytesRead == count){
				break;
			}
		}
	}catch(IOException ex){
		throw new KryoException(ex);
	}
}
 
Example #15
Source File: KryoBackedDecoder.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public byte readByte() throws EOFException {
    try {
        return input.readByte();
    } catch (KryoException e) {
        throw maybeEndOfStream(e);
    }
}
 
Example #16
Source File: ByteBufOutput.java    From turbo-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * Writes the length and CharSequence as UTF8, or null. The string can be read
 * using {@link Input#readString()} or {@link Input#readStringBuilder()}.
 * 
 * @param value
 *            May be null.
 */
public void writeString(CharSequence value) throws KryoException {
	if (value == null) {
		writeStringFast(null);
		return;
	}

	writeStringFast(value.toString());
	return;
}
 
Example #17
Source File: KryoBackedDecoder.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public int readSmallInt() throws EOFException {
    try {
        return input.readInt(true);
    } catch (KryoException e) {
        throw maybeEndOfStream(e);
    }
}
 
Example #18
Source File: KryoBackedDecoder.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public int readSmallInt() throws EOFException {
    try {
        return input.readInt(true);
    } catch (KryoException e) {
        throw maybeEndOfStream(e);
    }
}
 
Example #19
Source File: FastPublisher.java    From Bats with 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 #20
Source File: KryoObjectInput.java    From dubbox with 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 #21
Source File: GrpcTxEventEndpointImpl.java    From txle with Apache License 2.0 5 votes vote down vote up
private Object[] deserialize(byte[] message) {
    try {
        Input input = new Input(new ByteArrayInputStream(message));
        Kryo kryo = pool.borrow();
        Object[] objects = kryo.readObjectOrNull(input, Object[].class);
        pool.release(kryo);
        return objects;
    } catch (KryoException e) {
        throw new RuntimeException("Unable to deserialize message", e);
    }
}
 
Example #22
Source File: PyObjectSerializer.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public void write(Kryo kryo, Output output, PyObject po) {
	try {
		byte[] serPo = SerializationUtils.serializeObject(po);
		output.writeInt(serPo.length);
		output.write(serPo);
	} catch (IOException e) {
		throw new KryoException("Failed to serialize object.", e);
	}
}
 
Example #23
Source File: TaskReceiver.java    From jstorm with Apache License 2.0 5 votes vote down vote up
protected void deserialize(KryoTupleDeserializer deserializer, byte[] serMsg, DisruptorQueue queue) {
    long start = deserializeTimer.getTime();
    try {
        if (serMsg == null || serMsg.length == 0) {
            return;
        }

        if (serMsg.length == 1) {
            byte newStatus = serMsg[0];
            LOG.info("Change task status as " + newStatus);
            taskStatus.setStatus(newStatus);
            return;
        }

        // ser_msg.length > 1
        if (bolt != null && bolt instanceof IProtoBatchBolt) {
            ((IProtoBatchBolt) bolt).protoExecute(this, deserializer, queue, serMsg);
        } else {
            deserializeTuple(deserializer, serMsg, queue);
        }

    } catch (Throwable e) {
        if (Utils.exceptionCauseIsInstanceOf(KryoException.class, e))
            throw new RuntimeException(e);
        if (!taskStatus.isShutdown()) {
            LOG.error(idStr + " recv thread error " + JStormUtils.toPrintableString(serMsg), e);
        }
    } finally {
        if (MetricUtils.metricAccurateCal)
            deserializeTimer.updateTime(start);
    }
}
 
Example #24
Source File: KryoObjectInput.java    From JobX with Apache License 2.0 5 votes vote down vote up
public String readUTF() throws IOException {
    try {
        return input.readString();
    } catch (KryoException e) {
        throw new IOException(e);
    }
}
 
Example #25
Source File: PTOperator.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
@Override
public void read(final Object object, final Input in) throws KryoException
{
  PhysicalPlan plan = (PhysicalPlan)object;

  int operatorId = in.readInt();
  int stateOrd = in.readInt();
  plan.getAllOperators().get(operatorId).state = PTOperator.State.values()[stateOrd];
}
 
Example #26
Source File: KryoObjectInput.java    From JobX with 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 #27
Source File: KryoBackedDecoder.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public long readLong() throws EOFException {
    try {
        return input.readLong();
    } catch (KryoException e) {
        throw maybeEndOfStream(e);
    }
}
 
Example #28
Source File: KryoSerializer.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public T deserialize(DataInputView source) throws IOException {
	if (CONCURRENT_ACCESS_CHECK) {
		enterExclusiveThread();
	}

	try {
		checkKryoInitialized();

		if (source != previousIn) {
			DataInputViewStream inputStream = new DataInputViewStream(source);
			input = new NoFetchingInput(inputStream);
			previousIn = source;
		}

		try {
			return (T) kryo.readClassAndObject(input);
		} catch (KryoException ke) {
			Throwable cause = ke.getCause();

			if (cause instanceof EOFException) {
				throw (EOFException) cause;
			} else {
				throw ke;
			}
		}
	}
	finally {
		if (CONCURRENT_ACCESS_CHECK) {
			exitExclusiveThread();
		}
	}
}
 
Example #29
Source File: KryoCoder.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public T decode(InputStream inStream) throws IOException {
  final KryoState kryoState = KryoState.get(this);
  final InputChunked inputChunked = kryoState.getInputChunked();
  inputChunked.setInputStream(inStream);
  try {
    @SuppressWarnings("unchecked")
    final T instance = (T) kryoState.getKryo().readClassAndObject(inputChunked);
    return instance;
  } catch (KryoException e) {
    throw new CoderException("Cannot decode object from input stream.", e);
  }
}
 
Example #30
Source File: KryoSerializer.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public T copy(T from) {
	if (from == null) {
		return null;
	}

	if (CONCURRENT_ACCESS_CHECK) {
		enterExclusiveThread();
	}

	try {
		checkKryoInitialized();
		try {
			return kryo.copy(from);
		}
		catch (KryoException ke) {
			// kryo was unable to copy it, so we do it through serialization:
			ByteArrayOutputStream baout = new ByteArrayOutputStream();
			Output output = new Output(baout);

			kryo.writeObject(output, from);

			output.close();

			ByteArrayInputStream bain = new ByteArrayInputStream(baout.toByteArray());
			Input input = new Input(bain);

			return (T)kryo.readObject(input, from.getClass());
		}
	}
	finally {
		if (CONCURRENT_ACCESS_CHECK) {
			exitExclusiveThread();
		}
	}
}