1 |
/* |
2 |
* jlscp - a java LinuxSampler control protocol API |
3 |
* |
4 |
* Copyright (C) 2005-2006 Grigor Iliev <grigor@grigoriliev.com> |
5 |
* |
6 |
* This file is part of jlscp. |
7 |
* |
8 |
* jlscp is free software; you can redistribute it and/or modify |
9 |
* it under the terms of the GNU General Public License version 2 |
10 |
* as published by the Free Software Foundation. |
11 |
* |
12 |
* jlscp is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with jlscp; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
20 |
* MA 02111-1307 USA |
21 |
*/ |
22 |
|
23 |
package org.linuxsampler.lscp; |
24 |
|
25 |
import java.io.InputStream; |
26 |
import java.io.IOException; |
27 |
|
28 |
|
29 |
/** |
30 |
* |
31 |
* @author Grigor Iliev |
32 |
*/ |
33 |
class LscpInputStream { |
34 |
private InputStream in; |
35 |
private StringBuffer buf = new StringBuffer(); |
36 |
|
37 |
/** |
38 |
* Creates a new instance of LscpInputStream. |
39 |
* |
40 |
*/ |
41 |
public |
42 |
LscpInputStream(InputStream in) { |
43 |
this.in = in; |
44 |
} |
45 |
|
46 |
/** |
47 |
* Reads a line. |
48 |
* This method is thread safe. |
49 |
* |
50 |
* @return A string containing the next line readed from the stream or <code>null</code> |
51 |
* if the end of the stream has been reached. |
52 |
* |
53 |
* @throws LscpException If the end of line or file is reached unexpectedly. |
54 |
* @throws IOException If an I/O error occurs. |
55 |
*/ |
56 |
public synchronized String |
57 |
readLine() throws IOException, LscpException { |
58 |
int i; |
59 |
buf.setLength(0); |
60 |
|
61 |
while((i = in.read()) != -1) { |
62 |
if(i == '\r') { |
63 |
checkLF(); |
64 |
break; |
65 |
} |
66 |
buf.append((char)i); |
67 |
} |
68 |
|
69 |
if(i == -1) { |
70 |
if(buf.length() > 0) |
71 |
throw new LscpException(LscpI18n.getLogMsg("LscpInputStream.EOL!")); |
72 |
return null; |
73 |
} |
74 |
return buf.toString(); |
75 |
} |
76 |
|
77 |
/** |
78 |
* Returns the number of bytes that can be read from this input stream without blocking. |
79 |
* |
80 |
* @return The number of bytes that can be read from this input stream without blocking. |
81 |
* @throws IOException If an I/O error occurs. |
82 |
*/ |
83 |
public synchronized int |
84 |
available() throws IOException { return in.available(); } |
85 |
|
86 |
private void |
87 |
skipLine() throws IOException, LscpException { |
88 |
int i; |
89 |
while((i = in.read()) != -1) { |
90 |
if(i == '\r') { |
91 |
checkLF(); |
92 |
break; |
93 |
} |
94 |
} |
95 |
} |
96 |
|
97 |
private void |
98 |
checkLF() throws IOException, LscpException { |
99 |
int i = in.read(); |
100 |
if(i == -1) throw new LscpException(LscpI18n.getLogMsg("LscpInputStream.EOF!")); |
101 |
if(i != '\n') throw new LscpException(LscpI18n.getLogMsg("LscpInputStream.EOL!")); |
102 |
} |
103 |
} |