| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| MllpDecoder |
|
| 2.75;2.75 |
| 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 "MllpDecoder.java". Description: | |
| 10 | "Decodes an MLLP input stream into a string" | |
| 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.ByteArrayOutputStream; | |
| 30 | import java.io.IOException; | |
| 31 | import java.io.InputStream; | |
| 32 | import java.io.UnsupportedEncodingException; | |
| 33 | import java.net.SocketTimeoutException; | |
| 34 | import java.nio.charset.Charset; | |
| 35 | ||
| 36 | /** | |
| 37 | * MllpDecoder decodes an InputStream into a String. | |
| 38 | * | |
| 39 | * @author Christian Ohr | |
| 40 | */ | |
| 41 | class MllpDecoder { | |
| 42 | ||
| 43 | protected Charset charset; | |
| 44 | 5 | private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(MllpDecoder.class); |
| 45 | ||
| 46 | 803 | MllpDecoder(Charset charset) { |
| 47 | 803 | this.charset = charset; |
| 48 | 803 | } |
| 49 | ||
| 50 | /** | |
| 51 | * Returns a HL7 message string extracted from the MLLP stream | |
| 52 | * | |
| 53 | * @param in MLLP input stream | |
| 54 | * @return HL7 message string | |
| 55 | * @throws IOException | |
| 56 | * @throws LLPException | |
| 57 | */ | |
| 58 | public String getMessage(InputStream in) throws IOException, LLPException { | |
| 59 | 2199 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 60 | try { | |
| 61 | 2199 | MllpDecoderState state = MllpDecoderState.START; |
| 62 | 304095 | while (state != MllpDecoderState.END) { |
| 63 | 302699 | state = state.read(in, out); |
| 64 | } | |
| 65 | 214 | } catch (SocketTimeoutException e) { |
| 66 | 214 | if (out.size() == 0) { |
| 67 | 204 | ourLog.debug("Got SocketTimeoutException while waiting for data"); |
| 68 | } else { | |
| 69 | 10 | ourLog.warn("Got SocketTimeoutException while waiting for data, discarding {} bytes", out.size()); |
| 70 | } | |
| 71 | 214 | return null; |
| 72 | 1396 | } |
| 73 | 1396 | return out.size() > 0 ? toString(out.toByteArray()) : null; |
| 74 | } | |
| 75 | ||
| 76 | /** | |
| 77 | * Converts the extract byte sequence into a String. This method must respect | |
| 78 | * a Charset, which is either statically configured or may be obtained at runtime. | |
| 79 | * | |
| 80 | * @param data HL7 message as byte sequence | |
| 81 | * @return HL7 message String using correct charset | |
| 82 | */ | |
| 83 | protected String toString(byte[] data) { | |
| 84 | 1296 | return asString(data, charset); |
| 85 | } | |
| 86 | ||
| 87 | protected static String asString(byte[] data, Charset charset) { | |
| 88 | try { | |
| 89 | 1396 | return new String(data, charset.name()); // remain 1.5 compatible |
| 90 | 0 | } catch (UnsupportedEncodingException e) { |
| 91 | // never happens | |
| 92 | 0 | return null; |
| 93 | } | |
| 94 | } | |
| 95 | } |