@Override public long getLength() throws IOException, InterruptedException { return split.getLength(); }
/** * Deserialize a {@link Split} that was written by the {@link #serialize(Split, DataOutput)} method * using a provided {@link ClassLoader} to load the {@link Split} class. * * @param in the {@link DataInput} for reading the serialized data * @param classLoader the {@link ClassLoader} for loading the actual {@link Split} class * @return a {@link Split} instance * @throws IOException if failed to deserialize * @throws ClassNotFoundException if failed to load the split class as indicated by the encoded string */ public static Split deserialize(DataInput in, ClassLoader classLoader) throws IOException, ClassNotFoundException { String className = in.readUTF(); try { Split split = (Split) classLoader.loadClass(className).getConstructor().newInstance(); split.readExternal(in); return split; } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException | UnsupportedOperationException e) { // If failed to instantiate the class using default constructor or the readExternal // throws UnsupportedOperationException, deserialize with Gson } // Use Gson if failed to do readExternal. byte[] bytes = new byte[in.readInt()]; in.readFully(bytes); return (Split) new Gson().fromJson(new InputStreamReader(new ByteArrayInputStream(bytes), StandardCharsets.UTF_8), classLoader.loadClass(className)); }
/** * Serialize a {@link Split} instance. * * @param split the {@link Split} to serialize * @param out the {@link DataOutput} that the serialized data is writing to * @throws IOException if failed to serialize */ public static void serialize(Split split, DataOutput out) throws IOException { try { // Write out the class name out.writeUTF(split.getClass().getName()); // Write out the split. For custom split that doesn't override the writeExternal method, // an UnsupportedOperationException will be thrown. // Also if the Split doesn't have a public default constructor, use Gson as well if ((split.getClass().getConstructor().getModifiers() & Modifier.PUBLIC) == Modifier.PUBLIC) { split.writeExternal(out); return; } } catch (UnsupportedOperationException | NoSuchMethodException e) { // Use gson } ByteArrayOutputStream os = new ByteArrayOutputStream(); try (Writer writer = new OutputStreamWriter(os, StandardCharsets.UTF_8)) { new Gson().toJson(split, writer); } byte[] bytes = os.toByteArray(); out.writeInt(bytes.length); out.write(bytes); }
@Override public long getLength() throws IOException, InterruptedException { return split.getLength(); }
@Override public long getLength() { return dataSetSplit.getLength(); }
@Override public long getLength() { return dataSetSplit.getLength(); }