1 |
/* |
/* |
2 |
* $Id$ |
* BlockInputStream.java |
3 |
* Copyright (C) 2003 The Free Software Foundation |
* Copyright (C) 2003 The Free Software Foundation |
4 |
* |
* |
5 |
* This file is part of GNU inetlib, a library. |
* This file is part of GNU inetlib, a library. |
46 |
* A DTP input stream that implements the FTP block transfer mode. |
* A DTP input stream that implements the FTP block transfer mode. |
47 |
* |
* |
48 |
* @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> |
* @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> |
|
* @version $Revision$ $Date$ |
|
49 |
*/ |
*/ |
50 |
class BlockInputStream extends DTPInputStream |
class BlockInputStream |
51 |
|
extends DTPInputStream |
52 |
{ |
{ |
53 |
|
|
54 |
static final int EOF = 64; |
static final int EOF = 64; |
57 |
int max = -1; |
int max = -1; |
58 |
int count = -1; |
int count = -1; |
59 |
|
|
60 |
BlockInputStream (DTP dtp, InputStream in) |
BlockInputStream(DTP dtp, InputStream in) |
61 |
{ |
{ |
62 |
super (dtp, in); |
super(dtp, in); |
63 |
} |
} |
64 |
|
|
65 |
public int read () throws IOException |
public int read() |
66 |
{ |
throws IOException |
67 |
if (transferComplete) |
{ |
68 |
{ |
if (transferComplete) |
69 |
return -1; |
{ |
70 |
} |
return -1; |
71 |
if (count == -1) |
} |
72 |
{ |
if (count == -1) |
73 |
readHeader (); |
{ |
74 |
} |
readHeader(); |
75 |
if (max < 1) |
} |
76 |
{ |
if (max < 1) |
77 |
close (); |
{ |
78 |
return -1; |
close(); |
79 |
} |
return -1; |
80 |
int c = in.read (); |
} |
81 |
if (c == -1) |
int c = in.read(); |
82 |
{ |
if (c == -1) |
83 |
dtp.transferComplete (); |
{ |
84 |
} |
dtp.transferComplete(); |
85 |
count++; |
} |
86 |
if (count >= max) |
count++; |
87 |
{ |
if (count >= max) |
88 |
count = -1; |
{ |
89 |
if (descriptor == EOF) |
count = -1; |
90 |
{ |
if (descriptor == EOF) |
91 |
close (); |
{ |
92 |
} |
close(); |
93 |
} |
} |
94 |
return c; |
} |
95 |
} |
return c; |
96 |
|
} |
97 |
public int read (byte[] buf) throws IOException |
|
98 |
{ |
public int read(byte[] buf) |
99 |
return read (buf, 0, buf.length); |
throws IOException |
100 |
} |
{ |
101 |
|
return read(buf, 0, buf.length); |
102 |
public int read (byte[] buf, int off, int len) throws IOException |
} |
103 |
{ |
|
104 |
if (transferComplete) |
public int read(byte[] buf, int off, int len) |
105 |
{ |
throws IOException |
106 |
return -1; |
{ |
107 |
} |
if (transferComplete) |
108 |
if (count == -1) |
{ |
109 |
{ |
return -1; |
110 |
readHeader (); |
} |
111 |
} |
if (count == -1) |
112 |
if (max < 1) |
{ |
113 |
{ |
readHeader(); |
114 |
close (); |
} |
115 |
return -1; |
if (max < 1) |
116 |
} |
{ |
117 |
int l = in.read (buf, off, len); |
close(); |
118 |
if (l == -1) |
return -1; |
119 |
{ |
} |
120 |
dtp.transferComplete (); |
int l = in.read(buf, off, len); |
121 |
} |
if (l == -1) |
122 |
count += l; |
{ |
123 |
if (count >= max) |
dtp.transferComplete(); |
124 |
{ |
} |
125 |
count = -1; |
count += l; |
126 |
if (descriptor == EOF) |
if (count >= max) |
127 |
{ |
{ |
128 |
close (); |
count = -1; |
129 |
} |
if (descriptor == EOF) |
130 |
} |
{ |
131 |
return l; |
close(); |
132 |
} |
} |
133 |
|
} |
134 |
|
return l; |
135 |
|
} |
136 |
|
|
137 |
/** |
/** |
138 |
* Reads the block header. |
* Reads the block header. |
139 |
*/ |
*/ |
140 |
void readHeader () throws IOException |
void readHeader() |
141 |
{ |
throws IOException |
142 |
descriptor = in.read (); |
{ |
143 |
int max_hi = in.read (); |
descriptor = in.read(); |
144 |
int max_lo = in.read (); |
int max_hi = in.read(); |
145 |
max = (max_hi << 8) | max_lo; |
int max_lo = in.read(); |
146 |
count = 0; |
max = (max_hi << 8) | max_lo; |
147 |
} |
count = 0; |
148 |
|
} |
149 |
|
|
150 |
} |
} |
151 |
|
|