View Javadoc

1   package org.naftulin.configmgr.parsers;
2   
3   import java.io.BufferedReader;
4   import java.io.IOException;
5   import java.io.InputStreamReader;
6   import java.io.InputStream;
7   
8   /***
9    * Common configuration entry parser class. Used as more of a place-holder for
10   * common functionality. The only method used so far is {@link #readStreamContentAsString(InputStream) 
11   * reading the input stream}.
12   * 
13   * @author Henry Naftulin
14   * @since 1.0
15   */
16  public abstract class AbstractConfigEntryParser implements ConfigEntryParser {
17  
18  	private static final int BUFFER_SIZE = 200;
19  
20  	/***
21  	 * Returns the content of the input stream. Reads the stream and creates a string out 
22  	 * of it's content.
23  	 * @param is the open, buffered input stream
24  	 * @return  the content of the input stream.
25  	 * @throws IOException if the stream could not be read.
26  	 */
27  	protected String readStreamContentAsString(final InputStream is) throws IOException {
28  		String content;
29  		final StringBuffer contentBuffer = new StringBuffer(BUFFER_SIZE);
30  		final BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
31  		String line = reader.readLine();
32  		while(line != null ) {
33  			contentBuffer.append(line).append("\n");
34  			line = reader.readLine();
35  		}
36  		content = contentBuffer.toString();
37  		return content;
38  	}
39  }