KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > audioconf > AudioConfInputStream


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.applications.audioconf;
20
21 import java.io.*;
22
23 import org.lucane.common.net.ObjectConnection;
24
25 public class AudioConfInputStream extends InputStream
26 {
27     private AudioConf plugin;
28     private ObjectConnection connection;
29     private byte[] buffer;
30     private int index;
31     
32     public AudioConfInputStream(AudioConf plugin, ObjectConnection connection)
33     {
34         this.plugin = plugin;
35         this.connection = connection;
36         this.buffer = new byte[0];
37         this.index = 0;
38     }
39     
40     public int read()
41     throws IOException
42     {
43         if(index >= buffer.length)
44             readNextBuffer();
45
46         //end of stream ?
47
if(index >= buffer.length)
48             return -1;
49                         
50         return buffer[index++];
51     }
52     
53     public int read(byte[] array, int offset, int length)
54     throws IOException
55     {
56         int bytes = 0;
57         
58         while(offset < array.length && bytes < length)
59         {
60             if(index >= buffer.length)
61             {
62                 readNextBuffer();
63                 continue;
64             }
65                             
66             array[offset++] = buffer[index++];
67             bytes++;
68         }
69         
70         return bytes;
71     }
72     
73     public int available()
74     {
75         return this.buffer.length - index;
76     }
77     
78     public void close()
79     throws IOException
80     {
81         this.connection.close();
82         super.close();
83     }
84     
85     
86     private synchronized void readNextBuffer()
87     throws IOException
88     {
89         this.index = 0;
90         try {
91             this.buffer = (byte[])connection.read();
92         } catch (ClassNotFoundException JavaDoc e) {
93             //byte[] should always exist...
94
this.buffer = new byte[0];
95             e.printStackTrace();
96         }
97     }
98 }
99
Popular Tags