// Open an InputStream to read from the file File file = new File("hl7_messages.txt"); InputStream is = new FileInputStream(file); // It's generally a good idea to buffer file IO is = new BufferedInputStream(is); // The following class is a HAPI utility that will iterate over // the messages which appear over an InputStream Hl7InputStreamMessageIterator iter = new Hl7InputStreamMessageIterator(is); while (iter.hasNext()) { Message next = iter.next(); // Do something with the message }
/** * Factory method which returns an instance which reads from a file on the * classpath */ public static Hl7InputStreamMessageIterator getForClasspathResource(String theClasspath) { InputStream is = Hl7InputStreamMessageIterator.class.getResourceAsStream(theClasspath); if (is == null) { throw new IllegalArgumentException("Can't find resource: " + theClasspath); } return new Hl7InputStreamMessageIterator(is); }
/** * {@inheritDoc} */ public Message next() { String nextString = myWrapped.next(); Message retVal; try { if (myMessageType != null) { retVal = ReflectionUtil.instantiateMessage(myMessageType, getHapiContext().getModelClassFactory()); retVal.parse(nextString); } else { retVal = getHapiContext().getPipeParser().parse(nextString); } } catch (EncodingNotSupportedException e) { throw new Hl7InputStreamMessageStringIterator.ParseFailureError("Failed to parse message", e); } catch (HL7Exception e) { throw new Hl7InputStreamMessageStringIterator.ParseFailureError("Failed to parse message", e); } return retVal; }
/** * Factory method which returns an instance which reads from a file on the * classpath */ public static Hl7InputStreamMessageIterator getForClasspathResource(String theClasspath) { InputStream is = Hl7InputStreamMessageIterator.class.getResourceAsStream(theClasspath); if (is == null) { throw new IllegalArgumentException("Can't find resource: " + theClasspath); } return new Hl7InputStreamMessageIterator(is); }
/** * {@inheritDoc} */ public Message next() { String nextString = myWrapped.next(); Message retVal; try { if (myMessageType != null) { retVal = ReflectionUtil.instantiateMessage(myMessageType, getHapiContext().getModelClassFactory()); retVal.parse(nextString); } else { retVal = getHapiContext().getPipeParser().parse(nextString); } } catch (EncodingNotSupportedException e) { throw new Hl7InputStreamMessageStringIterator.ParseFailureError("Failed to parse message", e); } catch (HL7Exception e) { throw new Hl7InputStreamMessageStringIterator.ParseFailureError("Failed to parse message", e); } return retVal; }
Hl7InputStreamMessageIterator iter = new Hl7InputStreamMessageIterator(is); while (iter.hasNext()) { Message next = iter.next();