/** * 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); } }
/** * 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); } }
/** * 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); } }
@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); } }
@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); } }
@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); } }
@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()); } }
@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; } } }
/** * 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); } } }
/** * 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); } } }
/** * 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; }
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); }
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); }
/** 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; }
/** 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; }
/** * 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; }
private <E> E readTableFile(String key, File originalFile) { try { return readContent(originalFile, getKryo()); } catch (FileNotFoundException | KryoException | ClassCastException e) { Throwable exception = e; // Give one more chance, read data in paper 1.x compatibility mode if (e instanceof KryoException) { try { return readContent(originalFile, createKryoInstance(true)); } catch (FileNotFoundException | KryoException | ClassCastException compatibleReadException) { exception = compatibleReadException; } } String errorMessage = "Couldn't read/deserialize file " + originalFile + " for table " + key; throw new PaperDbException(errorMessage, exception); } }
public BigDecimal read (Kryo kryo, Input input, Class<BigDecimal> type) { BigInteger unscaledValue = bigIntegerSerializer.read(kryo, input, BigInteger.class); if (unscaledValue == null) return null; int scale = input.readInt(false); if (type != BigDecimal.class && type != null) { // For subclasses, use reflection try { Constructor<BigDecimal> constructor = type.getConstructor(BigInteger.class, int.class); if (!constructor.isAccessible()) { try { constructor.setAccessible(true); } catch (SecurityException se) {} } return constructor.newInstance(unscaledValue, scale); } catch (Exception ex) { throw new KryoException(ex); } } // fast-path optimizations for BigDecimal constants if (unscaledValue == BigInteger.ZERO && scale == 0) { return BigDecimal.ZERO; } // default behaviour return new BigDecimal(unscaledValue, scale); }
public void write (Kryo kryo, Output output, Object object) { Cipher cipher = getCipher(Cipher.ENCRYPT_MODE); CipherOutputStream cipherStream = new CipherOutputStream(output, cipher); Output cipherOutput = new Output(cipherStream, 256) { public void close () throws KryoException { // Don't allow the CipherOutputStream to close the output. } }; serializer.write(kryo, cipherOutput, object); cipherOutput.flush(); try { cipherStream.close(); } catch (IOException ex) { throw new KryoException(ex); } }
/** 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 ((niobuffer.get(p++) & 0x80) == 0) return true; if (p == limit) return false; if ((niobuffer.get(p++) & 0x80) == 0) return true; if (p == limit) return false; if ((niobuffer.get(p++) & 0x80) == 0) return true; if (p == limit) return false; if ((niobuffer.get(p++) & 0x80) == 0) return true; if (p == limit) return false; if ((niobuffer.get(p++) & 0x80) == 0) return true; if (p == limit) return false; if ((niobuffer.get(p++) & 0x80) == 0) return true; if (p == limit) return false; if ((niobuffer.get(p++) & 0x80) == 0) return true; if (p == limit) return false; if ((niobuffer.get(p++) & 0x80) == 0) return true; if (p == limit) return false; return true; }
/** Writes a string that is known to contain only ASCII characters. Non-ASCII strings passed to this method will be corrupted. * Each byte is a 7 bit character with the remaining byte denoting if another character is available. This is slightly more * efficient than {@link #writeString(String)}. The string can be read using {@link Input#readString()} or * {@link Input#readStringBuilder()}. * @param value May be null. */ public void writeAscii (String value) throws KryoException { if (value == null) { writeByte(0x80); // 0 means null, bit 8 means UTF8. return; } int charCount = value.length(); switch (charCount) { case 0: writeByte(1 | 0x80); // 1 is string length + 1, bit 8 means UTF8. return; case 1: writeByte(2 | 0x80); // 2 is string length + 1, bit 8 means UTF8. writeByte(value.charAt(0)); return; } if (capacity - position < charCount) writeAscii_slow(value, charCount); else { getBytes(value, 0, charCount, buffer, position); position += charCount; } buffer[position - 1] |= 0x80; // Bit 8 means end of ASCII. }
public T read (Kryo kryo, Input input, Class<T> type) { T object = create(kryo, input, type); kryo.reference(object); int fieldCount = input.readVarInt(true); int[] tags = this.tags; CachedField[] fields = getFields(); for (int i = 0, n = fieldCount; i < n; i++) { int tag = input.readVarInt(true); CachedField cachedField = null; for (int ii = 0, nn = tags.length; ii < nn; ii++) { if (tags[ii] == tag) { cachedField = fields[ii]; break; } } if (cachedField == null) throw new KryoException("Unknown field tag: " + tag + " (" + getType().getName() + ")"); cachedField.read(input, object); } return object; }
/** @return true if the buffer has been resized. */ protected boolean require (int required) throws KryoException { if (capacity - position >= required) return false; if (required > maxCapacity) throw new KryoException("Buffer overflow. Max capacity: " + maxCapacity + ", required: " + required); flush(); while (capacity - position < required) { if (capacity == maxCapacity) throw new KryoException("Buffer overflow. Available: " + (capacity - position) + ", required: " + required); // Grow buffer. if (capacity == 0) capacity = 1; capacity = Math.min(capacity * 2, maxCapacity); if (capacity < 0) capacity = maxCapacity; byte[] newBuffer = new byte[capacity]; System.arraycopy(buffer, 0, newBuffer, 0, position); buffer = newBuffer; } return true; }
/** * Serialize an object. * Only supports BIN and BIN_GZ SerializationMode. * * @param outputStream * @param o * @throws IOException */ public static void serialize(OutputStream outStream, Object o, SerializationMode mode) { try { if (mode == SerializationMode.BIN || mode == SerializationMode.BIN_GZ) { Kryo kryo = new Kryo(); kryo.setReferences(false); Output output = mode == SerializationMode.BIN_GZ ? new Output(new GZIPOutputStream( outStream)) : new Output(outStream); kryo.writeObject(output, o); output.close(); } else { logger.warn("Unsupported serialization mode: {} file: {}", mode); } } catch (KryoException | IOException e) { logger.error("Serialization exception", e); throw new RuntimeException(e); } }
public byte readByte() throws EOFException { try { return input.readByte(); } catch (KryoException e) { throw maybeEndOfStream(e); } }
public void readBytes(byte[] buffer, int offset, int count) throws EOFException { try { input.readBytes(buffer, offset, count); } catch (KryoException e) { throw maybeEndOfStream(e); } }
public long readLong() throws EOFException { try { return input.readLong(); } catch (KryoException e) { throw maybeEndOfStream(e); } }
public long readSmallLong() throws EOFException, IOException { try { return input.readLong(true); } catch (KryoException e) { throw maybeEndOfStream(e); } }
public int readInt() throws EOFException { try { return input.readInt(); } catch (KryoException e) { throw maybeEndOfStream(e); } }
public int readSmallInt() throws EOFException { try { return input.readInt(true); } catch (KryoException e) { throw maybeEndOfStream(e); } }
public boolean readBoolean() throws EOFException { try { return input.readBoolean(); } catch (KryoException e) { throw maybeEndOfStream(e); } }
public String readNullableString() throws EOFException { try { return input.readString(); } catch (KryoException e) { throw maybeEndOfStream(e); } }
@Override public 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 OmegaException("Unable to deserialize message", e); } }
public boolean readBool() throws IOException { try { return input.readBoolean(); } catch (KryoException e) { throw new IOException(e); } }
public byte readByte() throws IOException { try { return input.readByte(); } catch (KryoException e) { throw new IOException(e); } }
public short readShort() throws IOException { try { return input.readShort(); } catch (KryoException e) { throw new IOException(e); } }
public int readInt() throws IOException { try { return input.readInt(); } catch (KryoException e) { throw new IOException(e); } }
public long readLong() throws IOException { try { return input.readLong(); } catch (KryoException e) { throw new IOException(e); } }
public float readFloat() throws IOException { try { return input.readFloat(); } catch (KryoException e) { throw new IOException(e); } }