Java Code Examples for java.io.InputStreamReader#ready()

The following examples show how to use java.io.InputStreamReader#ready() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: lizzie   File: GIBParser.java    License: GNU General Public License v3.0 6 votes vote down vote up
public static boolean load(String filename) throws IOException {
  // Clear the board
  Lizzie.board.clear();

  File file = new File(filename);
  if (!file.exists() || !file.canRead()) {
    return false;
  }

  String encoding = EncodingDetector.detect(filename);
  FileInputStream fp = new FileInputStream(file);
  InputStreamReader reader = new InputStreamReader(fp, encoding);
  StringBuilder builder = new StringBuilder();
  while (reader.ready()) {
    builder.append((char) reader.read());
  }
  reader.close();
  fp.close();
  String value = builder.toString();
  if (value.isEmpty()) {
    return false;
  }

  boolean returnValue = parse(value);
  return returnValue;
}
 
Example 2
Source Project: keywhiz   File: UpdateAction.java    License: Apache License 2.0 6 votes vote down vote up
@Override public void run() {
  String secretName = config.name;

  if (secretName == null || !validName(secretName)) {
    throw new IllegalArgumentException(format("Invalid name, must match %s", VALID_NAME_PATTERN));
  }

  byte[] content = {};
  if (config.contentProvided) {
    content = readSecretContent();
  }
  partialUpdateSecret(secretName, content, config);

  // If it appears that content was piped in but --content was not specified, print a warning
  if (!config.contentProvided) {
    try {
      InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
      if (reader.ready()) {
        System.out.println("\nWarning: Specify the --content flag to update a secret's content.");
        System.out.println("The secret has not been updated with any provided content.");
      }
    } catch (IOException e) {
      logger.warn("Unexpected error trying to create an InputStreamReader for stdin: '{}'", e.getMessage());
    }
  }
}
 
Example 3
Source Project: bazel   File: CommandHelper.java    License: Apache License 2.0 6 votes vote down vote up
static String execute(String action, List<String> command) throws IOException {
  final StringBuilder processLog = new StringBuilder();

  final Process process = new ProcessBuilder().command(command).redirectErrorStream(true).start();
  processLog.append("Command: ");
  Joiner.on("\\\n\t").appendTo(processLog, command);
  processLog.append("\nOutput:\n");
  final InputStreamReader stdout = new InputStreamReader(process.getInputStream(), UTF_8);
  while (process.isAlive()) {
    processLog.append(CharStreams.toString(stdout));
  }
  // Make sure the full stdout is read.
  while (stdout.ready()) {
    processLog.append(CharStreams.toString(stdout));
  }
  if (process.exitValue() != 0) {
    throw new RuntimeException(String.format("Error during %s:", action) + "\n" + processLog);
  }
  return processLog.toString();
}
 
Example 4
Source Project: consulo   File: ResourceUtil.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull
public static String loadText(@Nonnull URL url) throws IOException {
  InputStream inputStream = new BufferedInputStream(URLUtil.openStream(url));

  InputStreamReader reader = new InputStreamReader(inputStream, ENCODING_UTF_8);
  try {
    StringBuilder text = new StringBuilder();
    char[] buf = new char[5000];
    while (reader.ready()) {
      final int length = reader.read(buf);
      if (length == -1) break;
      text.append(buf, 0, length);
    }
    return text.toString();
  }
  finally {
    reader.close();
  }
}
 
Example 5
Source Project: org.hl7.fhir.core   File: CSVProcessor.java    License: Apache License 2.0 5 votes vote down vote up
private String readSource() throws IOException  {
  StringBuilder s = new StringBuilder();
  InputStreamReader r = new InputStreamReader(source,"UTF-8");
  while (r.ready()) {
    s.append((char) r.read()); 
  }
  r.close();
  return s.toString();
}
 
Example 6
public QueryStream(Response response) {
  this.response = response;

  this.objectMapper = new ObjectMapper();
  InputStreamReader isr = new InputStreamReader(
      (InputStream) response.getEntity(),
      StandardCharsets.UTF_8
  );
  QueryStream stream = this;
  this.responseScanner = new Scanner((buf) -> {
    int wait = 1;
    // poll the input stream's readiness between interruptable sleeps
    // this ensures we cannot block indefinitely on read()
    while (true) {
      if (closed) {
        throw stream.closedIllegalStateException("hasNext()");
      }
      if (isr.ready()) {
        break;
      }
      synchronized (stream) {
        if (closed) {
          throw stream.closedIllegalStateException("hasNext()");
        }
        try {
          wait = java.lang.Math.min(wait * 2, 200);
          stream.wait(wait);
        } catch (InterruptedException e) {
          // this is expected
          // just check the closed flag
        }
      }
    }
    return isr.read(buf);
  });

  this.bufferedRow = null;
}
 
Example 7
Source Project: java-study   File: IoTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 字节流
 * 创建一个文件并读取记录 防止乱码
 * @throws IOException
 */
private static void test4() throws IOException {
	String path="E:/test/hello.txt";
	String path2="E:/test/你好.txt";
	String str="你好!";
	//从文件读取数据
	InputStream input = new FileInputStream(path);
	InputStreamReader reader = new InputStreamReader(input, "UTF-8");
    StringBuffer sb=new StringBuffer();
	while(reader.ready()){
		sb.append((char)reader.read());
	}
	
	input.close();
	reader.close();
	
	//创建一个文件并向文件中写数据
	OutputStream output = new FileOutputStream(path2);
	OutputStreamWriter writer = new OutputStreamWriter(output, "UTF-8");
	writer.write(sb+str);
	
	writer.close();
	output.close();
	
	//从文件读取数据
	InputStream input2 = new FileInputStream(path2);
	InputStreamReader reader2 = new InputStreamReader(input2, "UTF-8");
    StringBuffer sb2=new StringBuffer();
	while(reader2.ready()){
		sb2.append((char)reader2.read());
	}
	System.out.println("输出:"+sb2);
	input2.close();
	reader2.close();
}
 
Example 8
Source Project: lizzie   File: SGFParser.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static boolean load(String filename) throws IOException {
  // Clear the board
  Lizzie.board.clear();

  File file = new File(filename);
  if (!file.exists() || !file.canRead()) {
    return false;
  }

  String encoding = EncodingDetector.detect(filename);
  if (encoding == "WINDOWS-1252") encoding = "gb2312";
  FileInputStream fp = new FileInputStream(file);
  InputStreamReader reader = new InputStreamReader(fp, encoding);
  StringBuilder builder = new StringBuilder();
  while (reader.ready()) {
    builder.append((char) reader.read());
  }
  reader.close();
  fp.close();
  String value = builder.toString();
  if (value.isEmpty()) {
    return false;
  }

  boolean returnValue = parse(value);
  return returnValue;
}
 
Example 9
protected String readResourceAsString(String name) throws IOException {
    try (InputStream stream = getClass().getResourceAsStream(name)) {
        StringBuilder builder = new StringBuilder();
        InputStreamReader reader = new InputStreamReader(stream);
        char[] buf = new char[4096];
        while (reader.ready()) {
            int len = reader.read(buf);
            builder.append(buf, 0, len);
        }
        return builder.toString();
    }
}
 
Example 10
Source Project: JavaCommon   File: FileIODemo.java    License: Apache License 2.0 4 votes vote down vote up
public static void fileStream() {
	try {
		File f = new File("mkdirs/test/filetest.txt");
		FileOutputStream fop = new FileOutputStream(f);
		// 构建FileOutputStream对象,文件不存在会自动新建

		OutputStreamWriter writer = new OutputStreamWriter(fop, "gbk");
		// 构建OutputStreamWriter对象,参数可以指定编码,默认为操作系统默认编码,windows上是gbk

		writer.append("中文输入");
		// 写入到缓冲区

		writer.append("\r\n");
		// 换行

		writer.append("English");
		// 刷新缓存冲,写入到文件,如果下面已经没有写入的内容了,直接close也会写入

		writer.close();
		// 关闭写入流,同时会把缓冲区内容写入文件,所以上面的注释掉

		fop.close();
		// 关闭输出流,释放系统资源

		FileInputStream fip = new FileInputStream(f);
		// 构建FileInputStream对象

		InputStreamReader reader = new InputStreamReader(fip, "UTF-8");
		// 构建InputStreamReader对象,编码与写入相同

		StringBuffer sb = new StringBuffer();
		while (reader.ready()) {
			sb.append((char) reader.read());
			// 转成char加到StringBuffer对象中
		}
		System.out.println(sb.toString());
		reader.close();
		// 关闭读取流

		fip.close();
		// 关闭输入流,释放系统资源
	} catch (Exception e) {
	}
}