List of usage examples for java.io Reader read
public abstract int read(char cbuf[], int off, int len) throws IOException;
From source file:com.google.dart.tools.update.core.internal.UpdateUtils.java
private static String toString(InputStream is) throws IOException { final char[] buffer = new char[0x10000]; StringBuilder out = new StringBuilder(); Reader in = new InputStreamReader(is, "UTF-8"); int read;//from www . j a v a 2s . c om do { read = in.read(buffer, 0, buffer.length); if (read > 0) { out.append(buffer, 0, read); } } while (read >= 0); return out.toString(); }
From source file:CSVWriter.java
private static 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 a2 s . c om*/ while ((n = r.read(cbuf, 0, cbuf.length)) != -1) { if (n > 0) { sb.append(cbuf, 0, n); } } return sb.toString(); }
From source file:CSVWriter.java
private static String read(Clob c) throws SQLException, IOException { StringBuilder sb = new StringBuilder((int) c.length()); Reader r = c.getCharacterStream(); char[] cbuf = new char[CLOBBUFFERSIZE]; int n;/*w ww . j a va 2 s . c o m*/ while ((n = r.read(cbuf, 0, cbuf.length)) != -1) { sb.append(cbuf, 0, n); } return sb.toString(); }
From source file:com.hazelcast.stabilizer.Utils.java
public static String fileAsText(File file) { try {/*from w ww. j av a 2 s .co m*/ FileInputStream stream = new FileInputStream(file); try { Reader reader = new BufferedReader(new InputStreamReader(stream)); StringBuilder builder = new StringBuilder(); char[] buffer = new char[8192]; int read; while ((read = reader.read(buffer, 0, buffer.length)) > 0) { builder.append(buffer, 0, read); } return builder.toString(); } finally { closeQuietly(stream); } } catch (IOException e) { throw new RuntimeException(e); } }
From source file:Main.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}.//from ww w . ja v a2 s . c o m * * @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; }
From source file:gate.DocumentFormat.java
/** Performs magic over Gate Document */ protected static MimeType runMagicNumbers(Reader aReader) { // No reader, nothing to detect if (aReader == null) return null; // Prepare to run the magic stuff String strBuffer = null;//from w w w . j a v a 2 s . c om int bufferSize = 2048; int charReads = 0; char[] cbuf = new char[bufferSize]; try { charReads = aReader.read(cbuf, 0, bufferSize); } catch (IOException e) { return null; } // End try if (charReads == -1) // the document is empty return null; // Create a string form the buffer and perform some search on it. strBuffer = new String(cbuf, 0, charReads); // If this fails then surrender return getTypeFromContent(strBuffer); }
From source file:Main.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}./* w w w . j av a 2 s . c o m*/ * * @param input character stream to skip * @param toSkip number of characters to skip. * @return number of characters actually skipped. * * @see Reader#skip(long) * * @throws IOException if there is a problem reading the file * @throws IllegalArgumentException if toSkip is negative * @since Commons IO 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; }
From source file:com.github.codingtogenomic.CodingToGenomic.java
private static String getContent(final String endpoint) throws MalformedURLException, IOException, InterruptedException { if (requestCount == 15) { // check every 15 final long currentTime = System.currentTimeMillis(); final long diff = currentTime - lastRequestTime; //if less than a second then sleep for the remainder of the second if (diff < 1000) { Thread.sleep(1000 - diff); }/* w w w . j a va 2s. co m*/ //reset lastRequestTime = System.currentTimeMillis(); requestCount = 0; } final URL url = new URL(SERVER + endpoint); URLConnection connection = url.openConnection(); HttpURLConnection httpConnection = (HttpURLConnection) connection; httpConnection.setRequestProperty("Content-Type", "application/json"); final InputStream response = httpConnection.getInputStream(); int responseCode = httpConnection.getResponseCode(); if (responseCode != 200) { if (responseCode == 429 && httpConnection.getHeaderField("Retry-After") != null) { double sleepFloatingPoint = Double.valueOf(httpConnection.getHeaderField("Retry-After")); double sleepMillis = 1000 * sleepFloatingPoint; Thread.sleep((long) sleepMillis); return getContent(endpoint); } throw new RuntimeException("Response code was not 200. Detected response was " + responseCode); } String output; Reader reader = null; try { reader = new BufferedReader(new InputStreamReader(response, "UTF-8")); StringBuilder builder = new StringBuilder(); char[] buffer = new char[8192]; int read; while ((read = reader.read(buffer, 0, buffer.length)) > 0) { builder.append(buffer, 0, read); } output = builder.toString(); } finally { if (reader != null) { try { reader.close(); } catch (IOException logOrIgnore) { logOrIgnore.printStackTrace(); } } } return output; }
From source file:fm.lastify.config.LastifyFmWebappConfig.java
/** * Used to configure the in-memory HSQLDB database Remove this method if * different datasource is used//w ww .j a v a 2 s . c o m * * @throws IOException */ @PostConstruct public void createDatabaseTable() throws IOException { Resource resource = new ClassPathResource( "org/springframework/social/connect/jdbc/JdbcUsersConnectionRepository.sql"); BufferedInputStream is = new BufferedInputStream(resource.getInputStream()); final char[] buffer = new char[0x10000]; StringBuilder out = new StringBuilder(); Reader in = new InputStreamReader(resource.getInputStream(), "UTF-8"); int read; do { read = in.read(buffer, 0, buffer.length); if (read > 0) { out.append(buffer, 0, read); } } while (read >= 0); JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); jdbcTemplate.execute(out.toString()); }
From source file:FileTransferHandler.java
/** * If the wrapped handler can import strings and the specified Transferable * can provide its data as a List of File objects, then we read the files, and * pass their contents as a string to the wrapped handler. Otherwise, we offer * the Transferable to the wrapped handler to handle on its own. *//*from w ww . j a va 2s. c o m*/ public boolean importData(JComponent c, Transferable t) { // See if we're offered a java.util.List of java.io.File objects. // We handle this case first because the Transferable is likely to // also offer the filenames as strings, and we want to import the // file contents, not their names! if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor) && wrappedHandler.canImport(c, stringFlavorArray)) { try { List filelist = (List) t.getTransferData(DataFlavor.javaFileListFlavor); // Loop through the files to determine total size int numfiles = filelist.size(); int numbytes = 0; for (int i = 0; i < numfiles; i++) { File f = (File) filelist.get(i); numbytes += (int) f.length(); } // There will never be more characters than bytes in the files char[] text = new char[numbytes]; // to hold file contents int p = 0; // current position in the text[] array // Loop through the files again, reading their content as text for (int i = 0; i < numfiles; i++) { File f = (File) filelist.get(i); Reader r = new BufferedReader(new FileReader(f)); p += r.read(text, p, (int) f.length()); } // Convert the character array to a string and wrap it // in a pre-defined Transferable class for transferring strings StringSelection selection = new StringSelection(new String(text, 0, p)); // Ask the wrapped handler to import the string return wrappedHandler.importData(c, selection); } // If anything goes wrong, just beep to tell the user catch (UnsupportedFlavorException e) { c.getToolkit().beep(); // audible error return false; // return failure code } catch (IOException e) { c.getToolkit().beep(); // audible error return false; // return failure code } } // Otherwise let the wrapped class handle this transferable itself return wrappedHandler.importData(c, t); }