GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
ByteBufferInputStream.java
Go to the documentation of this file.
1/**
2 * Copyright 2014 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28package com.jogamp.common.nio;
29
30import java.io.BufferedInputStream;
31import java.io.IOException;
32import java.io.InputStream;
33import java.nio.ByteBuffer;
34import java.nio.MappedByteBuffer;
35import java.nio.channels.FileChannel;
36import java.nio.channels.FileChannel.MapMode;
37
38/**
39 * An {@link InputStream} implementation based on an underlying {@link ByteBuffer}
40 * supporting {@link #markSupported() mark}.
41 * <p>
42 * May be utilized as well with a {@link MappedByteBuffer memory-mapped} {@link FileChannel#map(MapMode, long, long) FileChannel}
43 * using a size &le; {@link Integer#MAX_VALUE}.<br>
44 * This becomes efficient with files &ge; 10 MiB, depending on the platform
45 * and whether the traditional method uses a {@link BufferedInputStream} supporting {@code mark} incl. it's buffer size.<br>
46 * See test case {@code com.jogamp.common.nio.TestByteBufferInputStream}.
47 * </p>
48 * @since 2.3.0
49 */
50public class ByteBufferInputStream extends InputStream {
51 private final ByteBuffer buf;
52 private int mark;
53
54 /**
55 * Creates a new byte-buffer input stream.
56 *
57 * @param buf the underlying byte buffer.
58 */
59 public ByteBufferInputStream(final ByteBuffer buf) {
60 this.buf = buf;
61 this.mark = -1;
62 }
63
64 @Override
65 public final int available() {
66 return buf.remaining();
67 }
68
69 /**
70 * <i>This implementation supports {@code mark}.</i>
71 * <p>
72 * {@inheritDoc}
73 * </p>
74 */
75 @Override
76 public final boolean markSupported() {
77 return true;
78 }
79
80 /**
81 * <i>This implementation supports {@code mark}.</i>
82 * <p>
83 * {@inheritDoc}
84 * </p>
85 * @see #markSupported()
86 */
87 @Override
88 public final synchronized void mark(final int unused) {
89 mark = buf.position();
90 }
91
92 /**
93 * <i>This implementation supports {@code mark}.</i>
94 * <p>
95 * {@inheritDoc}
96 * </p>
97 * @see #markSupported()
98 */
99 @Override
100 public final synchronized void reset() throws IOException {
101 if ( mark == -1 ) {
102 throw new IOException();
103 }
104 buf.position(mark);
105 }
106
107 @Override
108 public final long skip(final long n) throws IOException {
109 if( 0 > n ) {
110 return 0;
111 }
112 final int s = (int) Math.min( buf.remaining(), n );
113 buf.position(buf.position() + s);
114 return s;
115 }
116
117 @Override
118 public final int read() {
119 if ( ! buf.hasRemaining() ) {
120 return -1;
121 }
122 return buf.get() & 0xFF;
123 }
124
125 @Override
126 public final int read(final byte[] b, final int off, final int len) {
127 if (b == null) {
128 throw new NullPointerException();
129 } else if( off < 0 ||
130 len < 0 ||
131 off > b.length ||
132 off + len > b.length ||
133 off + len < 0
134 ) {
135 throw new IndexOutOfBoundsException("offset "+off+", length "+len+", b.length "+b.length);
136 } else if ( 0 == len ) {
137 return 0;
138 }
139 final int totalRem = buf.remaining();
140 if ( 0 == totalRem ) {
141 return -1;
142 }
143 final int maxLen = Math.min(totalRem, len);
144 if( buf.hasArray() ) {
145 System.arraycopy(buf.array(), buf.arrayOffset() + buf.position(), b, off, maxLen);
146 buf.position( buf.position() + maxLen );
147 } else {
148 buf.get(b, off, maxLen);
149 }
150 return maxLen;
151 }
152
153 // @Override
154 public final int read(final ByteBuffer b, final int len) {
155 if (b == null) {
156 throw new NullPointerException();
157 } else if (len < 0 || len > b.remaining()) {
158 throw new IndexOutOfBoundsException("length "+len+", b "+b);
159 } else if ( 0 == len ) {
160 return 0;
161 }
162 final int remaining = buf.remaining();
163 if ( 0 == remaining ) {
164 return -1;
165 }
166 final int maxLen = Math.min(remaining, len);
167 if( buf.hasArray() && b.hasArray() ) {
168 System.arraycopy(buf.array(), buf.arrayOffset() + buf.position(), b.array(), b.arrayOffset() + b.position(), maxLen);
169 buf.position( buf.position() + maxLen );
170 b.position( b.position() + maxLen );
171 } else if( maxLen == remaining ) {
172 b.put(buf);
173 } else {
174 final int _limit = buf.limit();
175 buf.limit(maxLen);
176 try {
177 b.put(buf);
178 } finally {
179 buf.limit(_limit);
180 }
181 }
182 return maxLen;
183 }
184
185 public final ByteBuffer getBuffer() { return buf; }
186}
An InputStream implementation based on an underlying ByteBuffer supporting mark.
final boolean markSupported()
This implementation supports mark.
final synchronized void reset()
This implementation supports mark.
ByteBufferInputStream(final ByteBuffer buf)
Creates a new byte-buffer input stream.
final int read(final byte[] b, final int off, final int len)
final synchronized void mark(final int unused)
This implementation supports mark.
final int read(final ByteBuffer b, final int len)