View Javadoc

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 "MinLLPWriter.java".  Description: 
10  "Title:        MinLLPWriter
11    Description:  Writes HL7 messages to an OutputStream
12    Copyright:    Copyright (c) 2001
13    Company:      University Health Network
14    @author       Damian Horton
15    @version 1.1" 
16  
17  The Initial Developer of the Original Code is University Health Network. Copyright (C) 
18  2001.  All Rights Reserved. 
19  
20  Contributor(s): ______________________________________. 
21  
22  Alternatively, the contents of this file may be used under the terms of the 
23  GNU General Public License (the  �GPL�), in which case the provisions of the GPL are 
24  applicable instead of those above.  If you wish to allow use of your version of this 
25  file only under the terms of the GPL and not to allow others to use your version 
26  of this file under the MPL, indicate your decision by deleting  the provisions above 
27  and replace  them with the notice and other provisions required by the GPL License.  
28  If you do not delete the provisions above, a recipient may use your version of 
29  this file under either the MPL or the GPL. 
30  
31  */
32  
33  package ca.uhn.hl7v2.llp;
34  
35  import java.io.*;
36  import java.nio.charset.Charset;
37  
38  /**
39   * Title:        MinLLPWriter
40   * Description:  Writes HL7 messages to an OutputStream.  The character set defaults to US-ASCII.  
41   * It can be chaged by setting the system property ca.uhn.hl7v2.llp.charset to another value that 
42   * is the name of a valid java.nio.charset.Charset.  If this property is set to "default", then 
43   * the system default is used. 
44   * 
45   * Copyright:    Copyright (c) 2001
46   * Company:      University Health Network
47   * @author       Damian Horton; mods by Bryan Tripp
48   * @version 1.1
49   */
50  
51  public class MinLLPWriter implements HL7Writer
52  {
53  	/**
54  	 * @see MinLLPReader#CHARSET_KEY
55  	 */
56      public static final String CHARSET_KEY = MinLLPReader.CHARSET_KEY;
57      
58      BufferedWriter myWriter; //reads from the input stream given in the
59                               //constructor
60  
61      private OutputStream myOutputStream;
62  
63  	private Charset charset;
64  
65      /**
66       * Creates a MinLLPWriter with no output stream specified - <code>setOutputStream</code>
67       * must be called before attempting to write any messages. 
68       */
69      public MinLLPWriter() {
70      }
71      
72      /** 
73       * Creates a MinLLPWriter, specifying the underlying output stream.
74       */
75      public MinLLPWriter(OutputStream out) throws IOException {
76          setOutputStream(out);
77      }
78      
79      /** 
80       * Creates a MinLLPWriter, specifying the underlying output stream.
81       */
82      public MinLLPWriter(OutputStream out, Charset theCharset) throws IOException {
83      	charset = theCharset;
84          setOutputStream(out);
85      }
86  
87      /** 
88       * Sets the underlying output stream to which messages are written. 
89       */
90      public synchronized void setOutputStream(OutputStream out) throws IOException  
91      {
92          myOutputStream = out;
93         	myWriter = new BufferedWriter(getWriter(out));
94      }
95  
96      /** 
97       * Sends a complete message to the underlying output stream, delimited 
98       * according to the minimal lower layer protocol.  
99       */
100     public synchronized void writeMessage(String message) throws LLPException, IOException 
101     {
102         myWriter.write('\u000b');
103         myWriter.write(message);
104         myWriter.write('\u001c' + "\r");
105         myWriter.flush();            
106     }
107 
108     /** 
109      * Sends a complete message to the underlying output stream, delimited 
110      * according to the minimal lower layer protocol, using the specified character set. 
111      */
112     public synchronized void writeMessage(String message, String charset) throws LLPException, IOException 
113     {
114         BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(myOutputStream, charset));
115         writer.write('\u000b');
116         writer.write(message);
117         writer.write('\u001c' + "\r");
118         writer.flush();
119     }
120 
121     public synchronized void close() throws java.io.IOException
122     {
123         myWriter.close();
124     }
125     
126     private OutputStreamWriter getWriter(OutputStream theStream) throws IOException {
127         if (charset != null) {
128             return new OutputStreamWriter(theStream, charset);
129         } else {
130         	String charsetString = System.getProperty(CHARSET_KEY, "US-ASCII");
131 	        if (charsetString.equals("default")) {
132 	            return new OutputStreamWriter(theStream);
133 	        } else {
134 	            return new OutputStreamWriter(theStream, charsetString);
135 	        }
136         }
137     }
138     
139 }