001package ca.uhn.hl7v2.hoh.util;
002
003import java.io.IOException;
004import java.io.InputStream;
005import java.net.SocketTimeoutException;
006
007public class SplitInputStream extends InputStream {
008
009        private int myBytesUntilSplit;
010        private InputStream myWrap;
011
012        public SplitInputStream(InputStream theWrap, int theSplitPoint) {
013                myBytesUntilSplit = theSplitPoint;
014                myWrap = theWrap;
015        }
016
017        /*
018         * (non-Javadoc)
019         * 
020         * @see java.io.InputStream#read(byte[])
021         */
022        @Override
023        public int read(byte[] theB) throws IOException {
024                throw new UnsupportedOperationException();
025        }
026
027        /*
028         * (non-Javadoc)
029         * 
030         * @see java.io.InputStream#read(byte[], int, int)
031         */
032        @Override
033        public int read(byte[] theB, int theOff, int theLen) throws IOException {
034                if (myBytesUntilSplit < 0) {
035                        // nothing
036                } else if (myBytesUntilSplit > 0 && theLen > myBytesUntilSplit) {
037                        theLen = myBytesUntilSplit;
038                } else {
039                        myBytesUntilSplit = -1;
040                        theLen = 0;
041                        throw new SocketTimeoutException();
042                }
043                int retVal = myWrap.read(theB, theOff, theLen);
044                myBytesUntilSplit -= retVal;
045                return retVal;
046        }
047
048        /*
049         * (non-Javadoc)
050         * 
051         * @see java.io.InputStream#skip(long)
052         */
053        @Override
054        public long skip(long theN) throws IOException {
055                myBytesUntilSplit -= theN;
056                return myWrap.skip(theN);
057        }
058
059        /*
060         * (non-Javadoc)
061         * 
062         * @see java.io.InputStream#available()
063         */
064        @Override
065        public int available() throws IOException {
066                return myBytesUntilSplit >= 0 ? Math.min(myBytesUntilSplit, myWrap.available()) : myWrap.available();
067        }
068
069        /*
070         * (non-Javadoc)
071         * 
072         * @see java.io.InputStream#close()
073         */
074        @Override
075        public void close() throws IOException {
076                myWrap.close();
077        }
078
079        /*
080         * (non-Javadoc)
081         * 
082         * @see java.io.InputStream#mark(int)
083         */
084        @Override
085        public synchronized void mark(int theReadlimit) {
086                throw new UnsupportedOperationException();
087        }
088
089        /*
090         * (non-Javadoc)
091         * 
092         * @see java.io.InputStream#reset()
093         */
094        @Override
095        public synchronized void reset() throws IOException {
096                throw new UnsupportedOperationException();
097        }
098
099        /*
100         * (non-Javadoc)
101         * 
102         * @see java.io.InputStream#markSupported()
103         */
104        @Override
105        public boolean markSupported() {
106                throw new UnsupportedOperationException();
107        }
108
109        @Override
110        public int read() throws IOException {
111                if (myBytesUntilSplit == 0) {
112                        myBytesUntilSplit--;
113                        throw new SocketTimeoutException();
114                }
115                int retVal = myWrap.read();
116                if (retVal != -1) {
117                        myBytesUntilSplit--;
118                }
119                return retVal;
120        }
121
122}