1 /* Copyright (C) 1998, 1999 Red Hat, Inc.
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
12 * @author Warren Levy <warrenl@cygnus.com>
13 * @date October 7, 1998.
16 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
17 * "The Java Language Specification", ISBN 0-201-63451-1
18 * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
19 * Status: Believed complete and correct
22 public class ByteArrayInputStream
extends InputStream
24 /* An array of bytes provided by the creator of the stream. */
27 /* Position of the next byte in buf to be read. */
30 /* The currently marked position in the stream. */
33 /* The index in buf one greater than the last valid character. */
36 public ByteArrayInputStream(byte[] buffer
)
38 this(buffer
, 0, buffer
.length
);
41 public ByteArrayInputStream(byte[] buffer
, int offset
, int length
)
45 count
= offset
+ length
;
46 if (count
> buf
.length
)
50 // TBD: What should we do if pos is neg. or > count? E.g. throw exc. or:
51 // if (pos < 0 || pos > count)
57 public synchronized int available()
62 public synchronized void mark(int readAheadLimit
)
64 // readAheadLimit is ignored per Java Class Lib. book, p.220.
68 public boolean markSupported()
73 public synchronized int read()
76 throw new ArrayIndexOutOfBoundsException(pos
);
79 return ((int) buf
[pos
++]) & 0xFF;
83 public synchronized int read(byte[] b
, int off
, int len
)
85 /* Don't need to check pos value, arraycopy will check it. */
86 if (off
< 0 || len
< 0 || off
+ len
> b
.length
)
87 throw new ArrayIndexOutOfBoundsException();
92 int numBytes
= Math
.min(count
- pos
, len
);
93 System
.arraycopy(buf
, pos
, b
, off
, numBytes
);
98 public synchronized void reset()
103 public synchronized long skip(long n
)
105 // Even though the var numBytes is a long, in reality it can never
106 // be larger than an int since the result of subtracting 2 positive
107 // ints will always fit in an int. Since we have to return a long
108 // anyway, numBytes might as well just be a long.
109 long numBytes
= Math
.min((long) (count
- pos
), n
< 0 ?
0L : n
);