javauis/mmapi_qt/baseline/javasrc.emc/com/nokia/microedition/media/BufferSourceStream.java
changeset 23 98ccebc37403
equal deleted inserted replaced
21:2a9601315dfc 23:98ccebc37403
       
     1 /*
       
     2 * Copyright (c) 2002 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  SourceStream that buffers data from another SourceStream.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 package com.nokia.microedition.media;
       
    20 
       
    21 import java.io.IOException;
       
    22 import javax.microedition.media.protocol.SourceStream;
       
    23 
       
    24 /**
       
    25  * SourceStream that buffers data from another SourceStream.
       
    26  *
       
    27  */
       
    28 public class BufferSourceStream extends InputStreamSourceStream
       
    29 {
       
    30     // Deferred buffer size
       
    31     private static final int BUFFER_SIZE = 5120;
       
    32 
       
    33     // Stream to buffer
       
    34     SourceStream iSourceStream;
       
    35 
       
    36     byte[] iBuffer;
       
    37 
       
    38     // stream read position
       
    39     int iPos;
       
    40 
       
    41     /**
       
    42      * Constructor.
       
    43      * @param aInputStream An InputStream
       
    44      */
       
    45     public BufferSourceStream(SourceStream aSourceStream)
       
    46     {
       
    47         iSourceStream = aSourceStream;
       
    48     }
       
    49 
       
    50     /**
       
    51      * Return header data.
       
    52      * @return header data
       
    53      */
       
    54     public byte[] getHeader() throws IOException
       
    55     {
       
    56         byte[] tmpBuffer = new byte[ BUFFER_SIZE ];
       
    57         int bytesInBuffer = BUFFER_SIZE;
       
    58 
       
    59         // Read into iBuffer first if iBuffer doesn't already contain
       
    60         // first 256 bytes
       
    61         if (iBuffer == null)
       
    62         {
       
    63             bytesInBuffer = readAndBuffer(tmpBuffer, 0, 256);
       
    64 
       
    65             if (bytesInBuffer < 0)
       
    66             {
       
    67                 throw new IOException();
       
    68             }
       
    69         }
       
    70         else if (iBuffer.length < 256)
       
    71         {
       
    72             bytesInBuffer = readAndBuffer(tmpBuffer, iBuffer.length, (256 - iBuffer.length));
       
    73 
       
    74             if (bytesInBuffer == -1)
       
    75             {
       
    76                 bytesInBuffer = iBuffer.length;
       
    77             }
       
    78         }
       
    79 
       
    80         // tmpBuffer will be overwritten here
       
    81         System.arraycopy(iBuffer, 0,
       
    82                          tmpBuffer, 0, bytesInBuffer);
       
    83 
       
    84         return tmpBuffer;
       
    85     }
       
    86 
       
    87 
       
    88     public int readAndBuffer(byte[] aBuffer, int aOffset, int aLength) throws IOException
       
    89     {
       
    90         int bytesInBuffer = iSourceStream.read(aBuffer, aOffset, aLength);
       
    91 
       
    92         if (iBuffer == null)
       
    93         {
       
    94             iBuffer = new byte[bytesInBuffer];
       
    95 
       
    96             System.arraycopy(aBuffer, aOffset,
       
    97                              iBuffer, 0, bytesInBuffer);
       
    98         }
       
    99         else if (bytesInBuffer >= 0)
       
   100         {
       
   101             byte[] tempBuffer = iBuffer;
       
   102             iBuffer = new byte[iBuffer.length + bytesInBuffer];
       
   103 
       
   104             System.arraycopy(tempBuffer, 0,
       
   105                              iBuffer, 0, tempBuffer.length);
       
   106             System.arraycopy(aBuffer, aOffset,
       
   107                              iBuffer, tempBuffer.length, bytesInBuffer);
       
   108             tempBuffer = null; // relese tempBuffer
       
   109         }
       
   110 
       
   111         return bytesInBuffer;
       
   112     }
       
   113 
       
   114     /**
       
   115      * Read from Input Stream
       
   116      * @param buffer Input Stream
       
   117      * @param offset where reading starts
       
   118      * @param length length of read data
       
   119      * @return bytes read
       
   120      * @throws IOException
       
   121      */
       
   122     public int read(byte[] aBuffer, int aOffset, int aLength) throws IOException
       
   123     {
       
   124         // bytes read from internal buffer
       
   125         int bytesFromBuffer = 0;
       
   126 
       
   127 
       
   128         if ((iBuffer != null) &&   // read from iBuffer if getHeader is called
       
   129                 (iPos < iBuffer.length))
       
   130         {
       
   131             // Need to read from buffer
       
   132             if (aLength < iBuffer.length - iPos)
       
   133             {
       
   134                 // aLength bytes can be read from buffer
       
   135                 bytesFromBuffer = aLength;
       
   136             }
       
   137             else
       
   138             {
       
   139                 // aLength cannot be read from buffer
       
   140                 // read all there is available
       
   141                 bytesFromBuffer = iBuffer.length - iPos;
       
   142             }
       
   143 
       
   144             System.arraycopy(iBuffer, iPos,
       
   145                              aBuffer, aOffset, bytesFromBuffer);
       
   146 
       
   147             // move stream position
       
   148             iPos += bytesFromBuffer;
       
   149             return bytesFromBuffer;
       
   150         }
       
   151 
       
   152         // bytes read from iSourceStream
       
   153         int bytesFromStream = 0;
       
   154         // Check if bytes are needed from SourceStream
       
   155         if (bytesFromBuffer < aLength)
       
   156         {
       
   157             bytesFromStream = iSourceStream.read(aBuffer,
       
   158                                                  bytesFromBuffer,
       
   159                                                  aLength - bytesFromBuffer);
       
   160             if (bytesFromStream != -1)
       
   161             {
       
   162                 // move stream position, if not end of stream
       
   163                 iPos += bytesFromStream;
       
   164             }
       
   165         }
       
   166 
       
   167         return bytesFromStream;
       
   168     }
       
   169 }
       
   170 
       
   171 // End of File