Java Code Examples for java.io.Reader

The following are top voted examples for showing how to use java.io.Reader. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to product more good examples.

Example 1
Project: Telecom8   File: JSONTokener.java View source code 7 votes vote down vote up
/**
 * Construct a JSONTokener from a reader.
 *
 * @param reader     A reader.
 */
public JSONTokener(Reader reader) {
    this.reader = reader.markSupported() ? 
    		reader : new BufferedReader(reader);
    this.eof = false;
    this.usePrevious = false;
    this.previous = 0;
    this.index = 0;
    this.character = 1;
    this.line = 1;
}
 
Example 2
Project: jentrata-msh   File: MessageListenerJMSClient.java View source code 6 votes vote down vote up
private String convertStreamToString(InputStream is) throws IOException {
	if (is != null) {
		Writer writer = new StringWriter();

		char[] buffer = new char[1024];
		try {
			Reader reader = new BufferedReader(new InputStreamReader(is,
					"UTF-8"));
			int n;
			while ((n = reader.read(buffer)) != -1) {
				writer.write(buffer, 0, n);
			}
		} finally {
			is.close();
		}
		return writer.toString();
	} else {
		return "";
	}
}
 
Example 3
Project: apache-james-mime4j-groundhog   File: StringTextBody.java View source code 6 votes vote down vote up
@Override
public void writeTo(OutputStream out) throws IOException {
    if (out == null)
        throw new IllegalArgumentException();

    Reader reader = new StringReader(text);
    Writer writer = new OutputStreamWriter(out, charset);

    char buffer[] = new char[1024];
    while (true) {
        int nChars = reader.read(buffer);
        if (nChars == -1)
            break;

        writer.write(buffer, 0, nChars);
    }

    reader.close();
    writer.flush();
}
 
Example 4
Project: JerseyTest   File: Stax2JsonFactory.java View source code 6 votes vote down vote up
private static Reader ensureNonEmptyReader(Reader reader) throws XMLStreamException {
    try {
        Reader mr = reader.markSupported() ? reader : new BufferedReader(reader);
        mr.mark(1);
        if (mr.read() == -1) {
            throw new XMLStreamException("JSON expression can not be empty!");
        }
        mr.reset();
        return mr;
    } catch (IOException ex) {
        throw new XMLStreamException(ex);
    }
}
 
Example 5
Project: resin   File: OracleOciLob.java View source code 6 votes vote down vote up
/**
 * Exports LOB's contents to a file
 */
public boolean export(Env env,
                      Path file,
                      @Optional("0") long start,
                      @Optional("-1") long length)
{
  try {

    WriteStream writeStream = file.openWrite();

    if (_lob instanceof Blob) {
      Blob blob = (Blob) _lob;
      InputStream is = blob.getBinaryStream();
      is.skip(start);
      writeStream.writeStream(is);
      is.close();
    } else if (_lob instanceof Clob) {
      Clob clob = (Clob) _lob;
      Reader reader = clob.getCharacterStream();
      reader.skip(start);
      writeStream.writeStream(reader);
      reader.close();
    } else {
      writeStream.close();
      return false;
    }

    writeStream.close();
    return true;

  } catch (Exception ex) {
    log.log(Level.FINE, ex.toString(), ex);
    return false;
  }
}
 
Example 6
Project: raml-tester   File: IoUtils.java View source code 6 votes vote down vote up
public static String readIntoString(Reader reader) throws IOException {
    if (reader == null) {
        return null;
    }
    try {
        final StringBuilder sb = new StringBuilder();
        final char[] buf = new char[1000];
        while (reader.ready()) {
            final int read = reader.read(buf);
            sb.append(buf, 0, read);
        }
        return sb.toString();
    } finally {
        reader.close();
    }
}
 
Example 7
Project: externals   File: DOMConfigurator.java View source code 6 votes vote down vote up
/**
   Configure log4j by reading in a log4j.dtd compliant XML
   configuration file.

*/
public
void doConfigure(final Reader reader, LoggerRepository repository) 
                                        throws FactoryConfigurationError {
    ParseAction action = new ParseAction() {
        public Document parse(final DocumentBuilder parser) throws SAXException, IOException {
            InputSource inputSource = new InputSource(reader);
            inputSource.setSystemId("dummy://log4j.dtd");
            return parser.parse(inputSource);
        }
        public String toString() { 
            return "reader [" + reader.toString() + "]"; 
        }
    };
  doConfigure(action, repository);
}
 
Example 8
Project: callback-blackberry   File: Tokenizer.java View source code 6 votes vote down vote up
/**
     * Constructor.
     * @param reader The reader from which the JSON string is read.  Same as Tokenizer(reader, false);
     * 
     * @throws IOException Thrown on IOErrors such as invalid JSON or sudden reader closures.
     */
    public Tokenizer(Reader reader) throws IOException {
        super();

        Class readerClass= reader.getClass();
        //In-memory readers don't need to be buffered.  Also, skip PushbackReaders
        //because they probably already wrap a buffered stream.  And lastly, anything
        //that extends from a BufferedReader also doesn't need buffering!
//        if (!StringReader.class.isAssignableFrom(readerClass) && 
//            !CharArrayReader.class.isAssignableFrom(readerClass) &&
//            !PushbackReader.class.isAssignableFrom(readerClass) &&
//            !BufferedReader.class.isAssignableFrom(readerClass)) {
//            reader = new BufferedReader(reader);
//        }
        this.reader    = reader;
        this.lineNo    = 0;
        this.colNo     = 0;
        this.lastChar  = '\n';
        readChar();
    }