001    /*
002     * $Revision: 14844 $ 
003     * $Date: 2009-03-10 12:17:43 +0100 (Tue, 10 Mar 2009) $
004     *
005     * This file is part of ***  M y C o R e  ***
006     * See http://www.mycore.de/ for details.
007     *
008     * This program is free software; you can use it, redistribute it
009     * and / or modify it under the terms of the GNU General Public License
010     * (GPL) as published by the Free Software Foundation; either version 2
011     * of the License or (at your option) any later version.
012     *
013     * This program is distributed in the hope that it will be useful, but
014     * WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program, in a file called gpl.txt or license.txt.
020     * If not, write to the Free Software Foundation Inc.,
021     * 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA
022     */
023    
024    package org.mycore.datamodel.ifs2;
025    
026    import java.io.BufferedInputStream;
027    import java.io.IOException;
028    import java.io.InputStream;
029    
030    /**
031     * This class implements a special kind of BufferedInputStream that blocks
032     * invocations of the read method until the number of bytes that were requested
033     * are fully read from the underlying InputStream. In contrast,
034     * BufferedInputStream does NOT block when the requested number of bytes is not
035     * available yet.
036     * 
037     * @see BufferedInputStream
038     * 
039     * @author Frank Lützenkirchen
040     */
041    public class MCRBlockingInputStream extends BufferedInputStream {
042        /**
043         * Constructs a new MCRBlockingInputStream, using the default buffer size of
044         * BufferedInputStreams.
045         * 
046         * @param in
047         *            the InputStream to read data from
048         */
049        public MCRBlockingInputStream(InputStream in) {
050            super(in);
051        }
052    
053        /**
054         * Constructs a new MCRBlockingInputStream that uses the given buffer size.
055         * 
056         * @param in
057         *            the InputStream to read data from
058         * @param size
059         *            the size of the read buffer to be used
060         */
061        public MCRBlockingInputStream(InputStream in, int size) {
062            super(in, size);
063        }
064    
065        /**
066         * Reads 'len' bytes from the underlying input stream and writes the bytes
067         * that have been read to the buffer 'buf', starting at offset 'off' in the
068         * buffer byte array. In contrast to BufferedInputStream.read(), this method
069         * blocks the read request until 'len' bytes have been read completely or
070         * the end of the input stream is reached.
071         * 
072         * @return the number of bytes read. If the end of the input stream is not
073         *         reached yet, this is always the same as the number of bytes
074         *         requested in the 'len' parameter.
075         */
076        public int read(byte[] buf, int off, int len) throws IOException {
077            int total = 0;
078            int num = 0;
079    
080            while (len > 0) {
081                num = super.read(buf, off, len);
082    
083                if (num == 0) {
084                    continue;
085                }
086    
087                if (num == -1) {
088                    break;
089                }
090    
091                total += num;
092                off += num;
093                len -= num;
094            }
095    
096            return ((total == 0) ? num : total);
097        }
098    }