Example usage for java.io Reader read

List of usage examples for java.io Reader read

Introduction

In this page you can find the example usage for java.io Reader read.

Prototype

public abstract int read(char cbuf[], int off, int len) throws IOException;

Source Link

Document

Reads characters into a portion of an array.

Usage

From source file:dk.dr.radio.net.Diverse.java

    sStreng(InputStream is) throws IOException, UnsupportedEncodingException {

  // Det kan vre ndvendigt at hoppe over BOM mark - se http://android.forums.wordpress.org/topic/xml-pull-error?replies=2
  //is.read(); is.read(); is.read(); // - dette virker kun hvis der ALTID er en BOM
  // Hop over BOM - hvis den er der!
  is = new BufferedInputStream(is);  // bl.a. FileInputStream understtter ikke mark, s brug BufferedInputStream
  is.mark(1); // vi har faktisk kun brug for at sge n byte tilbage
  if (is.read() == 0xef) {
    is.read();//from ww  w  .  jav a 2 s  . c o m
    is.read();
  } // Der var en BOM! Ls de sidste 2 byte
  else is.reset(); // Der var ingen BOM - hop tilbage til start


  final char[] buffer = new char[0x3000];
  StringBuilder out = new StringBuilder();
  Reader in = new InputStreamReader(is, "UTF-8");
  int read;
  do {
    read = in.read(buffer, 0, buffer.length);
    if (read > 0) {
      out.append(buffer, 0, read);
    }
  } while (read >= 0);
  in.close();
  return out.toString();
}

From source file:com.subgraph.vega.internal.analysis.urls.HtmlUrlExtractor.java

private String inputStreamToString(InputStream in) throws IOException {
    final Reader r = new InputStreamReader(in, "UTF-8");
    final StringWriter w = new StringWriter();
    final char[] buffer = new char[8192];
    while (true) {
        int n = r.read(buffer, 0, buffer.length);
        if (n <= 0)
            return w.toString();
        w.write(buffer, 0, n);/*w w w . j  a  v  a 2s .c  o m*/
    }
}

From source file:com.shafiq.mytwittle.URLFetch.java

public String inputStreamToString(InputStream inputStream) {
    final char[] buffer = new char[0x10000];
    StringBuilder out = new StringBuilder();
    Reader in;
    try {/*from  w  w  w  .j ava 2s .  c  o  m*/
        in = new InputStreamReader(inputStream, "UTF-8");
        int read;
        do {
            read = in.read(buffer, 0, buffer.length);
            if (read > 0) {
                out.append(buffer, 0, read);
            }
        } while (read >= 0);

        return out.toString();
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

From source file:dk.dr.radio.data.JsonIndlaesning.java

    sInputStreamSomStreng(InputStream is) throws IOException, UnsupportedEncodingException {

  // Det kan vre ndvendigt at hoppe over BOM mark - se http://android.forums.wordpress.org/topic/xml-pull-error?replies=2
  //is.read(); is.read(); is.read(); // - dette virker kun hvis der ALTID er en BOM
  // Hop over BOM - hvis den er der!
  is = new BufferedInputStream(is);  // bl.a. FileInputStream understtter ikke mark, s brug BufferedInputStream
  is.mark(1); // vi har faktisk kun brug for at sge n byte tilbage
  if (is.read() == 0xef) {
    is.read();//from   w ww  . j  av  a  2s . c om
    is.read();
  } // Der var en BOM! Ls de sidste 2 byte
  else is.reset(); // Der var ingen BOM - hop tilbage til start


  final char[] buffer = new char[0x3000];
  StringBuilder out = new StringBuilder();
  Reader in = new InputStreamReader(is, "UTF-8");
  int read;
  do {
    read = in.read(buffer, 0, buffer.length);
    if (read > 0) {
      out.append(buffer, 0, read);
    }
  } while (read >= 0);
  in.close();
  return out.toString();
}

From source file:hudson.util.TextFile.java

/**
 * Reads the first N characters or until we hit EOF.
 *//*from w  w w. j a  v a 2s .  c  o  m*/
public @Nonnull String head(int numChars) throws IOException {
    char[] buf = new char[numChars];
    int read = 0;
    Reader r = new FileReader(file);

    try {
        while (read < numChars) {
            int d = r.read(buf, read, buf.length - read);
            if (d < 0)
                break;
            read += d;
        }

        return new String(buf, 0, read);
    } finally {
        org.apache.commons.io.IOUtils.closeQuietly(r);
    }
}

From source file:com.squid.core.jdbc.formatter.DefaultJDBCDataFormatter.java

protected String read(Clob c) throws SQLException, IOException {
    StringBuffer sb = new StringBuffer((int) c.length());
    Reader r = c.getCharacterStream();
    char[] cbuf = new char[2048];
    int n = 0;//from   www.j a v  a  2  s.c o  m
    while ((n = r.read(cbuf, 0, cbuf.length)) != -1) {
        if (n > 0) {
            sb.append(cbuf, 0, n);
        }
    }
    return sb.toString();
}

From source file:com.allogy.mime.MimeGeneratingInputStreamTest.java

@Test
public void stream_should_return_headers_split_by_CRLF_ending_with_dual_CRLF_followed_by_body()
        throws IOException {
    InputStream stream = createObjectUnderTest();

    Reader reader = new InputStreamReader(stream);

    for (Header header : headers) {
        String headerString = header.toString();
        char[] streamCharacters = new char[headerString.length()];

        int readStatus = reader.read(streamCharacters, 0, headerString.length());

        assertThat(readStatus, not(-1));
        assertThat(streamCharacters, is(headerString.toCharArray()));
        assertThat(reader.read(), is(MimeUtilities.CarriageReturnCharacter));
        assertThat(reader.read(), is(MimeUtilities.LineFeedCharacter));
    }/*from w  ww .  j a  va2  s. c  om*/

    assertThat(reader.read(), is(MimeUtilities.CarriageReturnCharacter));
    assertThat(reader.read(), is(MimeUtilities.LineFeedCharacter));

    char[] streamCharacters = new char[body.length()];
    int readStatus = reader.read(streamCharacters, 0, body.length());

    assertThat(readStatus, not(-1));
    assertThat(streamCharacters, is(body.toCharArray()));

    assertThat(stream.read(), is(-1));
}

From source file:com.github.jknack.handlebars.io.URLTemplateSource.java

@Override
public String content() throws IOException {
    Reader reader = null;
    final int bufferSize = 1024;
    try {//from  ww  w.jav  a  2  s . co  m
        reader = reader();
        char[] cbuf = new char[bufferSize];
        StringBuilder sb = new StringBuilder(bufferSize);
        int len;
        while ((len = reader.read(cbuf, 0, bufferSize)) != -1) {
            sb.append(cbuf, 0, len);
        }
        return sb.toString();
    } finally {
        if (reader != null) {
            reader.close();
        }
    }
}

From source file:com.eclipsesource.tabris.demos.entrypoints.GeolocationDemo.java

protected String readStream(InputStream stream) {
    final char[] buffer = new char[0x10000];
    StringBuilder out = new StringBuilder();
    Reader in;
    try {//from  w  ww.  ja v  a 2  s  . co m
        in = new InputStreamReader(stream, "UTF-8");
        int read;
        do {
            read = in.read(buffer, 0, buffer.length);
            if (read > 0) {
                out.append(buffer, 0, read);
            }
        } while (read >= 0);
        return out.toString();
    } catch (Exception shouldNotHappen) {
        throw new IllegalStateException(shouldNotHappen);
    }
}

From source file:my.FileBasedTestCase.java

/** Assert that the content of a file is equal to that in a char[]. */
protected void assertEqualContent(char[] c0, File file) throws IOException {
    Reader ir = new java.io.FileReader(file);
    int count = 0, numRead = 0;
    char[] c1 = new char[c0.length];
    try {//  w  w w. j  av  a 2s  . c o m
        while (count < c0.length && numRead >= 0) {
            numRead = ir.read(c1, count, c0.length);
            count += numRead;
        }
        assertEquals("Different number of chars: ", c0.length, count);
        for (int i = 0; i < count; i++) {
            assertEquals("char " + i + " differs", c0[i], c1[i]);
        }
    } finally {
        ir.close();
    }
}