Source for gnu.java.net.protocol.core.CoreInputStream

   1: // Handler.java - URLStreamHandler for core protocol.
   2: 
   3: /* Copyright (C) 2001  Free Software Foundation
   4: 
   5:    This file is part of libgcj.
   6: 
   7: This software is copyrighted work licensed under the terms of the
   8: Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
   9: details.  */
  10: 
  11: package gnu.java.net.protocol.core;
  12: 
  13: import gnu.gcj.Core;
  14: import gnu.gcj.RawData;
  15: import java.io.InputStream;
  16: import java.io.IOException;
  17: 
  18: public class CoreInputStream extends InputStream
  19: {
  20:   /* A pointer to the object in memory.  */
  21:   protected RawData ptr;
  22: 
  23:   /* Position of the next byte in core to be read. */
  24:   protected int pos;
  25: 
  26:   /* The currently marked position in the stream. */
  27:   protected int mark;
  28: 
  29:   /* The index in core one greater than the last valid character. */
  30:   protected int count;
  31: 
  32:   private native int unsafeGetByte (long offset);
  33:   private native int copyIntoByteArray (byte[] dest, int offset, int numBytes);
  34: 
  35:   public CoreInputStream (Core core)
  36:   {
  37:     ptr = core.ptr;
  38:     count = core.length;
  39:   }
  40: 
  41:   public synchronized int available()
  42:   {
  43:     return count - pos;
  44:   }
  45: 
  46:   public synchronized void mark(int readAheadLimit)
  47:   {
  48:     // readAheadLimit is ignored per Java Class Lib. book, p.220.
  49:     mark = pos;
  50:   }
  51: 
  52:   public boolean markSupported()
  53:   {
  54:     return true;
  55:   }
  56: 
  57:   public synchronized int read()
  58:   {
  59:     if (pos < count)
  60:       return ((int) unsafeGetByte(pos++)) & 0xFF;
  61:     return -1;
  62:   }
  63: 
  64:   public synchronized int read(byte[] b, int off, int len)
  65:   {
  66:     if (pos >= count)
  67:       return -1;
  68: 
  69:     int numBytes = Math.min(count - pos, len);
  70:     copyIntoByteArray (b, off, numBytes);
  71:     pos += numBytes;
  72:     return numBytes;
  73:   }
  74: 
  75:   public synchronized void reset()
  76:   {
  77:     pos = mark;
  78:   }
  79: 
  80:   public synchronized long skip(long n)
  81:   {
  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;
  89:   }
  90: }