當前位置: 首頁>>代碼示例>>Java>>正文


Java KryoException類代碼示例

本文整理匯總了Java中com.esotericsoftware.kryo.KryoException的典型用法代碼示例。如果您正苦於以下問題:Java KryoException類的具體用法?Java KryoException怎麽用?Java KryoException使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


KryoException類屬於com.esotericsoftware.kryo包,在下文中一共展示了KryoException類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: write

import com.esotericsoftware.kryo.KryoException; //導入依賴的package包/類
/**
 * Write the given instance to the given output.
 *
 * @param kryo      instance of {@link Kryo} object
 * @param output    a Kryo {@link Output} object
 * @param t         instance to serialize
 */
@Override
public void write(Kryo kryo, Output output, T t) {
    try {
        preSerialize(t);

        try(ByteArrayOutputStream stream = new ByteArrayOutputStream(4096)) {

            // write the object using the HTM serializer
            HTMObjectOutput writer = serializer.getObjectOutput(stream);
            writer.writeObject(t, t.getClass());
            writer.close();

            // write the serialized data
            output.writeInt(stream.size());
            stream.writeTo(output);

            LOGGER.debug("wrote {} bytes", stream.size());
        }
    }
    catch(IOException e) {
        throw new KryoException(e);
    }
}
 
開發者ID:htm-community,項目名稱:flink-htm,代碼行數:31,代碼來源:KryoSerializer.java

示例2: read

import com.esotericsoftware.kryo.KryoException; //導入依賴的package包/類
/**
 * Read an instance of the given class from the given input.
 *
 * @param kryo      instance of {@link Kryo} object
 * @param input     a Kryo {@link Input}
 * @param aClass    The class of the object to be read in.
 * @return  an instance of type <T>
 */
@Override
public T read(Kryo kryo, Input input, Class<T> aClass) {

    // read the serialized data
    byte[] data = new byte[input.readInt()];
    input.readBytes(data);

    try {
        try(ByteArrayInputStream stream = new ByteArrayInputStream(data)) {
            HTMObjectInput reader = serializer.getObjectInput(stream);
            T t = (T) reader.readObject(aClass);

            postDeSerialize(t);

            return t;
        }
    }
    catch(Exception e) {
        throw new KryoException(e);
    }
}
 
開發者ID:htm-community,項目名稱:flink-htm,代碼行數:30,代碼來源:KryoSerializer.java

示例3: copy

import com.esotericsoftware.kryo.KryoException; //導入依賴的package包/類
/**
 * Copy the given instance.
 * @param kryo instance of {@link Kryo} object
 * @param original an object to copy.
 * @return
 */
@Override
public T copy(Kryo kryo, T original) {
    try {
        preSerialize(original);

        try(CopyStream output = new CopyStream(4096)) {
            HTMObjectOutput writer = serializer.getObjectOutput(output);
            writer.writeObject(original, original.getClass());
            writer.close();

            try(InputStream input = output.toInputStream()) {
                HTMObjectInput reader = serializer.getObjectInput(input);
                T t = (T) reader.readObject(original.getClass());

                postDeSerialize(t);

                return t;
            }
        }
    }
    catch(Exception e) {
        throw new KryoException(e);
    }
}
 
開發者ID:htm-community,項目名稱:flink-htm,代碼行數:31,代碼來源:KryoSerializer.java

示例4: read

import com.esotericsoftware.kryo.KryoException; //導入依賴的package包/類
@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 IOException, 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);
    }
}
 
開發者ID:cuba-platform,項目名稱:cuba,代碼行數:20,代碼來源:KryoSerialization.java

示例5: write

import com.esotericsoftware.kryo.KryoException; //導入依賴的package包/類
@SuppressWarnings({"unchecked", "rawtypes"})
@Override
public void write(Kryo kryo, Output output, T o) {
	try {
		ObjectMap graphContext = kryo.getGraphContext();
		ObjectOutputStream objectStream = (ObjectOutputStream)graphContext.get(this);
		if (objectStream == null) {
			objectStream = new ObjectOutputStream(output);
			graphContext.put(this, objectStream);
		}
		objectStream.writeObject(o);
		objectStream.flush();
	} catch (Exception ex) {
		throw new KryoException("Error during Java serialization.", ex);
	}
}
 
開發者ID:axbaretto,項目名稱:flink,代碼行數:17,代碼來源:JavaSerializer.java

示例6: read

import com.esotericsoftware.kryo.KryoException; //導入依賴的package包/類
@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);
	}
}
 
開發者ID:axbaretto,項目名稱:flink,代碼行數:17,代碼來源:JavaSerializer.java

示例7: copy

import com.esotericsoftware.kryo.KryoException; //導入依賴的package包/類
@SuppressWarnings("unchecked")
@Override
public T copy(T from) {
	if (from == null) {
		return null;
	}
	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());
	}
}
 
開發者ID:axbaretto,項目名稱:flink,代碼行數:26,代碼來源:KryoSerializer.java

示例8: deserialize

import com.esotericsoftware.kryo.KryoException; //導入依賴的package包/類
@SuppressWarnings("unchecked")
@Override
public T deserialize(DataInputView source) throws IOException {
	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;
		}
	}
}
 
開發者ID:axbaretto,項目名稱:flink,代碼行數:23,代碼來源:KryoSerializer.java

示例9: copy

import com.esotericsoftware.kryo.KryoException; //導入依賴的package包/類
/**
 * 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);
		}
	}
}
 
開發者ID:axbaretto,項目名稱:flink,代碼行數:27,代碼來源:KryoUtils.java

示例10: require

import com.esotericsoftware.kryo.KryoException; //導入依賴的package包/類
/**
 * 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;
}
 
開發者ID:axbaretto,項目名稱:flink,代碼行數:35,代碼來源:NoFetchingInput.java

示例11: readName

import com.esotericsoftware.kryo.KryoException; //導入依賴的package包/類
protected Registration readName (Input input) {
	int nameId = input.readVarInt(true);
	if (nameIdToClass == null) nameIdToClass = new IntMap();
	Class type = nameIdToClass.get(nameId);
	if (type == null) {
		// Only read the class name the first time encountered in object graph.
		String className = input.readString();
		if (nameToClass != null) type = nameToClass.get(className);
		if (type == null) {
			try {
				type = Class.forName(className, false, kryo.getClassLoader());
			} catch (ClassNotFoundException ex) {
				throw new KryoException("Unable to find class: " + className, ex);
			}
			if (nameToClass == null) nameToClass = new ObjectMap();
			nameToClass.put(className, type);
		}
		nameIdToClass.put(nameId, type);
		if (TRACE) trace("kryo", "Read class name: " + className);
	} else {
		if (TRACE) trace("kryo", "Read class name reference " + nameId + ": " + className(type));
	}
	return kryo.getRegistration(type);
}
 
開發者ID:HoratiusTang,項目名稱:EsperDist,代碼行數:25,代碼來源:DefaultClassResolver.java

示例12: readName

import com.esotericsoftware.kryo.KryoException; //導入依賴的package包/類
protected Registration readName (Input input) {
	int nameId = input.readVarInt(true);
	if (nameIdToClass == null) nameIdToClass = new IntMap();
	Class type = nameIdToClass.get(nameId);
	if (type == null) {
		// Only read the class name the first time encountered in object graph.
		String className = input.readString();
		type = getTypeByName(className);
		if (type == null) {
			try {
				type = Class.forName(className, false, kryo.getClassLoader());
			} catch (ClassNotFoundException ex) {
				throw new KryoException("Unable to find class: " + className, ex);
			}
			if (nameToClass == null) nameToClass = new ObjectMap();
			nameToClass.put(className, type);
		}
		nameIdToClass.put(nameId, type);
		if (TRACE) trace("kryo", "Read class name: " + className);
	} else {
		if (TRACE) trace("kryo", "Read class name reference " + nameId + ": " + className(type));
	}
	return kryo.getRegistration(type);
}
 
開發者ID:HoratiusTang,項目名稱:EsperDist,代碼行數:25,代碼來源:DefaultClassResolver.java

示例13: read

import com.esotericsoftware.kryo.KryoException; //導入依賴的package包/類
/** Reads count bytes or less and writes them to the specified byte[], starting at offset, and returns the number of bytes read
 * or -1 if no more bytes are available. */
public int read (byte[] bytes, int offset, int count) throws KryoException {
	niobuffer.position(position);
	if (bytes == null) throw new IllegalArgumentException("bytes cannot be null.");
	int startingCount = count;
	int copyCount = Math.min(limit - position, count);
	while (true) {
		niobuffer.get(bytes, offset, copyCount);
		position += copyCount;
		count -= copyCount;
		if (count == 0) break;
		offset += copyCount;
		copyCount = optional(count);
		if (copyCount == -1) {
			// End of data.
			if (startingCount == count) return -1;
			break;
		}
		if (position == limit) break;
	}
	return startingCount - count;
}
 
開發者ID:HoratiusTang,項目名稱:EsperDist,代碼行數:24,代碼來源:ByteBufferInput.java

示例14: canReadLong

import com.esotericsoftware.kryo.KryoException; //導入依賴的package包/類
/** Returns true if enough bytes are available to read a long with {@link #readLong(boolean)}. */
public boolean canReadLong () throws KryoException {
	if (limit - position >= 9) return true;
	if (optional(5) <= 0) return false;
	int p = position;
	if ((buffer[p++] & 0x80) == 0) return true;
	if (p == limit) return false;
	if ((buffer[p++] & 0x80) == 0) return true;
	if (p == limit) return false;
	if ((buffer[p++] & 0x80) == 0) return true;
	if (p == limit) return false;
	if ((buffer[p++] & 0x80) == 0) return true;
	if (p == limit) return false;
	if ((buffer[p++] & 0x80) == 0) return true;
	if (p == limit) return false;
	if ((buffer[p++] & 0x80) == 0) return true;
	if (p == limit) return false;
	if ((buffer[p++] & 0x80) == 0) return true;
	if (p == limit) return false;
	if ((buffer[p++] & 0x80) == 0) return true;
	if (p == limit) return false;
	return true;
}
 
開發者ID:HoratiusTang,項目名稱:EsperDist,代碼行數:24,代碼來源:Input.java

示例15: read

import com.esotericsoftware.kryo.KryoException; //導入依賴的package包/類
/**
 * Utility method to read any object written with
 * {@link #write(Object, DataOutput)}.
 * 
 * @param <T>
 *            type of object
 * @param in
 *            input
 * @return the object
 * @throws IOException
 */
@SuppressWarnings("unchecked")
public static <T> T read(DataInput in) throws IOException {
	final int length = in.readInt();

	final byte[] bytes = new byte[length];
	in.readFully(bytes);

	final Kryo kryo = new Kryo();
	Object obj;
	try {
		obj = kryo.readClassAndObject(new Input(bytes));
	} catch (final KryoException e) {
		kryo.setInstantiatorStrategy(new StdInstantiatorStrategy());
		obj = kryo.readClassAndObject(new Input(bytes));
	}
	return (T) obj;
}
 
開發者ID:openimaj,項目名稱:openimaj,代碼行數:29,代碼來源:IOUtils.java


注:本文中的com.esotericsoftware.kryo.KryoException類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。