1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.sf.magicproject.network;
22
23 import java.io.BufferedReader;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.io.OutputStream;
28
29 import net.sf.magicproject.tools.MToolKit;
30
31 /***
32 * @author Fabrice Daugan
33 * @since 0.2c
34 */
35 public class MInputStream {
36
37 /***
38 * create a new instance of MInputStream
39 *
40 * @param inputStream
41 * is the inputStream where will be read the bytes
42 */
43 public MInputStream(InputStream inputStream) {
44 this.inputStream = inputStream;
45 this.buffIn = null;
46 this.outputStream = null;
47 }
48
49 /***
50 * create a new instance of MInputStream
51 *
52 * @param inputStream
53 * is the inputStream where will be read the bytes
54 * @param outputStream
55 * is the OutputStream where will be sent the bytes
56 */
57 public MInputStream(InputStream inputStream, OutputStream outputStream) {
58 this.inputStream = null;
59 this.buffIn = new BufferedReader(new InputStreamReader(inputStream));
60 this.outputStream = outputStream;
61 }
62
63 /***
64 * read a line from inputstream
65 *
66 * @return the line read from the inputstream
67 */
68 public String readLine() {
69 try {
70 if (buffIn != null) {
71 return buffIn.readLine();
72 }
73 return MToolKit.readString(inputStream);
74 } catch (IOException e) {
75 e.printStackTrace();
76 return null;
77 }
78 }
79
80 /***
81 * write a specified line in the outputStream stream.
82 *
83 * @param line
84 * is the string to write
85 */
86 public void sendLine(String line) {
87 if (outputStream != null) {
88 MToolKit.writeString(outputStream, line);
89 }
90 }
91
92 /***
93 * flush the current outputstream
94 */
95 public void flush() {
96 try {
97 outputStream.flush();
98 } catch (IOException e) {
99 e.printStackTrace();
100 }
101 }
102
103 /***
104 * the current inputstream
105 */
106 private final InputStream inputStream;
107
108 /***
109 * the buffer where bytes are written
110 */
111 private final OutputStream outputStream;
112
113 private final BufferedReader buffIn;
114 }