1 /* BlockInputStream.java -- 2 Copyright (C) 2003, 2004 Free Software Foundation, Inc. 3 4 This file is part of GNU Classpath. 5 6 GNU Classpath is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 2, or (at your option) 9 any later version. 10 11 GNU Classpath is distributed in the hope that it will be useful, but 12 WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with GNU Classpath; see the file COPYING. If not, write to the 18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19 02110-1301 USA. 20 21 Linking this library statically or dynamically with other modules is 22 making a combined work based on this library. Thus, the terms and 23 conditions of the GNU General Public License cover the whole 24 combination. 25 26 As a special exception, the copyright holders of this library give you 27 permission to link this library with independent modules to produce an 28 executable, regardless of the license terms of these independent 29 modules, and to copy and distribute the resulting executable under 30 terms of your choice, provided that you also meet, for each linked 31 independent module, the terms and conditions of the license of that 32 module. An independent module is a module which is not derived from 33 or based on this library. If you modify this library, you may extend 34 this exception to your version of the library, but you are not 35 obligated to do so. If you do not wish to do so, delete this 36 exception statement from your version. */ 37 38 39 package gnu.java.net.protocol.ftp; 40 41 import java.io.IOException; 42 import java.io.InputStream; 43 44 /** 45 * A DTP input stream that implements the FTP block transfer mode. 46 * 47 * @author Chris Burdess (dog@gnu.org) 48 */ 49 class BlockInputStream 50 extends DTPInputStream 51 { 52 53 static final int EOF = 64; 54 55 int descriptor; 56 int max = -1; 57 int count = -1; 58 BlockInputStream(DTP dtp, InputStream in)59 BlockInputStream(DTP dtp, InputStream in) 60 { 61 super(dtp, in); 62 } 63 read()64 public int read() 65 throws IOException 66 { 67 if (transferComplete) 68 { 69 return -1; 70 } 71 if (count == -1) 72 { 73 readHeader(); 74 } 75 if (max < 1) 76 { 77 close(); 78 return -1; 79 } 80 int c = in.read(); 81 if (c == -1) 82 { 83 dtp.transferComplete(); 84 } 85 count++; 86 if (count >= max) 87 { 88 count = -1; 89 if (descriptor == EOF) 90 { 91 close(); 92 } 93 } 94 return c; 95 } 96 read(byte[] buf)97 public int read(byte[] buf) 98 throws IOException 99 { 100 return read(buf, 0, buf.length); 101 } 102 read(byte[] buf, int off, int len)103 public int read(byte[] buf, int off, int len) 104 throws IOException 105 { 106 if (transferComplete) 107 { 108 return -1; 109 } 110 if (count == -1) 111 { 112 readHeader(); 113 } 114 if (max < 1) 115 { 116 close(); 117 return -1; 118 } 119 int l = in.read(buf, off, len); 120 if (l == -1) 121 { 122 dtp.transferComplete(); 123 } 124 count += l; 125 if (count >= max) 126 { 127 count = -1; 128 if (descriptor == EOF) 129 { 130 close(); 131 } 132 } 133 return l; 134 } 135 136 /** 137 * Reads the block header. 138 */ readHeader()139 void readHeader() 140 throws IOException 141 { 142 descriptor = in.read(); 143 int max_hi = in.read(); 144 int max_lo = in.read(); 145 max = (max_hi << 8) | max_lo; 146 count = 0; 147 } 148 149 } 150