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

org.jitsi.impl.neomedia.protocol.BufferStreamAdapter Maven / Gradle / Ivy

/*
 * Copyright @ 2015 Atlassian Pty Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jitsi.impl.neomedia.protocol;

import java.io.*;

import javax.media.*;
import javax.media.protocol.*;

/**
 * Represents a base class for adapters of SourceStreams, usually
 * ones reading data in arrays of bytes and not in Buffers, to
 * SourceStreams reading data in Buffers. An example
 * use is creating a PushBufferStream representation of a PushSourceStream.
 *
 * @param  the very type of SourceStream to be adapted by a
 * BufferStreamAdapter
 * @author Lyubomir Marinov
 */
public abstract class BufferStreamAdapter
    implements SourceStream
{

    /**
     * The Format of this stream to be reported through the output
     * Buffer this instance reads data into.
     */
    private final Format format;

    /**
     * The SourceStream being adapted by this instance.
     */
    protected final T stream;

    /**
     * Initializes a new BufferStreamAdapter which is to adapt a
     * specific SourceStream into a SourceStream with
     * a specific Format.
     *
     * @param stream the SourceStream to be adapted
     * @param format the specific Format of the SourceStream
     */
    public BufferStreamAdapter(T stream, Format format)
    {
        this.stream = stream;
        this.format = format;
    }

    /**
     * Implements SourceStream#endOfStream(). Delegates to the wrapped
     * SourceStream.
     *
     * @return true if the stream is finished, false otherwise
     */
    public boolean endOfStream()
    {
        return stream.endOfStream();
    }

    /**
     * Implements SourceStream#getContentDescriptor(). Delegates to the wrapped
     * SourceStream.
     *
     * @return the ContentDescriptor of the stream
     */
    public ContentDescriptor getContentDescriptor()
    {
        return stream.getContentDescriptor();
    }

    /**
     * Implements SourceStream#getContentLength(). Delegates to the wrapped
     * SourceStream.
     *
     * @return content length
     */
    public long getContentLength()
    {
        return stream.getContentLength();
    }

    /**
     * Implements Controls#getControl(String). Delegates to the wrapped
     * SourceStream.
     *
     * @param controlType a String value naming the type of the control
     * of this instance to be retrieved
     * @return an Object which represents the control of this instance
     * with the specified type
     */
    public Object getControl(String controlType)
    {
        return stream.getControl(controlType);
    }

    /**
     * Implements Controls#getControls(). Delegates to the wrapped SourceStream.
     *
     * @return array of JMF Control objects
     */
    public Object[] getControls()
    {
        return stream.getControls();
    }

    /**
     * Gets the Format of the data this stream provides.
     *
     * @return the Format of the data this stream provides
     */
    public Format getFormat()
    {
        return format;
    }

    /**
     * Gets the SourceStream wrapped by this instance.
     *
     * @return the SourceStream wrapped by this instance
     */
    public T getStream()
    {
        return stream;
    }

    /**
     * Reads byte data from this stream into a specific Buffer
     * which is to use a specific array of bytes for its data.
     *
     * @param buffer the Buffer to read byte data into from this
     * instance
     * @param data the array of bytes to read data into from this
     * instance and to be set as the data of the specified buffer
     * @throws IOException if I/O related errors occurred during read operation
     */
    protected void read(Buffer buffer, byte[] data, int offset, int length)
        throws IOException
    {
        int numberOfBytesRead = doRead(buffer, data, offset, length);

        buffer.setData(data);
        if (numberOfBytesRead >= 0)
        {
            buffer.setLength(numberOfBytesRead);
        }
        else
        {
            buffer.setLength(0);
            if (numberOfBytesRead == -1)
                buffer.setEOM(true);
        }
        buffer.setOffset(offset);

        Format format = getFormat();

        if (format != null)
            buffer.setFormat(format);
    }

    /**
     * Reads byte data from this stream into a specific array of bytes
     * starting the storing at a specific offset and reading at most a specific
     * number of bytes.
     *
     * @param buffer an optional Buffer instance associated with the
     * specified data, offset and length and
     * provided to the method in case the implementation would like to provide
     * additional Buffer properties such as flags
     * @param data the array of bytes into which the data read from
     * this stream is to be written
     * @param offset the offset in the specified data at which
     * writing data read from this stream should start
     * @param length the maximum number of bytes to be written into the
     * specified data
     * @return the number of bytes read from this stream and written into the
     * specified data
     * @throws IOException if I/O related errors occurred during read operation
     */
    protected abstract int doRead(
            Buffer buffer,
            byte[] data, int offset, int length)
        throws IOException;
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy