home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Java / JavaRadioStation / Source / com / apple / jens / radio / Buffer.java next >
Encoding:
Java Source  |  2000-09-28  |  7.4 KB  |  237 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        Buffer.java
  3.     
  4.     Copyright:     © Copyright 1999-2000 Apple Computer, Inc. All rights reserved.
  5.     
  6.     Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  7.                 ("Apple") in consideration of your agreement to the following terms, and your
  8.                 use, installation, modification or redistribution of this Apple software
  9.                 constitutes acceptance of these terms.  If you do not agree with these terms,
  10.                 please do not use, install, modify or redistribute this Apple software.
  11.  
  12.                 In consideration of your agreement to abide by the following terms, and subject
  13.                 to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  14.                 copyrights in this original Apple software (the "Apple Software"), to use,
  15.                 reproduce, modify and redistribute the Apple Software, with or without
  16.                 modifications, in source and/or binary forms; provided that if you redistribute
  17.                 the Apple Software in its entirety and without modifications, you must retain
  18.                 this notice and the following text and disclaimers in all such redistributions of
  19.                 the Apple Software.  Neither the name, trademarks, service marks or logos of
  20.                 Apple Computer, Inc. may be used to endorse or promote products derived from the
  21.                 Apple Software without specific prior written permission from Apple.  Except as
  22.                 expressly stated in this notice, no other rights or licenses, express or implied,
  23.                 are granted by Apple herein, including but not limited to any patent rights that
  24.                 may be infringed by your derivative works or by other works in which the Apple
  25.                 Software may be incorporated.
  26.  
  27.                 The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  28.                 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  29.                 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30.                 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  31.                 COMBINATION WITH YOUR PRODUCTS.
  32.  
  33.                 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  34.                 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  35.                 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  36.                 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  37.                 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  38.                 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  39.                 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40.                 
  41.     Change History (most recent first):
  42.  
  43. */
  44.  
  45.  
  46. package com.apple.jens.radio;
  47.  
  48. import java.io.*;
  49. import java.util.Vector;
  50.  
  51.  
  52. /** A buffer for raw binary data, including the methods to write to (fill) the buffer from
  53.     an InputStream, and to read the data from the Buffer to an OutputStream.
  54.     
  55.     <p> This class manages the synchronization between a single writer (the DJ thread)
  56.     and multiple readers (the Transmitter threads):
  57.     <ul>
  58.         <li> Multiple readers can access the buffer at once.
  59.         <li> Only one writer can access it at once.
  60.         <li> Read attempts will block while a Buffer is being written. (This prevents
  61.              Transmitter threads from overtaking the DJ.)
  62.         <li> However, a writer can replace the contents of a Buffer that's being read from --
  63.              the current readers will get the old contents, though.
  64.              (This prevents the DJ from getting blocked by a stuck (paused) Transmitter,
  65.              since the DJ uses a ring of Buffers.)
  66.     </ul>
  67.     
  68. */
  69.     
  70. class Buffer {
  71.  
  72.     // LIFECYCLE:
  73.     
  74.     
  75.     public Buffer( String name ) {
  76.         fBytes = new byte[sSize];
  77.         fName = name;
  78.     }
  79.     
  80.     
  81.     public synchronized String toString( ) {
  82.         return "Buffer["+fName+", "
  83.             +((fLength+512)/1024)+"K"
  84.             +( fReaders >0 ?", "+fReaders+" reader(s)" :"" )
  85.             +( fWriting ?", 1 writer)" :"" )
  86.             +"]";
  87.     }
  88.     
  89.     
  90.     // DATA ACCESS:
  91.     
  92.     
  93.     /** Fills the buffer with data read from the stream (up to the maximum buffer size.)
  94.         @return  True if data was read, or false if at EOF */
  95.     public boolean readFrom( InputStream in, int bitRate ) throws IOException {
  96.         // Grab the fWriting flag, making sure that only one thread can write.
  97.         // Note that this does not block if other threads are reading!
  98.         int nReaders;
  99.         synchronized(this) {
  100.             if( fWriting )
  101.                 throw new IllegalStateException("Buffer already has a writer");
  102.             fWriting = true;
  103.             nReaders = fReaders;
  104.             if( nReaders > 0 )
  105.                 Radio.WARN(1,this,"About to overwrite while already being read -- DJ overtook Transmitter");
  106.         }
  107.         
  108.         fBitRate = bitRate;
  109.         int bytesRead = -1; // number of bytes read
  110.         byte[] bytes = null;
  111.         
  112.         try{
  113.             // Allocate a new byte array for the data if there are any readers,
  114.             // so as not to clobber the old data they're using:
  115.             bytes = nReaders==0 ?fBytes :new byte[sSize];
  116.             
  117.             // Read the data from the stream into the buffer:
  118.             bytesRead = read(in,bytes);
  119.         }finally{
  120.         
  121.             synchronized(this) {
  122.                 // Update the stored length, and the byte array itself (unless we hit an error):
  123.                 fLength = Math.max(0,bytesRead);
  124.                 if( bytesRead >= 0 )
  125.                     fBytes = bytes;
  126.                 
  127.                 // Finally release fWriting and notify any blocked readers:
  128.                 fWriting = false;
  129.                 if( fReaders > 0 ) {
  130.                     LOG(3,"Notifying readers now that write is over");
  131.                     this.notifyAll();
  132.                 }
  133.             }
  134.         }
  135.         
  136.         return bytesRead > 0;
  137.     }
  138.     
  139.     
  140.     /** Similar to DataInputStream.readFully except that it doesn't require that
  141.         the entire array be fully read (thus it will not throw EOFException).
  142.         @return the number of bytes read; zero if at EOF */
  143.     private static int read( InputStream in, byte[] bytes ) throws IOException {
  144.         int n = 0;
  145.         while (n < bytes.length) {
  146.             int count = in.read(bytes, n, bytes.length - n);
  147.             if (count < 0)
  148.                 break;
  149.             n += count;
  150.         }
  151.         return n;
  152.     }
  153.     
  154.     
  155.     /** Writes the buffer's data to an OutputStream.
  156.         @return  The number of milliseconds of real time that this MP3 data represents. */
  157.     public long writeTo( OutputStream out ) throws IOException, InterruptedException {
  158.         byte[] bytes;
  159.         int length;
  160.         int bitRate;
  161.         
  162.         // Block until the buffer is written (to avoid overtaking the DJ thread),
  163.         // then bump the number of readers:
  164.         synchronized(this) {
  165.             if(fWriting) {
  166.                 LOG(3,"Blocking in writeTo until writer finishes");
  167.                 while( fWriting ) {
  168.                     this.wait();
  169.                 }
  170.                 LOG(3,"Done blocking in writeTo");
  171.             }
  172.             fReaders++;
  173.             
  174.             bytes = fBytes;
  175.             length = fLength;
  176.             bitRate = fBitRate;
  177.         }
  178.         
  179.         // Write the data:
  180.         try{
  181.             out.write(bytes,0,length);
  182.         }finally{
  183.             // Finally decrement the number of readers:
  184.             synchronized(this) {
  185.                 fReaders--;
  186.             }
  187.         }
  188.         
  189.         return length * 8000 / (long)bitRate;
  190.     }
  191.     
  192.     
  193.     /** Returns the current length in bytes of the data in the Buffer. */
  194.     public int getLength( ) {
  195.         return fLength;
  196.     }
  197.         
  198.     
  199.     private void LOG( int level, String msg ) {
  200.         Radio.LOG(level,this,msg);
  201.     }
  202.     
  203.  
  204.     // INSTANCE DATA:
  205.     
  206.     
  207.     /** The buffer's name, for debugging/logging purposes */
  208.     private final String fName;
  209.  
  210.     /** The data buffer. Only the first fLength bytes contain valid data. */
  211.     private byte[]    fBytes;
  212.     
  213.     /** The length of the valid data in fBytes. */
  214.     private int        fLength;
  215.     
  216.     /** The bitrate of the MP3 data in fBytes. */
  217.     private int        fBitRate;
  218.     
  219.     /** The number of threads reading from the buffer (in the writeTo method).
  220.         Access is synchronized against 'this'  */
  221.     private int        fReaders;
  222.     
  223.     /** True if a thread is writing to the buffer (in the readFrom method).
  224.         Access is synchronized against 'this'  */
  225.     private boolean    fWriting;
  226.     
  227.     
  228.     // STATIC DATA:
  229.     
  230.     
  231.     /** The size that will be used for newly created Buffers.
  232.         This is initialized by Radio.main
  233.         @see Radio#main */
  234.     public static int sSize = 32768;
  235.     
  236. }
  237.