java.io.Reader.read()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(7.7k)|赞(0)|评价(0)|浏览(240)

本文整理了Java中java.io.Reader.read方法的一些代码示例,展示了Reader.read的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Reader.read方法的具体详情如下:
包路径:java.io.Reader
类名称:Reader
方法名:read

Reader.read介绍

[英]Reads a single character from this reader and returns it as an integer with the two higher-order bytes set to 0. Returns -1 if the end of the reader has been reached.
[中]从该读取器读取单个字符,并将其作为两个高阶字节设置为0的整数返回。如果已到达读卡器的末端,则返回-1。

代码示例

代码示例来源:origin: apache/incubator-dubbo

/**
 * write.
 *
 * @param reader     Reader.
 * @param writer     Writer.
 * @param bufferSize buffer size.
 * @return count.
 * @throws IOException
 */
public static long write(Reader reader, Writer writer, int bufferSize) throws IOException {
  int read;
  long total = 0;
  char[] buf = new char[BUFFER_SIZE];
  while ((read = reader.read(buf)) != -1) {
    writer.write(buf, 0, read);
    total += read;
  }
  return total;
}

代码示例来源:origin: stackoverflow.com

final int bufferSize = 1024;
final char[] buffer = new char[bufferSize];
final StringBuilder out = new StringBuilder();
Reader in = new InputStreamReader(inputStream, "UTF-8");
for (; ; ) {
  int rsz = in.read(buffer, 0, buffer.length);
  if (rsz < 0)
    break;
  out.append(buffer, 0, rsz);
}
return out.toString();

代码示例来源:origin: stackoverflow.com

private static String getValue(Part part) throws IOException {
  BufferedReader reader = new BufferedReader(new InputStreamReader(part.getInputStream(), "UTF-8"));
  StringBuilder value = new StringBuilder();
  char[] buffer = new char[1024];
  for (int length = 0; (length = reader.read(buffer)) > 0;) {
    value.append(buffer, 0, length);
  }
  return value.toString();
}

代码示例来源:origin: stackoverflow.com

InputStream is = getResources().openRawResource(R.raw.json_file);
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();
}

String jsonString = writer.toString();

代码示例来源:origin: stackoverflow.com

private static String readUrl(String urlString) throws Exception {
  BufferedReader reader = null;
  try {
    URL url = new URL(urlString);
    reader = new BufferedReader(new InputStreamReader(url.openStream()));
    StringBuffer buffer = new StringBuffer();
    int read;
    char[] chars = new char[1024];
    while ((read = reader.read(chars)) != -1)
      buffer.append(chars, 0, read); 

    return buffer.toString();
  } finally {
    if (reader != null)
      reader.close();
  }
}

代码示例来源:origin: Atmosphere/atmosphere

throw new IOException("Read timeout");
} else {
  Reader reader = readers.getFirst();
  int c = reader.read(cbuf, off + count, len - count);
  if (c == -1) {
    synchronized (readers) {
      reader.close();
    } catch (IOException e) {

代码示例来源:origin: stackoverflow.com

URL url = new URL("http://stackoverflow.com/questions/1381617");
URLConnection con = url.openConnection();
Pattern p = Pattern.compile("text/html;\\s+charset=([^\\s]+)\\s*");
Matcher m = p.matcher(con.getContentType());
/* If Content-Type doesn't match this pre-conception, choose default and 
 * hope for the best. */
String charset = m.matches() ? m.group(1) : "ISO-8859-1";
Reader r = new InputStreamReader(con.getInputStream(), charset);
StringBuilder buf = new StringBuilder();
while (true) {
 int ch = r.read();
 if (ch < 0)
  break;
 buf.append((char) ch);
}
String str = buf.toString();

代码示例来源:origin: ltsopensource/light-task-scheduler

private static String getStreamAsString(InputStream stream, String charset) throws IOException {
  try {
    Reader reader = new InputStreamReader(stream, charset);
    StringBuilder response = new StringBuilder();
    final char[] buff = new char[1024];
    int read = 0;
    while ((read = reader.read(buff)) > 0) {
      response.append(buff, 0, read);
    }
    return response.toString();
  } finally {
    if (stream != null) {
      stream.close();
    }
  }
}

代码示例来源:origin: square/okhttp

@Override public int read(char[] cbuf, int off, int len) throws IOException {
 if (closed) throw new IOException("Stream closed");
 Reader delegate = this.delegate;
 if (delegate == null) {
  Charset charset = Util.bomAwareCharset(source, this.charset);
  delegate = this.delegate = new InputStreamReader(source.inputStream(), charset);
 }
 return delegate.read(cbuf, off, len);
}

代码示例来源:origin: neo4j/neo4j

try
  reader = new InputStreamReader( input, StandardCharsets.UTF_8 );
  int read;
  do
    read = reader.read( buffer, 0, buffer.length );
    if ( read > 0 )
      reader.close();

代码示例来源:origin: apache/groovy

public Writer writeTo(final Writer out) throws IOException {
  try (Reader reader = (this.encoding == null)
      ? new InputStreamReader(Files.newInputStream(this))
      : new InputStreamReader(Files.newInputStream(this), Charset.forName(this.encoding))) {
    int c = reader.read();
    while (c != -1) {
      out.write(c);
      c = reader.read();
    }
  }
  return out;
}

代码示例来源:origin: javamelody/javamelody

final InputStream inputStream = zipFile.getInputStream(entry);
try {
  final Reader reader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
  try {
    final char[] chars = new char[1024];
    int read = reader.read(chars);
    while (read != -1) {
      writer.write(chars, 0, read);
      read = reader.read(chars);
    reader.close();
  inputStream.close();

代码示例来源:origin: prestodb/presto

protected boolean _loadMore() throws IOException
{
  final int bufSize = _inputEnd;
  _currInputProcessed += bufSize;
  _currInputRowStart -= bufSize;
  // 26-Nov-2015, tatu: Since name-offset requires it too, must offset
  //   this increase to avoid "moving" name-offset, resulting most likely
  //   in negative value, which is fine as combine value remains unchanged.
  _nameStartOffset -= bufSize;
  if (_reader != null) {
    int count = _reader.read(_inputBuffer, 0, _inputBuffer.length);
    if (count > 0) {
      _inputPtr = 0;
      _inputEnd = count;
      return true;
    }
    // End of input
    _closeInput();
    // Should never return 0, so let's fail
    if (count == 0) {
      throw new IOException("Reader returned 0 characters when trying to read "+_inputEnd);
    }
  }
  return false;
}

代码示例来源:origin: redisson/redisson

/**
 * Compares the content of two character streams.
 *
 * @return <code>true</code> if the content of the first stream is equal
 *         to the content of the second stream.
 */
public static boolean compare(Reader input1, Reader input2) throws IOException {
  if (!(input1 instanceof BufferedReader)) {
    input1 = new BufferedReader(input1);
  }
  if (!(input2 instanceof BufferedReader)) {
    input2 = new BufferedReader(input2);
  }
  int ch = input1.read();
  while (ch != -1) {
    int ch2 = input2.read();
    if (ch != ch2) {
      return false;
    }
    ch = input1.read();
  }
  int ch2 = input2.read();
  return (ch2 == -1);
}

代码示例来源:origin: plutext/docx4j

public void execute () throws IOException {
 if (writer == null) {
  throw new IOException ("Not yet initialized");
 } else {
  int amount = reader.read (buffer);
  if (amount < 0) {
   writer.close ();
  } else {
   writer.write (buffer, 0, amount);
  }
 }
}

代码示例来源:origin: nostra13/Android-Universal-Image-Loader

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();
  }
}

代码示例来源:origin: spring-projects/spring-framework

char[] buffer = new char[BUFFER_SIZE];
int bytesRead = -1;
while ((bytesRead = in.read(buffer)) != -1) {
  out.write(buffer, 0, bytesRead);
  byteCount += bytesRead;
  in.close();

代码示例来源:origin: org.testng/testng

public static String streamToString(InputStream is) throws IOException {
  if (is != null) {
   Writer writer = new StringWriter();

   char[] buffer = new char[1024];
   try (Reader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
    int n;
    while ((n = reader.read(buffer)) != -1) {
     writer.write(buffer, 0, n);
    }
   }
   return writer.toString();
  } else {
   return "";
  }
 }
}

代码示例来源:origin: stackoverflow.com

URL url = new URL("http://example.net/new-message.php");
Map<String,Object> params = new LinkedHashMap<>();
params.put("name", "Freddie the Fish");
conn.getOutputStream().write(postDataBytes);
Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
for (int c; (c = in.read()) >= 0;)
  System.out.print((char)c);

代码示例来源:origin: pmd/pmd

protected void FillBuff() throws java.io.IOException
{
 int i;
 if (maxNextCharInd == 4096)
  maxNextCharInd = nextCharInd = 0;
 try {
  if ((i = inputStream.read(nextCharBuf, maxNextCharInd,
                    4096 - maxNextCharInd)) == -1)
  {
   inputStream.close();
   throw new java.io.IOException();
  }
  else
    maxNextCharInd += i;
  return;
 }
 catch(java.io.IOException e) {
  if (bufpos != 0)
  {
   --bufpos;
   backup(0);
  }
  else
  {
   bufline[bufpos] = line;
   bufcolumn[bufpos] = column;
  }
  throw e;
 }
}

相关文章