28import java.io.IOException;
29import java.io.OutputStream;
30import java.nio.ByteBuffer;
31import java.nio.channels.FileChannel;
32import java.nio.channels.FileChannel.MapMode;
34import org.jau.io.MappedByteBufferInputStream.CacheMode;
35import org.jau.io.MappedByteBufferInputStream.FileResizeOp;
53 if( FileChannel.MapMode.READ_ONLY == parent.getMapMode() ) {
54 throw new IOException(
"FileChannel map-mode is read-only");
73 final FileChannel.MapMode mmode,
75 final int sliceShift,
final FileResizeOp fileResizeOp)
throws IOException {
95 public final synchronized void setLength(
final long newTotalSize)
throws IOException {
109 public final synchronized long length() {
116 public final synchronized long remaining() throws IOException {
123 public final synchronized long position() throws IOException {
131 return parent.
position(newPosition);
137 public final synchronized long skip(
final long n )
throws IOException {
138 return parent.
skip(n);
142 public final synchronized void flush() throws IOException {
143 parent.
flush(
true );
150 public final synchronized void flush(
final boolean metaData)
throws IOException {
151 parent.
flush(metaData);
155 public final synchronized void close() throws IOException {
160 public final synchronized void write(
final int b)
throws IOException {
162 final long totalRem = parent.
remaining();
163 if ( totalRem < 1 ) {
167 final int currRem = slice.remaining();
168 if ( 0 == currRem ) {
169 if (
null == ( slice = parent.
nextSlice() ) ) {
171 System.err.println(
"EOT write: "+parent.
currentSlice());
172 parent.dbgDump(
"EOT write:", System.err);
174 throw new IOException(
"EOT");
177 slice.put( (
byte)(b & 0xFF) );
180 if(
null != slice ) {
181 parent.syncSlice(slice);
186 public final synchronized void write(
final byte b[],
final int off,
final int len)
throws IOException {
189 throw new NullPointerException();
190 }
else if( off < 0 ||
193 off + len > b.length ||
196 throw new IndexOutOfBoundsException(
"offset "+off+
", length "+len+
", b.length "+b.length);
197 }
else if( 0 == len ) {
200 final long totalRem = parent.
remaining();
201 if ( totalRem < len ) {
205 ByteBuffer slice =
null;
206 while( written < len ) {
208 int currRem = slice.remaining();
209 if ( 0 == currRem ) {
210 if (
null == ( slice = parent.
nextSlice() ) ) {
212 System.err.println(
"EOT write: offset "+off+
", length "+len+
", b.length "+b.length);
213 System.err.println(
"EOT write: written "+written+
" / "+len+
", currRem "+currRem);
214 System.err.println(
"EOT write: "+parent.
currentSlice());
215 parent.dbgDump(
"EOT write:", System.err);
217 throw new InternalError(
"EOT");
219 currRem = slice.remaining();
221 final int currLen = Math.min( len - written, currRem );
222 slice.put( b, off + written, currLen );
226 if(
null != slice ) {
227 parent.syncSlice(slice);
239 public final synchronized void write(
final ByteBuffer b,
final int len)
throws IOException {
242 throw new NullPointerException();
243 }
else if (len < 0 || len > b.remaining()) {
244 throw new IndexOutOfBoundsException(
"length "+len+
", b "+b);
245 }
else if( 0 == len ) {
248 final long totalRem = parent.
remaining();
249 if ( totalRem < len ) {
253 ByteBuffer slice =
null;
254 while( written < len ) {
256 int currRem = slice.remaining();
257 if ( 0 == currRem ) {
258 if (
null == ( slice = parent.
nextSlice() ) ) {
260 System.err.println(
"EOT write: length "+len+
", b "+b);
261 System.err.println(
"EOT write: written "+written+
" / "+len+
", currRem "+currRem);
262 System.err.println(
"EOT write: "+parent.
currentSlice());
263 parent.dbgDump(
"EOT write:", System.err);
265 throw new InternalError(
"EOT");
267 currRem = slice.remaining();
269 final int currLen = Math.min( len - written, currRem );
271 if( slice.hasArray() && b.hasArray() ) {
272 System.arraycopy(b.array(), b.arrayOffset() + b.position(),
273 slice.array(), slice.arrayOffset() + slice.position(),
275 b.position( b.position() + currLen );
276 slice.position( slice.position() + currLen );
277 }
else if( currLen == currRem ) {
280 final int _limit = b.limit();
291 if(
null != slice ) {
292 parent.syncSlice(slice);
311 throw new NullPointerException();
312 }
else if (len < 0 || len > b.remaining()) {
313 throw new IndexOutOfBoundsException(
"length "+len+
", b "+b);
314 }
else if( 0 == len ) {
317 final long totalRem = parent.
remaining();
318 if ( totalRem < len ) {
322 ByteBuffer slice =
null;
323 while( written < len ) {
325 int currRem = slice.remaining();
326 if ( 0 == currRem ) {
327 if (
null == ( slice = parent.
nextSlice() ) ) {
329 System.err.println(
"EOT write: length "+len+
", b "+b);
330 System.err.println(
"EOT write: written "+written+
" / "+len+
", currRem "+currRem);
331 System.err.println(
"EOT write: "+parent.
currentSlice());
332 parent.dbgDump(
"EOT write:", System.err);
334 throw new InternalError(
"EOT");
336 currRem = slice.remaining();
338 final int currLen = b.read(slice, (
int)Math.min( len - written, currRem ));
340 throw new InternalError(
"Unexpected InputStream EOT");
345 if(
null != slice ) {
346 parent.syncSlice(slice);
An OutputStream implementation based on an underlying FileChannel's memory mapped ByteBuffer.
final synchronized void write(final ByteBuffer b, final int len)
Perform similar to write(byte[], int, int) with ByteBuffer instead of byte array.
final synchronized long position()
See MappedByteBufferInputStream#position().
final synchronized void close()
final synchronized long remaining()
See MappedByteBufferInputStream#remaining().
MappedByteBufferOutputStream(final FileChannel fileChannel, final FileChannel.MapMode mmode, final CacheMode cmode, final int sliceShift, final FileResizeOp fileResizeOp)
Creates a new instance using the given FileChannel.
final synchronized void flush(final boolean metaData)
See MappedByteBufferInputStream#flush(boolean).
final synchronized long length()
See MappedByteBufferInputStream#length().
final synchronized void flush()
final synchronized void write(final int b)
final synchronized boolean getSynchronous()
See MappedByteBufferInputStream#getSynchronous().
final synchronized long skip(final long n)
See MappedByteBufferInputStream#skip(long).
final synchronized void notifyLengthChange(final long newTotalSize)
See MappedByteBufferInputStream#notifyLengthChange(long).
final synchronized void setLength(final long newTotalSize)
See MappedByteBufferInputStream#setLength(long).
final synchronized void write(final byte b[], final int off, final int len)
final synchronized MappedByteBufferInputStream position(final long newPosition)
See MappedByteBufferInputStream#position(long).
final synchronized void setSynchronous(final boolean s)
See MappedByteBufferInputStream#setSynchronous(boolean).
final synchronized void write(final MappedByteBufferInputStream b, final long len)
Perform similar to write(ByteBuffer, int) with MappedByteBufferInputStream instead of byte array.