2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / gnu / java / net / protocol / core / CoreInputStream.java
blob421bb1c476586dc53878edcc492304b5fcb9694e
1 // Handler.java - URLStreamHandler for core protocol.
3 /* Copyright (C) 2001 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
11 package gnu.java.net.protocol.core;
13 import gnu.gcj.Core;
14 import gnu.gcj.RawData;
15 import java.io.InputStream;
16 import java.io.IOException;
18 public class CoreInputStream extends InputStream
20 /* A pointer to the object in memory. */
21 protected RawData ptr;
23 /* Position of the next byte in core to be read. */
24 protected int pos;
26 /* The currently marked position in the stream. */
27 protected int mark;
29 /* The index in core one greater than the last valid character. */
30 protected int count;
32 private native int unsafeGetByte (long offset);
33 private native int copyIntoByteArray (byte[] dest, int offset, int numBytes);
35 public CoreInputStream (Core core)
37 ptr = core.ptr;
38 count = core.length;
41 public synchronized int available()
43 return count - pos;
46 public synchronized void mark(int readAheadLimit)
48 // readAheadLimit is ignored per Java Class Lib. book, p.220.
49 mark = pos;
52 public boolean markSupported()
54 return true;
57 public synchronized int read()
59 if (pos < count)
60 return ((int) unsafeGetByte(pos++)) & 0xFF;
61 return -1;
64 public synchronized int read(byte[] b, int off, int len)
66 if (pos >= count)
67 return -1;
69 int numBytes = Math.min(count - pos, len);
70 copyIntoByteArray (b, off, numBytes);
71 pos += numBytes;
72 return numBytes;
75 public synchronized void reset()
77 pos = mark;
80 public synchronized long skip(long n)
82 // Even though the var numBytes is a long, in reality it can never
83 // be larger than an int since the result of subtracting 2 positive
84 // ints will always fit in an int. Since we have to return a long
85 // anyway, numBytes might as well just be a long.
86 long numBytes = Math.min ((long) (count - pos), n < 0 ? 0L : n);
87 pos += numBytes;
88 return numBytes;