View Javadoc

1   /*
2    *   Magic-Project is a turn based strategy simulator
3    *   Copyright (C) 2003-2007 Fabrice Daugan
4    *
5    *   This program is free software; you can redistribute it and/or modify it 
6    * under the terms of the GNU General Public License as published by the Free 
7    * Software Foundation; either version 2 of the License, or (at your option) any
8    * later version.
9    *
10   *   This program is distributed in the hope that it will be useful, but WITHOUT 
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
13   * details.
14   *
15   *   You should have received a copy of the GNU General Public License along  
16   * with this program; if not, write to the Free Software Foundation, Inc., 
17   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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 }