1 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 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 e) { 93 this.buffer = new byte[0]; 95 e.printStackTrace(); 96 } 97 } 98 } 99 | Popular Tags |