Skip to content

Instantly share code, notes, and snippets.

@pmanvi
Created September 30, 2011 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmanvi/1253853 to your computer and use it in GitHub Desktop.
Save pmanvi/1253853 to your computer and use it in GitHub Desktop.
ObjectInputReaderTemplate - A generic class supporting all the data types
// Follows the template pattern to provide container for ObjectInput
static abstract class ObjectInputReaderTemplate<T> {
private ObjectInput objectInput;
public ObjectInputReaderTemplate(final ObjectInput objectInput){
this.objectInput=objectInput;
}
public final T execute() throws IOException {
T t = get();
if (t instanceof SKIP_START) {
while (true) {
//System.out.println("Reading till we get skip end...");
try {
SKIP_END skipEnd = (SKIP_END) objectInput.readObject();
// Ok we skipped all unwanted stuff, as there was no exception
t = get();
break;
} catch (ClassCastException exp) {
//exp.printStackTrace();
//Its expected to get ClassCastException till
//new information is skipped.
} catch(EOFException e){
// eatup as we have reached end & it came
// because of SKIP_END was at the end
}
catch (IOException exp) {
throw exp;
} catch (ClassNotFoundException ex) {
throw new IOException(ex);
}
}
}
return t;
}
public abstract T get() throws IOException;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment