• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (C) 2003 Vladimir Roubtsov. All rights reserved.
2  *
3  * This program and the accompanying materials are made available under
4  * the terms of the Common Public License v1.0 which accompanies this distribution,
5  * and is available at http://www.eclipse.org/legal/cpl-v10.html
6  *
7  * $Id: ByteArrayIStream.java,v 1.1.1.1 2004/05/09 16:57:51 vlad_r Exp $
8  */
9 package com.vladium.util;
10 
11 import java.io.InputStream;
12 
13 import com.vladium.util.asserts.$assert;
14 
15 // ----------------------------------------------------------------------------
16 /**
17  * An unsynchronized version of java.io.ByteArrayInputStream.<p>
18  *
19  * All argument validation is disabled in release mode.<p>
20  *
21  * @author (C) 2001, Vlad Roubtsov
22  */
23 public
24 final class ByteArrayIStream extends InputStream
25 {
26     // public: ................................................................
27 
28 
ByteArrayIStream(final byte [] buf)29     public ByteArrayIStream (final byte [] buf)
30     {
31         this (buf, buf.length);
32     }
33 
ByteArrayIStream(final byte [] buf, final int length)34     public ByteArrayIStream (final byte [] buf, final int length)
35     {
36         if ($assert.ENABLED) $assert.ASSERT ((length >= 0) && (length <= buf.length),
37             "invalid length: " + length);
38 
39         m_buf = buf;
40         m_max = length;
41     }
42 
43     // InputStream:
44 
read()45     public final int read ()
46     {
47         if (m_pos >= m_max)
48             return -1;
49         else
50             return 0xFF & m_buf [m_pos ++];
51     }
52 
read(final byte [] buf, final int offset, int length)53     public final int read (final byte [] buf, final int offset, int length)
54     {
55         if ($assert.ENABLED)
56             $assert.ASSERT ((offset >= 0) && (offset <= buf.length) &&
57                 (length >= 0) && ((offset + length) <= buf.length),
58                 "invalid input (" + buf.length + ", " + offset + ", " + length + ")");
59 
60         final int pos = m_pos;
61         final int max = m_max;
62 
63         if (pos >= max) return -1;
64         if (pos + length > max) length = max - pos;
65         if (length <= 0) return 0;
66 
67         final byte [] mbuf = m_buf;
68 
69         if (length < NATIVE_COPY_THRESHOLD)
70             for (int i = 0; i < length; ++ i) buf [offset + i] = mbuf [pos + i];
71         else
72             System.arraycopy (mbuf, pos, buf, offset, length);
73 
74         m_pos += length;
75 
76         return length;
77     }
78 
available()79     public final int available ()
80     {
81         return m_max - m_pos;
82     }
83 
skip(long n)84     public final long skip (long n)
85     {
86         if (m_pos + n > m_max) n = m_max - m_pos;
87 
88         if (n < 0) return 0;
89         m_pos += n;
90 
91         return n;
92     }
93 
94     /**
95      * Differs from the contruct for InputStream.reset() in that this method
96      * always resets the stream to the same it was immediately after creation.
97      */
reset()98     public final void reset ()
99     {
100         m_pos = 0;
101     }
102 
103     /**
104      * Equivalent to {@link #reset()}.
105      */
close()106     public final void close ()
107     {
108         reset ();
109     }
110 
111     // protected: .............................................................
112 
113     // package: ...............................................................
114 
115     // private: ...............................................................
116 
117 
118     private final byte [] m_buf;
119     private final int m_max;
120     private int m_pos;
121 
122     private static final int NATIVE_COPY_THRESHOLD  = 9;
123 
124 } // end of class
125 // ----------------------------------------------------------------------------