@SuppressWarnings("unchecked") @Override public void read(DataInputView in) throws IOException { super.read(in); // read in a way that allows the stream to recover from exceptions int serializerBytes = in.readInt(); byte[] buffer = new byte[serializerBytes]; in.readFully(buffer); ClassLoader previousClassLoader = Thread.currentThread().getContextClassLoader(); try ( InstantiationUtil.FailureTolerantObjectInputStream ois = new InstantiationUtil.FailureTolerantObjectInputStream(new ByteArrayInputStream(buffer), userClassLoader)) { Thread.currentThread().setContextClassLoader(userClassLoader); typeSerializer = (TypeSerializer<T>) ois.readObject(); } catch (Exception e) { throw new UnloadableTypeSerializerException(e, buffer); } finally { Thread.currentThread().setContextClassLoader(previousClassLoader); } }
@Override public void write(DataOutputView out) throws IOException { out.write(VERSIONED_IDENTIFIER); super.write(out); }
private void resolveVersionRead(int readVersion) throws VersionMismatchException { int[] compatibleVersions = getCompatibleVersions(); for (int compatibleVersion : compatibleVersions) { if (compatibleVersion == readVersion) { return; } } throw new VersionMismatchException( "Incompatible version: found " + readVersion + ", compatible versions are " + Arrays.toString(compatibleVersions)); } }
/** * Returns the compatible version values. * * <p>By default, the base implementation recognizes only the current version (identified by {@link #getVersion()}) * as compatible. This method can be used as a hook and may be overridden to identify more compatible versions. * * @return an array of integers representing the compatible version values. */ public int[] getCompatibleVersions() { return new int[] {getVersion()}; }
@Override public void read(DataInputView in) throws IOException { this.readVersion = in.readInt(); resolveVersionRead(readVersion); }
/** * Returns the found serialization version. If this instance was not read from serialized bytes * but simply instantiated, then the current version is returned. * * @return the read serialization version, or the current version if the instance was not read from bytes. */ public int getReadVersion() { return (readVersion == Integer.MIN_VALUE) ? getVersion() : readVersion; }
@Override public void read(DataInputView in) throws IOException { this.readVersion = in.readInt(); resolveVersionRead(readVersion); }
/** * This read attempts to first identify if the input view contains the special * {@link #VERSIONED_IDENTIFIER} by reading and buffering the first few bytes. * If identified to be versioned, the usual version resolution read path * in {@link VersionedIOReadableWritable#read(DataInputView)} is invoked. * Otherwise, we "reset" the input stream by pushing back the read buffered bytes * into the stream. */ public final void read(InputStream inputStream) throws IOException { byte[] tmp = new byte[VERSIONED_IDENTIFIER.length]; inputStream.read(tmp); if (Arrays.equals(tmp, VERSIONED_IDENTIFIER)) { DataInputView inputView = new DataInputViewStreamWrapper(inputStream); super.read(inputView); read(inputView, true); } else { PushbackInputStream resetStream = new PushbackInputStream(inputStream, VERSIONED_IDENTIFIER.length); resetStream.unread(tmp); read(new DataInputViewStreamWrapper(resetStream), false); } }
/** * Binary format layout of a written serializer snapshot is as follows: * * <ul> * <li>1. Format version of this util.</li> * <li>2. Name of the TypeSerializerSnapshot class.</li> * <li>3. The version of the TypeSerializerSnapshot's binary format.</li> * <li>4. The actual serializer snapshot data.</li> * </ul> */ @SuppressWarnings("deprecation") @Override public void write(DataOutputView out) throws IOException { setSerializerForWriteIfOldPath(serializerSnapshot, serializer); // write the format version of this utils format super.write(out); TypeSerializerSnapshot.writeVersionedSnapshot(out, serializerSnapshot); }
@Override public void write(DataOutputView out) throws IOException { out.writeInt(getVersion()); }
@Override public void read(DataInputView in) throws IOException { this.readVersion = in.readInt(); resolveVersionRead(readVersion); }
private void resolveVersionRead(int readVersion) throws VersionMismatchException { int[] compatibleVersions = getCompatibleVersions(); for (int compatibleVersion : compatibleVersions) { if (compatibleVersion == readVersion) { return; } } throw new VersionMismatchException( "Incompatible version: found " + readVersion + ", compatible versions are " + Arrays.toString(compatibleVersions)); } }
@Override public void read(DataInputView in) throws IOException { super.read(in); this.data = in.readUTF(); }
@Override public void write(DataOutputView out) throws IOException { super.write(out); out.writeUTF(data); }
/** * Returns the found serialization version. If this instance was not read from serialized bytes * but simply instantiated, then the current version is returned. * * @return the read serialization version, or the current version if the instance was not read from bytes. */ public int getReadVersion() { return (readVersion == Integer.MIN_VALUE) ? getVersion() : readVersion; }
private void resolveVersionRead(int readVersion) throws VersionMismatchException { int[] compatibleVersions = getCompatibleVersions(); for (int compatibleVersion : compatibleVersions) { if (compatibleVersion == readVersion) { return; } } throw new VersionMismatchException( "Incompatible version: found " + readVersion + ", compatible versions are " + Arrays.toString(compatibleVersions)); } }
@SuppressWarnings("unchecked") @Override public void read(DataInputView in) throws IOException { // read version super.read(in); final int version = getReadVersion(); switch (version) { case 2: serializerSnapshot = deserializeV2(in, userCodeClassLoader); break; case 1: serializerSnapshot = deserializeV1(in, userCodeClassLoader, serializer); break; default: throw new IOException("Unrecognized version for TypeSerializerSnapshot format: " + version); } }
@Override public void write(DataOutputView out) throws IOException { super.write(out); if (typeSerializer instanceof UnloadableDummyTypeSerializer) { UnloadableDummyTypeSerializer<T> dummyTypeSerializer = (UnloadableDummyTypeSerializer<T>) this.typeSerializer; byte[] serializerBytes = dummyTypeSerializer.getActualBytes(); out.write(serializerBytes.length); out.write(serializerBytes); } else { // write in a way that allows the stream to recover from exceptions try (ByteArrayOutputStreamWithPos streamWithPos = new ByteArrayOutputStreamWithPos()) { InstantiationUtil.serializeObject(streamWithPos, typeSerializer); out.writeInt(streamWithPos.getPosition()); out.write(streamWithPos.getBuf(), 0, streamWithPos.getPosition()); } } }
/** * Returns the compatible version values. * * <p>By default, the base implementation recognizes only the current version (identified by {@link #getVersion()}) * as compatible. This method can be used as a hook and may be overridden to identify more compatible versions. * * @return an array of integers representing the compatible version values. */ public int[] getCompatibleVersions() { return new int[] {getVersion()}; }
@Override public void read(DataInputView in) throws IOException { super.read(in); int numKvStates = in.readShort(); stateMetaInfoSnapshots = new ArrayList<>(numKvStates); for (int i = 0; i < numKvStates; i++) { stateMetaInfoSnapshots.add( OperatorBackendStateMetaInfoSnapshotReaderWriters .getReaderForVersion(getReadVersion(), userCodeClassLoader) .readStateMetaInfo(in)); } }