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 "MllpEncoder.java".  Description:
10   "Encodes a message string into an MLLP output stream"
11  
12   The Initial Developer of the Original Code is University Health Network. Copyright (C)
13   2013.  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  package ca.uhn.hl7v2.llp;
28  
29  import java.io.*;
30  import java.nio.charset.Charset;
31  
32  /**
33   * MllpEncoder encodes a String into an OutputStream
34   *
35   * @author Christian Ohr
36   */
37  class MllpEncoder {
38  
39      protected final Charset charset;
40      protected final boolean omitBOM;
41  
42      MllpEncoder(Charset charset, boolean omitBOM) {
43          this.charset = charset;
44          this.omitBOM = omitBOM;
45      }
46  
47      /**
48       * Returns a HL7 message string extracted from the MLLP stream
49       */
50      public void putMessage(String message, OutputStream out) throws IOException {
51          // Try to enforce sending the message in one frame. Multiple calls to write()
52          // may cause multiple frames being sent.
53          byte[] bytes = toByteArray(message);
54          byte[] outBytes = new byte[bytes.length + 3];
55          outBytes[0] = MllpConstants.START_BYTE;
56          System.arraycopy(bytes, 0, outBytes, 1, bytes.length);
57          outBytes[outBytes.length - 2] = MllpConstants.END_BYTE1;
58          outBytes[outBytes.length - 1] = MllpConstants.END_BYTE2;
59  
60          out.write(outBytes);
61          out.flush();
62      }
63  
64      /**
65       * Converts the extract byte sequence into a String. This method must respect
66       * a Charset, which is either statically configured or may be obtained at runtime.
67       *
68       * @param message HL7 message
69       * @return HL7 message as byte array
70       */
71      protected byte[] toByteArray(String message) {
72          return asByteArray(message, charset, omitBOM);
73      }
74  
75      protected static byte[] asByteArray(String message, Charset charset, boolean omitBOM) {
76          try {
77              byte[] b = message.getBytes(charset.name());
78              return omitBOM ? CharSetUtil.withoutBOM(b) : b;
79          } catch (UnsupportedEncodingException e) {
80              // never happens
81              return null;
82          }
83      }
84  
85  }