1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
|
//start of LimitedInputStream.java
//TEXT_STYLE:CODE=Shift_JIS(Japanese):RET_CODE=CRLF
/**
* LimitedInputStream.java
*
* Copyright (C) 2001-2002 Michel Ishizuka All rights reserved.
*
* ȉ̏ɓӂȂ\[XƃoCi`̍ĔzzƎgp
* ύX̗Lɂ炸B
*
* PD\[XR[h̍ĔzzɂĒ쌠\ ̏̃Xg
* щL̐ێȂĂ͂ȂȂB
*
* QDoCi`̍ĔzzɂĒ쌠\ ̏̃Xg
* щL̐gp ̑̔zz
* ܂ގɋLqȂȂȂB
*
* ̃\tgEFA͐Β˔ڂɂĖۏŒA̖
* IBłƂۏAilLƂۏɂƂǂ܂炸A
* Ȃ閾IшÎIȕۏȂB
* Β˔ڂ ̃\tgEFA̎gpɂ钼ړIAԐړIA
* IAȁAT^IȁA邢͕KRIȑQ(gpɂf[^
* AƖ̒f〈܂Ăv̈⎸Ai
* T[rX̓l邪AĂꂾɌ肳Ȃ
* Q)ɑāAȂ鎖Ԃ̌ƂȂƂĂA_̐
* C△ߎӔC܂ ȂӔC낤ƂAƂꂪs
* ŝׂ߂łƂĂA܂͂̂悤ȑQ̉\
* ĂƂĂ̐ӔCȂ̂ƂB
*/
package jp.gr.java_conf.dangan.io;
//import classes and interfaces
import java.io.InputStream;
import java.lang.Math;
//import exceptions
import java.io.IOException;
import java.lang.IllegalArgumentException;
/**
* ǂݍ݉\ȃf[^ʂꂽ̓Xg[B<br>
*
* <pre>
* -- revision history --
* $Log: LimitedInputStream.java,v $
* Revision 1.1.2.1 2003/07/20 17:03:37 dangan
* [maintenance]
* ŐV LimitedInputStream \[X荞ށB
*
* Revision 1.1 2002/12/05 00:00:00 dangan
* [maintenance]
* \[X
*
* Revision 1.0 2002/07/24 00:00:00 dangan
* add to version control
* [change]
* EndOfStream ɒB read( new byte[0] )
* read( byte[] buf, int off, 0 ) ̖߂l
* InputStream Ɠ 0 ɂȂ悤ɂ
* [maintenance]
* ^up~
* CZX̏C
*
* </pre>
*
* @author $Author: dangan $
* @version $Revision: 1.1.2.1 $
*/
public class LimitedInputStream extends InputStream{
//------------------------------------------------------------------
// instance field
//------------------------------------------------------------------
// source
//------------------------------------------------------------------
// private InputStream in
//------------------------------------------------------------------
/**
* ڑꂽ̓Xg[
*/
private InputStream in;
//------------------------------------------------------------------
// instance field
//------------------------------------------------------------------
// current position
//------------------------------------------------------------------
// private long position
// private final long limit
// private long markPosition
//------------------------------------------------------------------
/**
* ݓǂݍ݈ʒu
*/
private long position;
/**
* ǂݍE
*/
private final long limit;
/**
* }[Nʒu
*/
private long markPosition;
//------------------------------------------------------------------
// constructor
//------------------------------------------------------------------
// public LimitedInputStream( InputStream in, long limit )
//------------------------------------------------------------------
/**
* in ̓ǂݍ݉\ȃf[^ʂ𐧌
* ̓Xg[\zB<br>
*
* @param in ̓Xg[
* @param limit ǂݍ݉\oCg
*
* @exception IllegalArgumentException
* limit łꍇ
*/
public LimitedInputStream( InputStream in, long limit ){
if( in != null && 0 <= limit ){
this.in = in;
this.position = 0;
this.limit = limit;
this.markPosition = -1;
}else if( in == null ){
throw new NullPointerException( "in" );
}else{
throw new IllegalArgumentException( "limit must be 0 or more." );
}
}
//------------------------------------------------------------------
// method of java.io.InputStream
//------------------------------------------------------------------
// read
//------------------------------------------------------------------
// public int read()
// public int read( byte[] buffer )
// public int read( byte[] buffer, int index, int length )
// public long skip( long length )
//------------------------------------------------------------------
/**
* ڑꂽ̓Xg[ 1oCg̃f[^ǂݍށB
*
* @return ǂݍ܂ꂽ 1oCg̃f[^<br>
* EndOfStream ɒBĂA
* ɒBꍇ -1 ԂB<br>
*
* @exception IOException o̓G[ꍇ
*/
public int read() throws IOException {
if( this.position < this.limit ){
int ret = this.in.read(); //throws IOException
if( 0 <= ret ){
this.position++;
}
return ret;
}else{
return -1;
}
}
/**
* ڑꂽ̓Xg[ buffer 悤
* f[^ǂݍށB<br>
* f[^͕K buffer Ƃ͌ȂƂɒӁB<br>
*
* @param buffer ǂݍf[^i[邽߂̃oCgz<br>
*
* @return buffer ɓǂݍf[^ʂoCgŕԂB<br>
* EndOfStream ɒBĂA
* ɒBꍇ -1 ԂB<br>
*
* @exception IOException o̓G[ꍇ
*/
public int read( byte[] buffer ) throws IOException {
if( 0 < buffer.length ){
int ret;
if( buffer.length < this.limit - this.position ){
ret = this.in.read( buffer ); //throws IOException
}else if( this.position < this.limit ){
ret = this.in.read( buffer, 0, (int)( this.limit - this.position ) );//throws IOException
}else{
return -1;
}
if( 0 < ret ){
this.position += ret;
}
return ret;
}else{
return 0;
}
}
/**
* ڑꂽ̓Xg[ oCgz buffer
* index Ŏw肳ꂽʒu length oCg̃f[^
* ǂݍށB<br>
* f[^͕K length oCgǂݍ܂Ƃ͌
* ȂƂɒӁB<br>
*
* @param buffer ǂݍ܂ꂽf[^i[邽߂̃oCgz
* @param index buffer̃f[^ǂݍ݊Jnʒu
* @param length bufferɓǂݍރf[^
*
* @return buffer ɓǂݍf[^ʂoCgŕԂB<br>
* EndOfStream ɒBĂA
* ɒBꍇ -1 ԂB<br>
*
* @exception IOException o̓G[ꍇ
*/
public int read( byte[] buffer, int index, int length )
throws IOException {
if( 0 < length ){
if( this.limit <= this.position ){
return -1;
}else if( this.limit - this.position < length ){
length = (int)( this.limit - this.position );
}
int ret = this.in.read( buffer, index, length ); //throws IOException
if( 0 < ret ){
this.position += ret;
}
return ret;
}else{
return 0;
}
}
/**
* ڑꂽ̓Xg[̃f[^ length oCgǂݔB<br>
*
* @param length ǂݔoCgB<br>
*
* @return ۂɓǂݔꂽoCgB<br>
*
* @exception IOException o̓G[ꍇ
*/
public long skip( long length ) throws IOException {
if( 0 < length ){
if( this.limit <= this.position ){
return 0;
}else if( this.limit - this.position < length ){
length = this.limit - this.position;
}
length = this.in.skip( length ); //throws IOException
if( 0 < length ){
this.position += length;
}
return length;
}else{
return 0;
}
}
//------------------------------------------------------------------
// method of java.io.InputStream
//------------------------------------------------------------------
// mark/reset
//------------------------------------------------------------------
// public void mark( int readLimit )
// public void reset()
// public boolean markSupprted()
//------------------------------------------------------------------
/**
* ڑꂽ̓Xg[݈̌ʒuɃ}[Nݒ肵A
* reset() \bhŃ}[N_ ǂݍ݈ʒu
* ߂悤ɂB<br>
*
* @param readLimit }[Nʒuɖ߂ẼoCgB
* ̃oCgăf[^ǂ
* ꍇ reset()łȂȂ
* \B<br>
*/
public void mark( int readLimit ){
this.in.mark( readLimit );
this.markPosition = this.position;
}
/**
* ڑꂽ̓Xg[̓ǂݍ݈ʒuŌ
* mark() \bhĂяoꂽƂ̈ʒuɐݒ肷B<br>
*
* @exception IOException <br>
* <ol>
* <li> LimitedInputStream mark ȂĂȂꍇB<br>
* <li> ڑꂽ̓Xg[ markSupported()
* false ԂꍇB<br>
* <li> ڑꂽ̓Xg[
* o̓G[ꍇB<br>
* </ol>
* ̉ꂩB
*/
public void reset() throws IOException {
if( !this.in.markSupported() ){
throw new IOException( "not support mark()/reset()." );
}else if( this.markPosition < 0 ){ //RXgN^ MarkPosition -1 ɐݒ肳̂𗘗pB
throw new IOException( "not marked." );
}else{
this.in.reset(); //throws IOException
this.position = this.markPosition;
}
}
/**
* ڑꂽ̓Xg[ mark() reset()
* T|[g邩B<br>
*
* @return Xg[ mark() reset()
* T|[gꍇ trueB<br>
* T|[gȂꍇ falseB<br>
*/
public boolean markSupported(){
return this.in.markSupported();
}
//------------------------------------------------------------------
// method of java.io.InputStream
//------------------------------------------------------------------
// other
//------------------------------------------------------------------
// public int available()
// public void close()
//------------------------------------------------------------------
/**
* ڑꂽ̓Xg[ubNȂ
* ǂݍނƂ̂łoCgB<br>
*
* @return ubNȂœǂݏooCgB<br>
*
* @exception IOException o̓G[ꍇ
*/
public int available() throws IOException {
return (int)Math.min( (long)this.in.available(), //throws IOException
this.limit - this.position );
}
/**
* ̓̓Xg[AgpĂ
* SẴ\[XJB<br>
*
* @exception IOException o̓G[ꍇ
*/
public void close() throws IOException {
this.in.close(); //throws IOException
this.in = null;
}
}
//end of LimitedInputStream.java
|