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.FilterOutputStream;
042import java.io.IOException;
043import java.io.OutputStream;
044
045/**
046 * @author Gunter Zeilinger <gunterze@gmail.com>
047 *
048 */
049public class MLLPOutputStream extends FilterOutputStream {
050
051    private static final int SOM = 0x0b; // Start of Message 
052    private static final byte[] EOM = { 0x1c, 0x0d }; // End of Message
053
054    private boolean somWritten;
055
056    public MLLPOutputStream(OutputStream out) {
057        super(out);
058    }
059
060    @Override
061    public synchronized void write(int b) throws IOException {
062        writeStartBlock();
063        out.write(b);
064    }
065
066    @Override
067    public synchronized void write(byte b[], int off, int len)
068            throws IOException {
069        writeStartBlock();
070        out.write(b, off, len);
071    }
072
073    public void writeMessage(byte[] b) throws IOException {
074        writeMessage(b, 0, b.length);
075    }
076
077    public synchronized void writeMessage(byte b[], int off, int len)
078            throws IOException {
079        if (somWritten)
080            throw new IllegalStateException();
081
082        byte[] msg = new byte[len+3];
083        msg[0] = SOM;
084        System.arraycopy(b, off, msg, 1, len);
085        System.arraycopy(EOM, 0, msg, len + 1, 2);
086        out.write(msg);
087        out.flush();
088    }
089
090    private void writeStartBlock() throws IOException {
091        if (!somWritten) {
092            out.write(SOM);
093            somWritten = true;
094        }
095    }
096
097    public synchronized void finish() throws IOException {
098        if (!somWritten)
099            throw new IllegalStateException();
100        out.write(EOM);
101        out.flush();
102        somWritten = false;
103    }
104
105}