001 /*
002 *
003 * $Revision: 13085 $ $Date: 2008-02-06 18:27:24 +0100 (Mi, 06 Feb 2008) $
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.ifs;
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 * @version $Revision: 13085 $ $Date: 2008-02-06 18:27:24 +0100 (Mi, 06 Feb 2008) $
041 */
042 public class MCRBlockingInputStream extends BufferedInputStream {
043 /**
044 * Constructs a new MCRBlockingInputStream, using the default buffer size of
045 * BufferedInputStreams.
046 *
047 * @param in
048 * the InputStream to read data from
049 */
050 public MCRBlockingInputStream(InputStream in) {
051 super(in);
052 }
053
054 /**
055 * Constructs a new MCRBlockingInputStream that uses the given buffer size.
056 *
057 * @param in
058 * the InputStream to read data from
059 * @param size
060 * the size of the read buffer to be used
061 */
062 public MCRBlockingInputStream(InputStream in, int size) {
063 super(in, size);
064 }
065
066 /**
067 * Reads 'len' bytes from the underlying input stream and writes the bytes
068 * that have been read to the buffer 'buf', starting at offset 'off' in the
069 * buffer byte array. In contrast to BufferedInputStream.read(), this method
070 * blocks the read request until 'len' bytes have been read completely or
071 * the end of the input stream is reached.
072 *
073 * @return the number of bytes read. If the end of the input stream is not
074 * reached yet, this is always the same as the number of bytes
075 * requested in the 'len' parameter.
076 */
077 public int read(byte[] buf, int off, int len) throws IOException {
078 int total = 0;
079 int num = 0;
080
081 while (len > 0) {
082 num = super.read(buf, off, len);
083
084 if (num == 0) {
085 continue;
086 }
087
088 if (num == -1) {
089 break;
090 }
091
092 total += num;
093 off += num;
094 len -= num;
095 }
096
097 return ((total == 0) ? num : total);
098 }
099 }