001/**
002The contents of this file are subject to the Mozilla Public License Version 1.1 
003(the "License"); you may not use this file except in compliance with the License. 
004You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
005Software distributed under the License is distributed on an "AS IS" basis, 
006WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
007specific language governing rights and limitations under the License. 
008
009The Original Code is "MinLLPWriter.java".  Description: 
010"Title:        MinLLPWriter
011  Description:  Writes HL7 messages to an OutputStream
012  Copyright:    Copyright (c) 2001
013  Company:      University Health Network
014  @author       Damian Horton
015  @version 1.1" 
016
017The Initial Developer of the Original Code is University Health Network. Copyright (C) 
0182001.  All Rights Reserved. 
019
020Contributor(s): ______________________________________. 
021
022Alternatively, the contents of this file may be used under the terms of the 
023GNU General Public License (the  ?GPL?), in which case the provisions of the GPL are 
024applicable instead of those above.  If you wish to allow use of your version of this 
025file only under the terms of the GPL and not to allow others to use your version 
026of this file under the MPL, indicate your decision by deleting  the provisions above 
027and replace  them with the notice and other provisions required by the GPL License.  
028If you do not delete the provisions above, a recipient may use your version of 
029this file under either the MPL or the GPL. 
030
031*/
032
033package ca.uhn.hl7v2.llp;
034
035import java.io.*;
036import java.nio.charset.Charset;
037
038/**
039 * Title:        MinLLPWriter
040 * Description:  Writes HL7 messages to an OutputStream.  The character set defaults to US-ASCII.  
041 * It can be chaged by setting the system property ca.uhn.hl7v2.llp.charset to another value that 
042 * is the name of a valid java.nio.charset.Charset.  If this property is set to "default", then 
043 * the system default is used. 
044 * 
045 * Copyright:    Copyright (c) 2001
046 * Company:      University Health Network
047 * @author       Damian Horton; mods by Bryan Tripp
048 * @version 1.1
049 */
050
051public class MinLLPWriter implements HL7Writer
052{
053        /**
054         * @see MinLLPReader#CHARSET_KEY
055         */
056    public static final String CHARSET_KEY = MinLLPReader.CHARSET_KEY;
057    
058    BufferedWriter myWriter; //reads from the input stream given in the
059                             //constructor
060
061    private OutputStream myOutputStream;
062
063        private Charset charset;
064
065    /**
066     * Creates a MinLLPWriter with no output stream specified - <code>setOutputStream</code>
067     * must be called before attempting to write any messages. 
068     */
069    public MinLLPWriter() {
070    }
071    
072    /** 
073     * Creates a MinLLPWriter, specifying the underlying output stream.
074     */
075    public MinLLPWriter(OutputStream out) throws IOException {
076        setOutputStream(out);
077    }
078    
079    /** 
080     * Creates a MinLLPWriter, specifying the underlying output stream.
081     */
082    public MinLLPWriter(OutputStream out, Charset theCharset) throws IOException {
083        charset = theCharset;
084        setOutputStream(out);
085    }
086
087    /** 
088     * Sets the underlying output stream to which messages are written. 
089     */
090    public synchronized void setOutputStream(OutputStream out) throws IOException  
091    {
092        myOutputStream = out;
093        myWriter = new BufferedWriter(getWriter(out));
094    }
095
096    /** 
097     * Sends a complete message to the underlying output stream, delimited 
098     * according to the minimal lower layer protocol.  
099     */
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}