Created
September 30, 2011 14:15
-
-
Save pmanvi/1253853 to your computer and use it in GitHub Desktop.
ObjectInputReaderTemplate - A generic class supporting all the data types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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