Refine search
/** Copy the data from an {@link InputStream} to a string using the specified charset. * @param estimatedSize Used to allocate the output buffer to possibly avoid an array copy. * @param charset May be null to use the platform's default charset. */ public static String copyStreamToString (InputStream input, int estimatedSize, String charset) throws IOException { InputStreamReader reader = charset == null ? new InputStreamReader(input) : new InputStreamReader(input, charset); StringWriter writer = new StringWriter(Math.max(0, estimatedSize)); char[] buffer = new char[DEFAULT_BUFFER_SIZE]; int charsRead; while ((charsRead = reader.read(buffer)) != -1) { writer.write(buffer, 0, charsRead); } return writer.toString(); }
/** Reads the entire file into a string using the specified charset. * @throw RuntimeException if the file handle represents a directory, doesn't exist, or could not be read. */ public String readString (String charset) { StringBuilder output = new StringBuilder(512); InputStreamReader reader = null; try { if (charset == null) reader = new InputStreamReader(read()); else reader = new InputStreamReader(read(), charset); char[] buffer = new char[256]; while (true) { int length = reader.read(buffer); if (length == -1) break; output.append(buffer, 0, length); } } catch (IOException ex) { throw new RuntimeException("Error reading layout file: " + this, ex); } finally { try { if (reader != null) reader.close(); } catch (IOException ignored) { } } return output.toString(); }
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(); }
/** Copy the data from an {@link InputStream} to a string using the specified charset. * @param estimatedSize Used to allocate the output buffer to possibly avoid an array copy. * @param charset May be null to use the platform's default charset. */ public static String copyStreamToString (InputStream input, int estimatedSize, String charset) throws IOException { InputStreamReader reader = charset == null ? new InputStreamReader(input) : new InputStreamReader(input, charset); StringWriter writer = new StringWriter(Math.max(0, estimatedSize)); char[] buffer = new char[DEFAULT_BUFFER_SIZE]; int charsRead; while ((charsRead = reader.read(buffer)) != -1) { writer.write(buffer, 0, charsRead); } return writer.toString(); }
/** Reads the entire file into a string using the specified charset. * @throw RuntimeException if the file handle represents a directory, doesn't exist, or could not be read. */ public String readString (String charset) { StringBuilder output = new StringBuilder(512); InputStreamReader reader = null; try { if (charset == null) reader = new InputStreamReader(read()); else reader = new InputStreamReader(read(), charset); char[] buffer = new char[256]; while (true) { int length = reader.read(buffer); if (length == -1) break; output.append(buffer, 0, length); } } catch (IOException ex) { throw new RuntimeException("Error reading layout file: " + this, ex); } finally { try { if (reader != null) reader.close(); } catch (IOException ignored) { } } return output.toString(); }
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(); }
/** * Copy the contents of the given InputStream into a String. * Leaves the stream open when done. * @param in the InputStream to copy from (may be {@code null} or empty) * @return the String that has been copied to (possibly empty) * @throws IOException in case of I/O errors */ public static String copyToString(@Nullable InputStream in, Charset charset) throws IOException { if (in == null) { return ""; } StringBuilder out = new StringBuilder(); InputStreamReader reader = new InputStreamReader(in, charset); char[] buffer = new char[BUFFER_SIZE]; int bytesRead = -1; while ((bytesRead = reader.read(buffer)) != -1) { out.append(buffer, 0, bytesRead); } return out.toString(); }
public String serialize(InputStream in) { InputStreamReader reader = null; int temp; StringBuilder item = new StringBuilder(); boolean eof = false; try { reader = new InputStreamReader(in); while (!eof) { temp = reader.read(); if (temp == -1) { eof = true; } else { item.append((char) temp); } } } catch (IOException e) { LOG.error("Unable to merge source and patch locations", e); } finally { if (reader != null) { try{ reader.close(); } catch (Throwable e) { LOG.error("Unable to merge source and patch locations", e); } } } return item.toString(); }
InputStreamReader reader = new InputStreamReader( yourInputStream, "windows-1252"); // or what ever seems to be the correct encoding! StringBuilder builder = new StringBuilder(); while (reader.ready()) { builder.append(reader.read()); } reader.close(); String string = builder.toString();
public static String toString(InputStream input) throws IOException { if (input == null) { return null; } final InputStreamReader inputStreamReader = new InputStreamReader(input); final StringBuilder stringBuilder = new StringBuilder(); final char[] buffer = new char[BUFFER_SIZE]; int n = 0; while (EOF != (n = inputStreamReader.read(buffer))) { stringBuilder.append(buffer, 0, n); } return stringBuilder.toString(); }
public String serialize(InputStream in) { InputStreamReader reader = null; int temp; StringBuilder item = new StringBuilder(); boolean eof = false; try { reader = new InputStreamReader(in); while (!eof) { temp = reader.read(); if (temp == -1) { eof = true; } else { item.append((char) temp); } } } catch (IOException e) { LOG.error("Unable to merge source and patch locations", e); } finally { if (reader != null) { try{ reader.close(); } catch (Throwable e) { LOG.error("Unable to merge source and patch locations", e); } } } return item.toString(); }
/** * Copy the contents of the given InputStream into a String. * Leaves the stream open when done. * @param in the InputStream to copy from (may be {@code null} or empty) * @return the String that has been copied to (possibly empty) * @throws IOException in case of I/O errors */ public static String copyToString(@Nullable InputStream in, Charset charset) throws IOException { if (in == null) { return ""; } StringBuilder out = new StringBuilder(); InputStreamReader reader = new InputStreamReader(in, charset); char[] buffer = new char[BUFFER_SIZE]; int bytesRead = -1; while ((bytesRead = reader.read(buffer)) != -1) { out.append(buffer, 0, bytesRead); } return out.toString(); }
private String readInputStream(InputStream is, int size, int messageNumber) throws IOException { InputStreamReader reader = new InputStreamReader(is); try { char[] buffer; if (size > 0) { buffer = new char[size]; } else { buffer = new char[1024]; } int count; StringBuilder builder = new StringBuilder(); while ((count = reader.read(buffer)) != -1) { builder.append(buffer, 0, count); if (size > 0) break; } return builder.toString(); } catch (IOException ioe) { return createDefaultMessage(messageNumber); } finally { reader.close(); } }
/** * Checks whether an Alluxio service is running. * * @param className class name of the Alluxio service * @return whether the Alluxio service is running */ public static boolean isAlluxioRunning(String className) { String[] command = {"bash", "-c", "ps -Aww -o command | grep -i \"[j]ava\" | grep " + className}; try { Process p = Runtime.getRuntime().exec(command); try (InputStreamReader input = new InputStreamReader(p.getInputStream())) { if (input.read() >= 0) { return true; } } return false; } catch (IOException e) { System.err.format("Unable to check Alluxio status: %s.%n", e.getMessage()); return false; } }
public static String file2String(File file, String encoding) { InputStreamReader reader = null; StringWriter writer = new StringWriter(); try { if (encoding == null || "".equals(encoding.trim())) { reader = new InputStreamReader(new FileInputStream(file)); } else { reader = new InputStreamReader(new FileInputStream(file), encoding); } // 将输入流写入输出流 char[] buffer = new char[BUFF_SIZE]; int n = 0; while (-1 != (n = reader.read(buffer))) { writer.write(buffer, 0, n); } } catch (Exception e) { e.printStackTrace(); return null; } finally { if (reader != null) try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } return writer.toString(); }
/** * Copy the contents of the given InputStream into a String. * Leaves the stream open when done. * @param in the InputStream to copy from * @param charset charset * @return the String that has been copied to * @throws IOException in case of I/O errors */ public static String copyToString(InputStream in, Charset charset) throws IOException { StringBuilder out = new StringBuilder(); InputStreamReader reader = new InputStreamReader(in, charset); char[] buffer = new char[BUFFER_SIZE]; int bytesRead = -1; while ((bytesRead = reader.read(buffer)) != -1) { out.append(buffer, 0, bytesRead); } return out.toString(); }