OSDN Git Service

* All files: Updated copyright information.
[pf3gnuchains/gcc-fork.git] / libjava / java / io / ByteArrayInputStream.java
1 /* Copyright (C) 1998, 1999  Free Software Foundation
2
3    This file is part of libgcj.
4
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
7 details.  */
8  
9 package java.io;
10
11 /**
12  * @author Warren Levy <warrenl@cygnus.com>
13  * @date October 7, 1998.  
14  */ 
15
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
20  */
21  
22 public class ByteArrayInputStream extends InputStream
23 {
24   /* An array of bytes provided by the creator of the stream. */
25   protected byte[] buf;
26
27   /* Position of the next byte in buf to be read. */
28   protected int pos;
29
30   /* The currently marked position in the stream. */
31   protected int mark;
32
33   /* The index in buf one greater than the last valid character. */
34   protected int count;
35
36   public ByteArrayInputStream(byte[] buffer)
37   {
38     this(buffer, 0, buffer.length);
39   }
40
41   public ByteArrayInputStream(byte[] buffer, int offset, int length)
42   {
43     buf = buffer;
44
45     count = offset + length;
46     if (count > buf.length)
47       count = buf.length;
48
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;
53
54     mark = pos;
55   }
56
57   public synchronized int available()
58   {
59     return count - pos;
60   }
61
62   public synchronized void mark(int readAheadLimit)
63   {
64     // readAheadLimit is ignored per Java Class Lib. book, p.220.
65     mark = pos;
66   }
67
68   public boolean markSupported()
69   {
70     return true;
71   }
72
73   public synchronized int read()
74   {
75     if (pos < 0)
76       throw new ArrayIndexOutOfBoundsException(pos);
77
78     if (pos < count)
79       return ((int) buf[pos++]) & 0xFF;
80     return -1;
81   }
82
83   public synchronized int read(byte[] b, int off, int len)
84   {
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();
88
89     if (pos >= count)
90       return -1;
91
92     int numBytes = Math.min(count - pos, len);
93     System.arraycopy(buf, pos, b, off, numBytes);
94     pos += numBytes;
95     return numBytes;
96   }
97
98   public synchronized void reset()
99   {
100     pos = mark;
101   }
102
103   public synchronized long skip(long n)
104   {
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;
112   }
113 }