Make Exported just be SquirrelJMEVendorApi.
[SquirrelJME.git] / modules / cldc-compact / src / main / java / cc / squirreljme / jvm / mle / RawJarPackageBracketInputStream.java
blobfe4d94ad44a35a9a74428ece39cbaaca6e698394
1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the GNU General Public License v3+, or later.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc.squirreljme.jvm.mle;
12 import cc.squirreljme.jvm.mle.brackets.JarPackageBracket;
13 import cc.squirreljme.runtime.cldc.annotation.Api;
14 import java.io.IOException;
15 import java.io.InputStream;
17 /**
18 * This is an input stream over the raw representation of a
19 * {@link JarPackageBracket} which might not be in the form of a JAR or any
20 * actual readable data.
22 * @since 2022/03/04
24 @Api
25 public class RawJarPackageBracketInputStream
26 extends InputStream
28 /** The given library. */
29 protected final JarPackageBracket jar;
31 /** The size of the JAR. */
32 protected final int jarSize;
34 /** Single byte read, as only bulk read is supported. */
35 private final byte[] _singleByte =
36 new byte[1];
38 /** The current read position. */
39 private int _readPos;
41 /**
42 * Initializes the input stream to read the raw JAR.
44 * @param __jar The JAR to read raw data from.
45 * @throws IOException If reading from the given JAR in its
46 * raw data form is not possible.
47 * @throws NullPointerException On null arguments.
48 * @since 2022/03/04
50 @Api
51 public RawJarPackageBracketInputStream(JarPackageBracket __jar)
52 throws IOException, NullPointerException
54 this(__jar, 0);
57 /**
58 * Initializes the input stream to read the raw JAR.
60 * @param __jar The JAR to read raw data from.
61 * @param __offset The initial read offset.
62 * @throws IndexOutOfBoundsException If the offset is out of bounds.
63 * @throws IOException If reading from the given JAR in its
64 * raw data form is not possible.
65 * @throws NullPointerException On null arguments.
66 * @since 2022/04/09
68 @Api
69 public RawJarPackageBracketInputStream(JarPackageBracket __jar,
70 int __offset)
71 throws IndexOutOfBoundsException, IOException, NullPointerException
73 if (__jar == null)
74 throw new NullPointerException("NARG");
76 // {@squirreljme.error ZZ3u The specified JAR cannot be accessed
77 // directly. (The JAR path)}
78 int jarSize = JarPackageShelf.rawSize(__jar);
79 if (jarSize < 0)
80 throw new IOException("ZZ3u " +
81 JarPackageShelf.libraryPath(__jar));
83 // {@squirreljme.error ZZ4j Invalid offset into direct JAR.}
84 if (__offset < 0 || __offset > jarSize)
85 throw new IndexOutOfBoundsException("ZZ4j");
87 // Set for later
88 this.jar = __jar;
89 this.jarSize = jarSize;
90 this._readPos = __offset;
93 /**
94 * {@inheritDoc}
95 * @since 2022/03/04
97 @Override
98 public int available()
99 throws IOException
101 return this.jarSize - this._readPos;
105 * {@inheritDoc}
106 * @since 2022/03/04
108 @Override
109 public int read()
110 throws IOException
112 // Keep trying to read a single byte
113 byte[] singleByte = this._singleByte;
114 for (;;)
116 // Try reading byte
117 int read = this.read(singleByte, 0, 1);
119 // EOF?
120 if (read < 0)
121 return -1;
123 // Use the given byte assuming it was read
124 if (read != 0)
125 return singleByte[0] & 0xFF;
130 * {@inheritDoc}
131 * @since 2022/03/04
133 @Override
134 public int read(byte[] __b, int __o, int __l)
135 throws IndexOutOfBoundsException, IOException, NullPointerException
137 if (__b == null)
138 throw new NullPointerException("NARG");
139 if (__o < 0 || __l < 0 || (__o + __l) < 0 || (__o + __l) > __b.length)
140 throw new IndexOutOfBoundsException("IOOB");
142 // Read in the JAR data
143 int readPos = this._readPos;
144 int count = JarPackageShelf.rawData(this.jar, readPos, __b, __o, __l);
146 // EOF?
147 if (count < 0)
149 this._readPos = this.jarSize;
150 return -1;
153 // Count up what we read
154 this._readPos = readPos + count;
156 // And use our count!
157 return count;