001/* ***** BEGIN LICENSE BLOCK *****
002 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003 *
004 * The contents of this file are subject to the Mozilla Public License Version
005 * 1.1 (the "License"); you may not use this file except in compliance with
006 * the License. You may obtain a copy of the License at
007 * http://www.mozilla.org/MPL/
008 *
009 * Software distributed under the License is distributed on an "AS IS" basis,
010 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
011 * for the specific language governing rights and limitations under the
012 * License.
013 *
014 * The Original Code is part of dcm4che, an implementation of DICOM(TM) in
015 * Java(TM), hosted at https://github.com/gunterze/dcm4che.
016 *
017 * The Initial Developer of the Original Code is
018 * Agfa Healthcare.
019 * Portions created by the Initial Developer are Copyright (C) 2012
020 * the Initial Developer. All Rights Reserved.
021 *
022 * Contributor(s):
023 * See @authors listed below
024 *
025 * Alternatively, the contents of this file may be used under the terms of
026 * either the GNU General Public License Version 2 or later (the "GPL"), or
027 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
028 * in which case the provisions of the GPL or the LGPL are applicable instead
029 * of those above. If you wish to allow use of your version of this file only
030 * under the terms of either the GPL or the LGPL, and not to allow others to
031 * use your version of this file under the terms of the MPL, indicate your
032 * decision by deleting the provisions above and replace them with the notice
033 * and other provisions required by the GPL or the LGPL. If you do not delete
034 * the provisions above, a recipient may use your version of this file under
035 * the terms of any one of the MPL, the GPL or the LGPL.
036 *
037 * ***** END LICENSE BLOCK ***** */
038
039package org.dcm4che3.hl7;
040
041import java.io.BufferedInputStream;
042import java.io.ByteArrayOutputStream;
043import java.io.EOFException;
044import java.io.IOException;
045import java.io.InputStream;
046import java.io.OutputStream;
047
048/**
049 * @author Gunter Zeilinger <gunterze@gmail.com>
050 *
051 */
052public class MLLPInputStream extends BufferedInputStream {
053
054    private static final int SOM = 0x0b; // Start of Message
055    private static final int EOM1 = 0x1c; // End of Message Byte 1
056    private static final int EOM2 = 0x0d; // End of Message Byte 2
057
058    private boolean eom = true;
059    private ByteArrayOutputStream readBuffer = new ByteArrayOutputStream();
060
061    public MLLPInputStream(InputStream in) {
062        super(in);
063    }
064
065    public MLLPInputStream(InputStream in, int size) {
066        super(in, size);
067    }
068
069    public synchronized boolean hasMoreInput() throws IOException {
070        if (!eom)
071            throw new IllegalStateException();
072
073        int b = super.read();
074        if (b == -1)
075            return false;
076
077        if (b != SOM)
078            throw new IOException("Missing Start Block character");
079
080        eom = false;
081        return true;
082    }
083
084    @Override
085    public synchronized int read() throws IOException {
086        if (eom)
087            return -1;
088
089        int b = super.read();
090        if (b == -1)
091            throw new EOFException();
092        
093        if (b != EOM1)
094            return b;
095
096        eom();
097        return -1;
098    }
099
100    @Override
101    public synchronized int read(byte[] b, int off, int len) throws IOException {
102        if (b == null)
103            throw new NullPointerException();
104
105        if (off < 0 || len < 0 || len > b.length - off)
106            throw new IndexOutOfBoundsException();
107
108        if (eom)
109            return -1;
110
111        if (len == 0)
112            return 0;
113
114        if (read() == -1)
115            return -1;
116
117        int rlen = Math.min(count - pos, len-1);
118        int remaining = remaining(pos + rlen);
119        if (remaining == -1) {
120            System.arraycopy(buf, pos-1, b, off, rlen+1);
121            pos += rlen;
122            return rlen+1;
123        }
124
125        System.arraycopy(buf, pos-1, b, off, remaining+1);
126        pos += remaining+1;
127        eom();
128        return remaining + 1;
129    }
130
131    public synchronized int copyTo(OutputStream out) throws IOException {
132        if (eom)
133            throw new IllegalStateException();
134
135        int totlen = 0;
136        int remaining;
137        int leftover = 0;
138        while ((remaining = remaining(count)) == -1) {
139            int avail = count - pos;
140            out.write(buf, pos - leftover, avail + leftover);
141            totlen += avail + leftover;
142            pos = count;
143            if (read() == -1)
144                return totlen;
145            leftover = 1;
146        }
147        out.write(buf, pos - leftover, remaining + leftover);
148        totlen += remaining + leftover;
149        pos += remaining + 1;
150        eom();
151        return totlen;
152    }
153
154    public synchronized byte[] readMessage() throws IOException {
155        if (!hasMoreInput())
156            return null;
157
158        readBuffer.reset();
159        copyTo(readBuffer);
160        return readBuffer.toByteArray();
161    }
162
163    private void eom() throws IOException {
164        int b = super.read();
165        if (b != EOM2)
166            throw new IOException("1CH followed by "
167                        + Integer.toHexString(b & 0xff) + "H instead by 0DH");
168        eom = true;
169    }
170
171    private int remaining(int count) {
172        for (int i = pos; i < count; i++)
173            if (buf[i] == EOM1)
174                return i - pos;
175
176        return -1;
177    }
178}