| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| HL7EncoderWriter |
|
| 2.0;2 |
| 1 | /** | |
| 2 | The contents of this file are subject to the Mozilla Public License Version 1.1 | |
| 3 | (the "License"); you may not use this file except in compliance with the License. | |
| 4 | You may obtain a copy of the License at http://www.mozilla.org/MPL/ | |
| 5 | Software distributed under the License is distributed on an "AS IS" basis, | |
| 6 | WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the | |
| 7 | specific language governing rights and limitations under the License. | |
| 8 | ||
| 9 | The Original Code is "HL7EncoderWriter.java". Description: | |
| 10 | "" | |
| 11 | ||
| 12 | The Initial Developer of the Original Code is University Health Network. Copyright (C) | |
| 13 | 2001. All Rights Reserved. | |
| 14 | ||
| 15 | Contributor(s): ______________________________________. | |
| 16 | ||
| 17 | Alternatively, the contents of this file may be used under the terms of the | |
| 18 | GNU General Public License (the �GPL�), in which case the provisions of the GPL are | |
| 19 | applicable instead of those above. If you wish to allow use of your version of this | |
| 20 | file only under the terms of the GPL and not to allow others to use your version | |
| 21 | of this file under the MPL, indicate your decision by deleting the provisions above | |
| 22 | and replace them with the notice and other provisions required by the GPL License. | |
| 23 | If you do not delete the provisions above, a recipient may use your version of | |
| 24 | this file under either the MPL or the GPL. | |
| 25 | ||
| 26 | */ | |
| 27 | ||
| 28 | package ca.uhn.hl7v2.llp; | |
| 29 | ||
| 30 | import java.io.BufferedOutputStream; | |
| 31 | import java.io.IOException; | |
| 32 | import java.io.OutputStream; | |
| 33 | import java.io.OutputStreamWriter; | |
| 34 | import java.nio.charset.Charset; | |
| 35 | ||
| 36 | import static ca.uhn.hl7v2.llp.MllpConstants.CHARSET_KEY; | |
| 37 | ||
| 38 | /** | |
| 39 | * Writes HL7 messages to an OutputStream. The character set defaults to US-ASCII. | |
| 40 | * It can be changed by setting the system property ca.uhn.hl7v2.llp.charset to another value that | |
| 41 | * is the name of a valid java.nio.charset.Charset. If this property is set to "default", then | |
| 42 | * the system default is used. | |
| 43 | * | |
| 44 | * <p/> | |
| 45 | * | |
| 46 | * @author Damian Horton; mods by Bryan Tripp | |
| 47 | * @author Christian Ohr | |
| 48 | */ | |
| 49 | ||
| 50 | abstract class HL7EncoderWriter<T extends MllpEncoder> implements HL7Writer { | |
| 51 | ||
| 52 | private OutputStream out; | |
| 53 | private T encoder; | |
| 54 | private Charset charset; | |
| 55 | protected boolean omitBOM; | |
| 56 | ||
| 57 | /** | |
| 58 | * Creates a HL7EncoderWriter with no output stream specified - <code>setOutputStream</code> | |
| 59 | * must be called before attempting to write any messages. | |
| 60 | */ | |
| 61 | 60 | public HL7EncoderWriter() { |
| 62 | 60 | encoder = initEncoder(); |
| 63 | 60 | } |
| 64 | ||
| 65 | /** | |
| 66 | * Creates a HL7EncoderWriter, specifying the underlying output stream. | |
| 67 | */ | |
| 68 | 155 | public HL7EncoderWriter(OutputStream out) throws IOException { |
| 69 | 155 | setOutputStream(out); |
| 70 | 150 | encoder = initEncoder(); |
| 71 | 150 | } |
| 72 | ||
| 73 | /** | |
| 74 | * Creates a HL7EncoderWriter, specifying the underlying output stream. | |
| 75 | */ | |
| 76 | 563 | public HL7EncoderWriter(OutputStream out, Charset charset, boolean omitBOM) throws IOException { |
| 77 | 563 | this.out = out; |
| 78 | 563 | this.charset = charset; |
| 79 | 563 | this.omitBOM = omitBOM; |
| 80 | 563 | encoder = initEncoder(); |
| 81 | 563 | } |
| 82 | ||
| 83 | protected abstract T initEncoder(); | |
| 84 | ||
| 85 | /** | |
| 86 | * Sets the underlying output stream to which messages are written. | |
| 87 | */ | |
| 88 | public synchronized void setOutputStream(OutputStream out) throws IOException { | |
| 89 | 165 | if (out == null) throw new NullPointerException("OutputStream must not be null"); |
| 90 | 155 | this.out = new BufferedOutputStream(out); |
| 91 | 155 | } |
| 92 | ||
| 93 | /** | |
| 94 | * Sends a complete message to the underlying output stream, delimited | |
| 95 | * according to the minimal lower layer protocol. | |
| 96 | */ | |
| 97 | public synchronized void writeMessage(String message) throws LLPException, IOException { | |
| 98 | 1451 | if (message == null) { |
| 99 | 5 | throw new NullPointerException("Message must not be null"); |
| 100 | } | |
| 101 | 1446 | encoder.putMessage(message, out); |
| 102 | 1441 | } |
| 103 | ||
| 104 | /** | |
| 105 | * Sends a complete message to the underlying output stream, delimited | |
| 106 | * according to the minimal lower layer protocol, using the specified character set. | |
| 107 | */ | |
| 108 | public synchronized void writeMessage(String message, String charset) throws LLPException, IOException { | |
| 109 | 10 | if (message == null) { |
| 110 | 0 | throw new NullPointerException("Message may not be null"); |
| 111 | } | |
| 112 | 10 | OutputStreamWriter writer = new OutputStreamWriter(out, charset); |
| 113 | 10 | writer.write(MllpConstants.START_BYTE); |
| 114 | 10 | writer.write(message); |
| 115 | 10 | writer.write(MllpConstants.END_BYTE1); |
| 116 | 10 | writer.write(MllpConstants.END_BYTE2); |
| 117 | 10 | writer.flush(); |
| 118 | 10 | } |
| 119 | ||
| 120 | public synchronized void close() throws IOException { | |
| 121 | 35 | if (out != null) out.close(); |
| 122 | 35 | } |
| 123 | ||
| 124 | protected Charset getCharset() { | |
| 125 | 773 | if (charset == null) { |
| 126 | 763 | String charsetString = System.getProperty(CHARSET_KEY, "US-ASCII"); |
| 127 | 763 | if (charsetString.equals("default")) { |
| 128 | 0 | charset = Charset.defaultCharset(); |
| 129 | } else { | |
| 130 | 763 | charset = Charset.forName(charsetString); |
| 131 | } | |
| 132 | } | |
| 133 | 773 | return charset; |
| 134 | } | |
| 135 | } |