bytebufferinputstream.java

来自「MG4J (Managing Gigabytes for Java) is a 」· Java 代码 · 共 116 行

JAVA
116
字号
package it.unimi.dsi.mg4j.io;/*		  * MG4J: Managing Gigabytes for Java * * Copyright (C) 2007 Sebastiano Vigna  * *  This library is free software; you can redistribute it and/or modify it *  under the terms of the GNU Lesser General Public License as published by the Free *  Software Foundation; either version 2.1 of the License, or (at your option) *  any later version. * *  This library is distributed in the hope that it will be useful, but *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License *  for more details. * *  You should have received a copy of the GNU Lesser General Public License *  along with this program; if not, write to the Free Software *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * */import it.unimi.dsi.fastutil.io.MeasurableInputStream;import it.unimi.dsi.fastutil.io.RepositionableStream;import java.io.IOException;import java.io.InputStream;import java.nio.ByteBuffer;import java.nio.MappedByteBuffer;/** A bridge between byte {@linkplain ByteBuffer buffers} and {@linkplain InputStream input streams}. *  * <p>The main usefulness of this class is that of making it possible creating input streams * that are really based on a {@link MappedByteBuffer}. *  * @author Sebastiano Vigna * @since 1.2 * @deprecated Moved to <code>dsiutils</code>. */@Deprecatedpublic class ByteBufferInputStream extends MeasurableInputStream implements RepositionableStream {	/** The underlying byte buffer. */	private final ByteBuffer byteBuffer;	/** The current mark as a position, or -1 if no mark exists. */	private int mark = -1;	/** Creates a new byte-buffer input stream.	 * 	 * @param byteBuffer the underlying byte buffer.	 */		public ByteBufferInputStream( final ByteBuffer byteBuffer ) {		this.byteBuffer = byteBuffer;	}	public int available() {		return byteBuffer.remaining();	}	@Override	public boolean markSupported() {		return true;	}	@Override	public synchronized void mark( final int unused ) {		this.mark = byteBuffer.position();	}	@Override	public synchronized void reset() throws IOException {		if ( mark == -1 ) throw new IOException();		position( mark );	}	@Override	public long skip( final long n ) throws IOException {		final int toSkip = (int)Math.min( byteBuffer.remaining(), n );		position( position() + toSkip );		return toSkip;	}	@Override	public int read() {		if ( ! byteBuffer.hasRemaining() ) return -1;		return byteBuffer.get() & 0xFF;	}	public int read( final byte[] b, final int offset, final int length ) {		if ( length == 0 ) return 0;		if ( ! byteBuffer.hasRemaining() ) return -1;		final int realLength = Math.min( byteBuffer.remaining(), length );		byteBuffer.get( b, offset, realLength );		return realLength;	}	@Override	public long length() throws IOException {		return byteBuffer.capacity();	}	@Override	public long position() throws IOException {		return byteBuffer.position();	}		public void position( final long newPosition ) throws IOException {		byteBuffer.position( (int)Math.min( newPosition, length() ) );	}	}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?