All Downloads are FREE. Search and download functionalities are using the official Maven repository.

mq5.0-source.main.comm-io.src.main.java.com.sun.messaging.jmq.io.JMQByteBufferInputStream Maven / Gradle / Ivy

There is a newer version: 5.1
Show newest version
/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2000-2012 Oracle and/or its affiliates. All rights reserved.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common Development
 * and Distribution License("CDDL") (collectively, the "License").  You
 * may not use this file except in compliance with the License.  You can
 * obtain a copy of the License at
 * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
 * or packager/legal/LICENSE.txt.  See the License for the specific
 * language governing permissions and limitations under the License.
 *
 * When distributing the software, include this License Header Notice in each
 * file and include the License file at packager/legal/LICENSE.txt.
 *
 * GPL Classpath Exception:
 * Oracle designates this particular file as subject to the "Classpath"
 * exception as provided by Oracle in the GPL Version 2 section of the License
 * file that accompanied this code.
 *
 * Modifications:
 * If applicable, add the following below the License Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyright [year] [name of copyright owner]"
 *
 * Contributor(s):
 * If you wish your version of this file to be governed by only the CDDL or
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
 * elects to include this software in this distribution under the [CDDL or GPL
 * Version 2] license."  If you don't indicate a single choice of license, a
 * recipient has the option to distribute your version of this file under
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 * its licensees as provided above.  However, if you add GPL Version 2 code
 * and therefore, elected the GPL Version 2 license, then the option applies
 * only if the new code is made subject to such option by the copyright
 * holder.
 */

/*
 * @(#)JMQByteBufferInputStream.java	1.3 06/27/07
 */ 

package com.sun.messaging.jmq.io;

import java.io.InputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

/**
 * A JMQByteBufferInputStream contains
 * an internal ByteBuffer that contains bytes that
 * may be read from the stream.
 */
public
class JMQByteBufferInputStream extends InputStream {

    /**
     * A flag that is set to true when this stream is closed.
     */
    private boolean isClosed = false;

    /**
     * The ByteBuffer holding data
     */
    protected ByteBuffer buf;

    /**
     * Creates a ByteBufferInputStream
     * so that it  uses ByteBuffer as its
     * buffer.
     * The ByteBuffer is not copied, sliced or duplicated.
     * Reads will start at the current position of the byte buffer.
     *
     * @param   buf   the input buffer.
     */
    public JMQByteBufferInputStream(ByteBuffer buf) {
        this.buf = buf;
    }

    /**
     * Reads the next byte of data from this input stream. The value 
     * byte is returned as an int in the range 
     * 0 to 255. If no byte is available 
     * because the end of the stream has been reached, the value 
     * -1 is returned. 
     * 

* This read method * cannot block. *

* The read will update the backing ByteBuffer's position. * * @return the next byte of data, or -1 if the end of the * stream has been reached. */ public synchronized int read() { ensureOpen(); try { // Mask off upper bits to convert to unsigned byte return buf.get() & 0xFF; } catch (Exception e) { return -1; } } /** * Reads up to len bytes of data into an array of bytes * from this input stream. * -1 is returned when there are no more bytes in the buffer. * This read method cannot block. *

* The read will update the backing ByteBuffer's position. * * @param b the buffer into which the data is read. * @param off the start offset of the data. * @param len the maximum number of bytes read. * @return the total number of bytes read into the buffer, or * -1 if there is no more data because the end of * the stream has been reached. */ public synchronized int read(byte b[], int off, int len) { ensureOpen(); if (available() == 0) { return -1; } if (len > available()) { len = available(); } try { buf.get(b, off, len); } catch (Exception e) { // Should never happen System.err.println(this.getClass().getName() + ": Got exception when reading " + len + " bytes from buffer " + buf.toString()); return(-2); } return(len); } /** * Skips n bytes of input from this input stream. Fewer * bytes might be skipped if the end of the input stream is reached. * The actual number k * of bytes to be skipped is equal to the smaller * of n and capacity-pos. * The value k is added into pos * and k is returned. *

* The skip will update the backing ByteBuffer's position. * * @param n the number of bytes to be skipped. * @return the actual number of bytes skipped. */ public synchronized long skip(long n) { ensureOpen(); if (n > available()) { n = available(); } buf.position((int)(buf.position() + n)); return n; } /** * Returns the number of bytes that can be read from this input * stream without blocking. * The value returned is * capacity - pos, * which is the number of bytes remaining to be read from the input buffer. * * @return the number of bytes that can be read from the input stream * without blocking. */ public synchronized int available() { ensureOpen(); return buf.remaining(); } /** * Tests if ByteArrayInputStream supports mark/reset. * * @since JDK1.1 */ public boolean markSupported() { return true; } /** * Set the current marked position in the stream. * ByteBufferInputStream objects are marked at position zero by * default when constructed. They may be marked at another * position within the buffer by this method. * readAheadLimit is ignored. * * @since JDK1.1 */ public void mark(int readAheadLimit) { ensureOpen(); buf.mark(); } /** * Resets the buffer to the marked position. The marked position * is the beginning unless another position was marked. * The value of position is set to 0. */ public synchronized void reset() { ensureOpen(); buf.rewind(); } /** * Closes this input stream and releases any system resources * associated with the stream. *

*/ public synchronized void close() throws IOException { isClosed = true; buf = null; } /** Check to make sure that the stream has not been closed */ private void ensureOpen() { /* This method does nothing for now. Once we add throws clauses * to the I/O methods in this class, it will throw an IOException * if the stream has been closed. */ } /** * Return the back store ByteBuffer. The returned buffer is exactly * the buffer that was passed to the constructor. The buffer's position * is the current position of the backing buffer. */ public ByteBuffer getByteBuffer() { return buf; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy