* All files: Updated copyright to reflect Cygnus purchase.
[official-gcc.git] / libjava / java / io / ByteArrayInputStream.java
blob366e1c2c38e9bf3e2092bd5a35a48a0608451e9b
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
7 details. */
9 package java.io;
11 /**
12 * @author Warren Levy <warrenl@cygnus.com>
13 * @date October 7, 1998.
14 */
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. */
25 protected byte[] buf;
27 /* Position of the next byte in buf to be read. */
28 protected int pos;
30 /* The currently marked position in the stream. */
31 protected int mark;
33 /* The index in buf one greater than the last valid character. */
34 protected int count;
36 public ByteArrayInputStream(byte[] buffer)
38 this(buffer, 0, buffer.length);
41 public ByteArrayInputStream(byte[] buffer, int offset, int length)
43 buf = buffer;
45 count = offset + length;
46 if (count > buf.length)
47 count = buf.length;
49 pos = offset;
50 // TBD: What should we do if pos is neg. or > count? E.g. throw exc. or:
51 // if (pos < 0 || pos > count)
52 // pos = 0;
54 mark = pos;
57 public synchronized int available()
59 return count - pos;
62 public synchronized void mark(int readAheadLimit)
64 // readAheadLimit is ignored per Java Class Lib. book, p.220.
65 mark = pos;
68 public boolean markSupported()
70 return true;
73 public synchronized int read()
75 if (pos < 0)
76 throw new ArrayIndexOutOfBoundsException(pos);
78 if (pos < count)
79 return ((int) buf[pos++]) & 0xFF;
80 return -1;
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();
89 if (pos >= count)
90 return -1;
92 int numBytes = Math.min(count - pos, len);
93 System.arraycopy(buf, pos, b, off, numBytes);
94 pos += numBytes;
95 return numBytes;
98 public synchronized void reset()
100 pos = mark;
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);
110 pos += numBytes;
111 return numBytes;