⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chunkinputstream.java

📁 Java的面向对象数据库系统的源代码
💻 JAVA
字号:
// You can redistribute this software and/or modify it under the terms of
// the Ozone Library License version 1 published by ozone-db.org.
//
// The original code and portions created by SMB are
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
//
// $Id: ChunkInputStream.java,v 1.2 2002/09/18 06:54:18 per_nyfelt Exp $

package org.ozoneDB.xml.util;

import java.io.InputStream;
import java.io.IOException;
import java.io.Serializable;


/**
 */
public final class ChunkInputStream extends InputStream implements Serializable {
    
    /**
     * The internal buffer array where the data is stored. When necessary,
     * it may be replaced by another array of
     * a different size.
     */
    protected byte[] buf;
    
    /**
     * The current position in the buffer. This is the index of the next
     * character to be read from the <code>buf</code> array.
     * <p>
     * This value is always in the range <code>0</code>
     * through <code>count</code>. If it is less
     * than <code>count</code>, then  <code>buf[pos]</code>
     * is the next byte to be supplied as input;
     * if it is equal to <code>count</code>, then
     * the  next <code>read</code> or <code>skip</code>
     * operation will require more bytes to be
     * read from the contained  input stream.
     *
     * @see     java.io.BufferedInputStream#buf
     */
    protected int pos;
    
    
    /**
     * Creates a <code>BufferedInputStream</code>
     * and saves its  argument, the input stream
     * <code>in</code>, for later use. An internal
     * buffer array is created and  stored in <code>buf</code>.
     *
     * @param data the underlying input stream.
     */
    public ChunkInputStream( byte[] data ) {
        this.buf = data;
        this.pos = 0;
    }
    
    
    /**
     * See
     * the general contract of the <code>read</code>
     * method of <code>InputStream</code>.
     *
     * @return     the next byte of data, or <code>-1</code> if the end of the
     *             stream is reached.
     * @exception  IOException  if an I/O error occurs.
     * @see        java.io.FilterInputStream#in
     */
    public synchronized int read() throws IOException {
        if (this.pos >= this.buf.length) {
            return -1;
        }

        return this.buf[this.pos++] & 0xff;
    } 
    
    
    /**
     *  Reads bytes from this byte-input stream into the specified byte array,
     *  starting at the given offset.
     *
     *  @param b destination buffer.
     *  @param off offset at which to start storing bytes.
     *  @param len maximum number of bytes to read.
     *  @return the number of bytes read, or <code>-1</code> if the end of
     *          the stream has been reached.
     * @exception  IOException  if an I/O error occurs.
     */
    public synchronized final int read( byte[] b, int off, int len ) throws IOException {
        if (this.pos >= this.buf.length) {
            return -1;
        } 
        
        if (off < 0 || off > b.length || len < 0 || off + len > b.length || off + len < 0) {
            throw new IndexOutOfBoundsException();
        } else {
            if (len == 0) {
                return 0;
            } 
        } 
        
        int toRead = this.buf.length - this.pos;
        len = toRead <= len ? toRead : len;
        System.arraycopy( this.buf, this.pos, b, off, len );
        
        this.pos += len;

        return len;
    } 
    
    
    /**
     *  See the general contract of the <code>skip</code>
     *  method of <code>InputStream</code>.
     *
     *  @param n the number of bytes to be skipped.
     *  @return the actual number of bytes skipped.
     *  @exception IOException if an I/O error occurs.
     */
    public synchronized final long skip( long n ) throws IOException {
        int toRead = this.buf.length - this.pos;
        n = toRead <= n ? toRead : n;
        pos += n;
        return n;
    } 
    
    
    /**
     * Returns the number of bytes that can be read from this input
     * stream without blocking.
     *
     * @return     the number of bytes that can be read from this input
     *             stream without blocking.
     * @exception  IOException  if an I/O error occurs.
     */
    public synchronized final int available() throws IOException {
        return this.buf.length - pos;
    } 
    
    
    /**
     * Tests if this input stream supports the <code>mark</code>
     * and <code>reset</code> methods. The <code>markSupported</code>
     * method of <code>ChunkInputStream</code> returns <code>false</code>.
     *
     * @return  a <code>boolean</code> indicating if this stream type supports
     *          the <code>mark</code> and <code>reset</code> methods.
     */
    public final boolean markSupported() {
        return false;
    } 
    
    
    /**
     * Closes this input stream and releases any system resources
     * associated with the stream.
     *
     * @exception  IOException  if an I/O error occurs.
     */
    public synchronized final void close() throws IOException {
        this.buf = null;
    } 
    
    
    /**
     *  Set a new buffer for this input stream.
     *  @param buffer the data buffer
     */
    public final void setBuffer( byte[] buffer ) {
        this.buf = buffer;
        this.pos = 0;
    } 
    
}

⌨️ 快捷键说明

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