01: /*
02: * Lucane - a collaborative platform
03: * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation; either
08: * version 2.1 of the License, or (at your option) any later version.
09: *
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: private AudioConf plugin;
27: private ObjectConnection connection;
28: private byte[] buffer;
29: private int index;
30:
31: public AudioConfInputStream(AudioConf plugin,
32: ObjectConnection connection) {
33: this .plugin = plugin;
34: this .connection = connection;
35: this .buffer = new byte[0];
36: this .index = 0;
37: }
38:
39: public int read() throws IOException {
40: if (index >= buffer.length)
41: readNextBuffer();
42:
43: //end of stream ?
44: if (index >= buffer.length)
45: return -1;
46:
47: return buffer[index++];
48: }
49:
50: public int read(byte[] array, int offset, int length)
51: throws IOException {
52: int bytes = 0;
53:
54: while (offset < array.length && bytes < length) {
55: if (index >= buffer.length) {
56: readNextBuffer();
57: continue;
58: }
59:
60: array[offset++] = buffer[index++];
61: bytes++;
62: }
63:
64: return bytes;
65: }
66:
67: public int available() {
68: return this .buffer.length - index;
69: }
70:
71: public void close() throws IOException {
72: this .connection.close();
73: super .close();
74: }
75:
76: private synchronized void readNextBuffer() throws IOException {
77: this .index = 0;
78: try {
79: this .buffer = (byte[]) connection.read();
80: } catch (ClassNotFoundException e) {
81: //byte[] should always exist...
82: this .buffer = new byte[0];
83: e.printStackTrace();
84: }
85: }
86: }
|