java.io.Reader#read ( )源码实例Demo

下面列出了java.io.Reader#read ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: appinventor-extensions   文件: CommandUtil.java
public static String convertStreamToStr(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 "";
    }
  }
 
源代码2 项目: AndroidBase   文件: IOUtils.java
/**
 * Read characters from an input character stream.
 * This implementation guarantees that it will read as many characters
 * as possible before giving up; this may not always be the case for
 * subclasses of {@link Reader}.
 *
 * @param input  where to read input from
 * @param buffer destination
 * @param offset inital offset into buffer
 * @param length length to read, must be >= 0
 * @return actual length read; may be less than requested if EOF was reached
 * @throws IOException if a read error occurs
 * @since 2.2
 */
public static int read(Reader input, char[] buffer, int offset, int length) throws IOException {
    if (length < 0) {
        throw new IllegalArgumentException("Length must not be negative: " + length);
    }
    int remaining = length;
    while (remaining > 0) {
        int location = length - remaining;
        int count = input.read(buffer, offset + location, remaining);
        if (EOF == count) { // EOF
            break;
        }
        remaining -= count;
    }
    return length - remaining;
}
 
源代码3 项目: smart-cache   文件: Utils.java
public static String read(Reader reader, int length) {
    try {
        char[] buffer = new char[length];

        int offset = 0;
        int rest = length;
        int len;
        while ((len = reader.read(buffer, offset, rest)) != -1) {
            rest -= len;
            offset += len;

            if (rest == 0) {
                break;
            }
        }

        return new String(buffer, 0, length - rest);
    } catch (IOException ex) {
        throw new IllegalStateException("read error", ex);
    }
}
 
源代码4 项目: uima-uimaj   文件: FileCompare.java
/**
 * Read a bufferedReader into a string, using the default platform encoding.
 * 
 * @param reader to be read in
 * @param bufSize - size of stream, in bytes.  Size in chars is &le; size in bytes, because
 * chars take 1 or more bytes to encode.
 * @return String The contents of the stream.
 * @throws IOException
 *           Various I/O errors.
 *           
 * TODO: This is duplicated from org.apache.uima.internal.util.FileUtils in the uimaj-core
 * package. We can't have a compile dependency on uimaj-core since that introduces a cycle. Not
 * sure what the best way of handling this is.
 */
public static String reader2String(Reader reader, int bufSize) throws IOException {
  char[] buf = new char[bufSize];
  int read_so_far = 0;
  try {
    while (read_so_far < bufSize) {
      int count = reader.read(buf, read_so_far, bufSize - read_so_far);
      if (0 > count) {
        break;
      }
      read_so_far += count;
    }
  } finally {
    reader.close();
  }
  return new String(buf, 0, read_so_far);
}
 
源代码5 项目: orion.server   文件: GitTest.java
private static String toString(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();
	}
	return "";
}
 
源代码6 项目: memoir   文件: IOUtils.java
/**
 * Skip characters from an input character stream.
 * This implementation guarantees that it will read as many characters
 * as possible before giving up; this may not always be the case for
 * subclasses of {@link Reader}.
 *
 * @param input  character stream to skip
 * @param toSkip number of characters to skip.
 * @return number of characters actually skipped.
 * @throws IOException              if there is a problem reading the file
 * @throws IllegalArgumentException if toSkip is negative
 * @see Reader#skip(long)
 * @since 2.0
 */
public static long skip(Reader input, long toSkip) throws IOException {
    if (toSkip < 0) {
        throw new IllegalArgumentException("Skip count must be non-negative, actual: " + toSkip);
    }
    /*
     * N.B. no need to synchronize this because: - we don't care if the buffer is created multiple times (the data
     * is ignored) - we always use the same size buffer, so if it it is recreated it will still be OK (if the buffer
     * size were variable, we would need to synch. to ensure some other thread did not create a smaller one)
     */
    if (SKIP_CHAR_BUFFER == null) {
        SKIP_CHAR_BUFFER = new char[SKIP_BUFFER_SIZE];
    }
    long remain = toSkip;
    while (remain > 0) {
        long n = input.read(SKIP_CHAR_BUFFER, 0, (int) Math.min(remain, SKIP_BUFFER_SIZE));
        if (n < 0) { // EOF
            break;
        }
        remain -= n;
    }
    return toSkip - remain;
}
 
源代码7 项目: goclipse   文件: StreamUtil.java
/** Reads and returns all chars from given reader until an EOF is read.
 * Closes reader afterwards. */
public static CharArrayWriter readAllCharsFromReader(Reader reader) throws IOException {
	try {
		final int BUFFER_SIZE = 1024;
		char[] buffer = new char[BUFFER_SIZE];
		CharArrayWriter chars = new CharArrayWriter();
		
		int read;
		while((read = reader.read(buffer)) != EOF) {
			chars.write(buffer, 0, read);
		}
		return chars;
	} finally {
		reader.close();
	}
}
 
源代码8 项目: scriptella-etl   文件: IOUtils.java
/**
 * Loads a reader content into a string.
 *
 * @param reader    reader to load content from. Closed at the end of the operation.
 * @param maxLength max number of characters to read before throwing a Content Too Long Exception.
 * @return string representation of reader content.
 */
public static String toString(final Reader reader, final long maxLength) throws IOException {
    char cb[] = new char[4096];
    StringBuilder sb = new StringBuilder(cb.length);
    long len = 0;

    try {
        for (int n; (n = reader.read(cb)) >= 0;) {
            len += n;
            if (len > maxLength) {
                throw new IOException("Content too long to fit in memory");
            }
            sb.append(cb, 0, n);
        }
    } finally {
        closeSilently(reader);
    }

    return sb.toString();
}
 
源代码9 项目: HTTP-RPC   文件: JSONDecoder.java
private Number readNumber(Reader reader) throws IOException {
    valueBuilder.setLength(0);

    boolean decimal = false;

    while (c != EOF && (Character.isDigit(c) || c == '.' || c == 'e' || c == 'E' || c == '-')) {
        valueBuilder.append((char)c);

        decimal |= (c == '.');

        c = reader.read();
    }

    Number number;
    if (decimal) {
        number = Double.valueOf(valueBuilder.toString());
    } else {
        number = Long.valueOf(valueBuilder.toString());
    }

    return number;
}
 
源代码10 项目: ToolsFinal   文件: IOUtils.java
/**
 * Copy some or all chars from a large (over 2GB) <code>InputStream</code> to an
 * <code>OutputStream</code>, optionally skipping input chars.
 * <p/>
 * This method uses the provided buffer, so there is no need to use a
 * <code>BufferedReader</code>.
 * <p/>
 *
 * @param input       the <code>Reader</code> to read from
 * @param output      the <code>Writer</code> to write to
 * @param inputOffset : number of chars to skip from input before copying
 *                    -ve values are ignored
 * @param length      : number of chars to copy. -ve means all
 * @param buffer      the buffer to be used for the copy
 * @return the number of chars copied
 * @throws NullPointerException if the input or output is null
 * @throws IOException  if an I/O error occurs
 * @since 2.2
 */
public static long copyLarge(Reader input, Writer output, final long inputOffset, final long length, char[] buffer)
        throws IOException {
    if (inputOffset > 0) {
        skipFully(input, inputOffset);
    }
    if (length == 0) {
        return 0;
    }
    int bytesToRead = buffer.length;
    if (length > 0 && length < buffer.length) {
        bytesToRead = (int) length;
    }
    int read;
    long totalRead = 0;
    while (bytesToRead > 0 && EOF != (read = input.read(buffer, 0, bytesToRead))) {
        output.write(buffer, 0, read);
        totalRead += read;
        if (length > 0) { // only adjust length if not reading to the end
            // Note the cast must work because buffer.length is an integer
            bytesToRead = (int) Math.min(length - totalRead, buffer.length);
        }
    }
    return totalRead;
}
 
源代码11 项目: netbeans   文件: EncodingTest.java
/** Compares content of document and reader
 */
private static void compareDoc (Reader r, Document doc) throws Exception {
    for (int i = 0; i < doc.getLength(); i++) {
        String ch = doc.getText (i, 1);
        assertEquals ("Really one char", 1, ch.length());
        
        char fromStream = (char)r.read ();
        if (fromStream != ch.charAt (0) && fromStream == (char)13 && ch.charAt (0) == (char)10) {
            // new line in document is always represented by 13, read next character
            fromStream = (char)r.read ();
        }
        
        
        assertEquals ("Stream and doc should be the same on index " + i, (int)fromStream, (int)ch.charAt (0));
    }
}
 
源代码12 项目: jtransc   文件: Matcher.java
@GwtIncompatible
public void setAll(Reader in) throws IOException {
    char[] mychars = data;
    int free;
    if (mychars == null || shared) {
        mychars = new char[free = 1024];
    } else free = mychars.length;
    int count = 0;
    int c;
    while ((c = in.read(mychars, count, free)) >= 0) {
        free -= c;
        count += c;
        if (free == 0) {
            int newsize = count * 3;
            char[] newchars = new char[newsize];
            System.arraycopy(mychars, 0, newchars, 0, count);
            mychars = newchars;
            free = newsize - count;
        }
    }
    setTarget(mychars, 0, count, false);
}
 
源代码13 项目: stategen   文件: IOHelpers.java
public static void copy(Reader reader, Writer writer) {
    char[] buf = new char[8192];
    int n = 0;
    try {
        while ((n = reader.read(buf)) != -1) {
            writer.write(buf, 0, n);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
源代码14 项目: AndroidDemo   文件: DiskLruCache.java
/**
 * Returns the remainder of 'reader' as a string, closing it when done.
 */
public static String readFully(Reader reader) throws IOException {
    try {
        StringWriter writer = new StringWriter();
        char[] buffer = new char[1024];
        int count;
        while ((count = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, count);
        }
        return writer.toString();
    } finally {
        reader.close();
    }
}
 
源代码15 项目: hawkular-alerts   文件: TokenReplacingReader.java
private String readAll(Reader r) throws IOException {
    int c;
    StringBuilder bld = new StringBuilder();
    while((c = r.read()) >= 0) {
        bld.append((char)c);
    }

    return bld.toString();
}
 
源代码16 项目: cacheonix-core   文件: TextType.java
public Object get(ResultSet rs, String name) throws HibernateException, SQLException {

			// Retrieve the value of the designated column in the current row of this
			// ResultSet object as a java.io.Reader object
			Reader charReader = rs.getCharacterStream(name);

			// if the corresponding SQL value is NULL, the reader we got is NULL as well
			if (charReader==null) return null;

			// Fetch Reader content up to the end - and put characters in a StringBuffer
			StringBuffer sb = new StringBuffer();
			try {
				char[] buffer = new char[2048];
				while (true) {
					int amountRead = charReader.read(buffer, 0, buffer.length);
					if ( amountRead == -1 ) break;
					sb.append(buffer, 0, amountRead);
				}
			}
			catch (IOException ioe) {
				throw new HibernateException( "IOException occurred reading text", ioe );
			}
			finally {
				try {
					charReader.close();
				}
				catch (IOException e) {
					throw new HibernateException( "IOException occurred closing stream", e );
				}
			}

			// Return StringBuffer content as a large String
			return sb.toString();
	}
 
源代码17 项目: j2objc   文件: OldAndroidBufferedReaderTest.java
public static String read(Reader a) throws IOException {
    int r;
    StringBuilder builder = new StringBuilder();
    do {
        r = a.read();
        if (r != -1)
            builder.append((char) r);
    } while (r != -1);
    return builder.toString();
}
 
源代码18 项目: gemfirexd-oss   文件: OffHeapTest.java
public static String convertClobToString(Clob clob) throws IOException, 
SQLException{
  Reader reader = clob.getCharacterStream();
  StringBuilder sb = new StringBuilder();
  int ch;
  while( (ch = reader.read()) != -1) {
    sb.append((char)ch);
  }
  return sb.toString();
}
 
源代码19 项目: spring4-understanding   文件: XmlBeansMarshaller.java
@Override
public int read() throws IOException {
	Reader rdr = getReader();
	return (rdr != null ? rdr.read() : -1);
}
 
源代码20 项目: HTTP-RPC   文件: CSVDecoder.java
private void readValues(Reader reader, ArrayList<String> values, char delimiter) throws IOException {
    int c = reader.read();

    while (c != '\r' && c != '\n' && c != EOF) {
        valueBuilder.setLength(0);

        boolean quoted = false;

        if (c == '"') {
            quoted = true;

            c = reader.read();
        }

        while ((quoted || (c != delimiter && c != '\r' && c != '\n')) && c != EOF) {
            valueBuilder.append((char)c);

            c = reader.read();

            if (c == '"') {
                c = reader.read();

                if (c != '"') {
                    quoted = false;
                }
            }
        }

        if (quoted) {
            throw new IOException("Unterminated quoted value.");
        }

        values.add(valueBuilder.toString());

        if (c == delimiter) {
            c = reader.read();
        }
    }

    if (c == '\r') {
        c = reader.read();

        if (c != '\n') {
            throw new IOException("Improperly terminated record.");
        }
    }
}