List of usage examples for java.io Reader read
public int read() throws IOException
From source file:jcurl.core.io.OldConfigReader.java
/** * Read a single token./*from ww w.j a v a2 s .c om*/ * * @param read * @return - * @throws IOException */ static String readToken(final Reader read) throws IOException { final StringBuffer ret = new StringBuffer(); final int pre = 1; final int content = 2; final int comment = 3; char sep = '-'; int state = pre; for (;;) { int ch = read.read(); switch (state) { case pre: if (ch == -1) { log.debug("token=[null]"); return null; } if (Character.isWhitespace((char) ch)) continue; if ('#' == (char) ch) state = comment; else { ret.setLength(0); if ('"' == (char) ch) sep = '"'; else { sep = ' '; ret.append((char) ch); } state = content; } break; case content: final String s0 = ret.toString().trim(); if (ch == -1) { log.debug("token=[" + s0 + "]"); return s0; } if (sep == ' ' && Character.isWhitespace((char) ch)) { log.debug("token=[" + s0 + "]"); return s0; } if (sep == '"' && '"' == (char) ch) { log.debug("token=[" + s0 + "]"); return s0; } if (sep == ' ' && '#' == (char) ch) state = comment; else ret.append((char) ch); break; case comment: final String s = ret.toString().trim(); if (ch == -1) { log.debug("token=[" + s + "]"); return s; } if ('\n' == (char) ch || '\r' == (char) ch) { if (s.length() == 0) state = pre; else { log.debug("token=[" + s + "]"); return s; } } break; } } }
From source file:net.sf.jabref.MetaData.java
/** * Reads the next unit. Units are delimited by ';'. *//*from w ww. j a va2s . co m*/ private static Optional<String> getNextUnit(Reader reader) throws IOException { int c; boolean escape = false; StringBuilder res = new StringBuilder(); while ((c = reader.read()) != -1) { if (escape) { res.append((char) c); escape = false; } else if (c == '\\') { escape = true; } else if (c == ';') { break; } else { res.append((char) c); } } if (res.length() > 0) { return Optional.of(res.toString()); } return Optional.empty(); }
From source file:Main.java
public static boolean hasString(Reader reader, String string, char c) throws IOException { if (DEBUG)// w ww . ja v a 2s. c o m System.out.println("XMLParserUtilities.hasString( " + reader + ", " + string + ", " + c + ")"); char[] chars = string.toCharArray(); for (int i = 0; i < chars.length; i++) { if (DEBUG) System.out.print(c); if (chars[i] != c) { if (DEBUG) System.out.println("XMLParserUtilities.hasString() [false]"); return false; } if ((i + 1) < chars.length) { c = (char) reader.read(); } } if (DEBUG) System.out.println("XMLParserUtilities.hasString() [true]"); return true; }
From source file:com.github.magicsky.sya.checkers.TestSourceReader.java
/** * Searches for the offset of the first occurrence of a string in a workspace file. * * @param lookfor string to be searched for * @param fullPath full path of the workspace file * @return the offset or -1/*from ww w . jav a 2s . c o m*/ * @throws Exception * @throws UnsupportedEncodingException * @since 4.0 */ public static int indexOfInFile(String lookfor, Path fullPath) throws Exception { IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(fullPath); Reader reader = new BufferedReader(new InputStreamReader(file.getContents(), file.getCharset())); Assert.assertTrue(lookfor.indexOf('\n') == -1); try { int c = 0; int offset = 0; StringBuilder buf = new StringBuilder(); while ((c = reader.read()) >= 0) { buf.append((char) c); if (c == '\n') { int idx = buf.indexOf(lookfor); if (idx >= 0) { return idx + offset; } offset += buf.length(); buf.setLength(0); } } int idx = buf.indexOf(lookfor); if (idx >= 0) { return idx + offset; } return -1; } finally { reader.close(); } }
From source file:Main.java
/** * Retrieve the name of the first tag in the XML document specified by the * given Reader, without parsing the full file/string. This function is * useful to identify the DocType of an XML document before parsing, * possibly to send the document off to different pieces of code. * For performance reasons, the function attempts to read as little of * the file or string as possible before making its decision about the * first tag. Leading comments are ignored. * @param xml a Reader containing an XML document. * @return the first tag name, as a String, or null if no first tag * can be found./*from ww w.j a va2s . c om*/ */ public static String getFirstTagName(Reader xml) { final int OUTSIDE = 0; // constant: identify outside state final int BRACKET = 1; // constant: bracket, contents unknown final int COMMENT = 2; // constant: identify a comment section final int IGNORE = 3; // constant: identify an ignored section final int TAG = 4; // constant: identify a tag section int state = OUTSIDE; String commentMatch = null; StringBuffer tagBuffer = null; boolean sawBang = false; try { int c = xml.read(); for (;;) { // No tag found if we hit EOF first. if (c == -1) return null; switch (state) { case OUTSIDE: // Start of any sort of tag if (c == '<') { state = BRACKET; commentMatch = "!--"; sawBang = false; c = xml.read(); } // Other non-whitespace characters outside of any tag else if (!Character.isWhitespace((char) c)) return null; // Whitespace characters are ignored else c = xml.read(); break; case BRACKET: // Check for the start of a comment. if (commentMatch != null) { if (c == commentMatch.charAt(0)) { // This match indicates a comment if (commentMatch.length() == 1) { c = xml.read(); commentMatch = "-->"; state = COMMENT; } else { // Remove the first character from commentMatch, // then process the character as usual. commentMatch = commentMatch.substring(1, commentMatch.length()); } } else // No longer eligible for comment. commentMatch = null; } // Hit whitespace; ignore the character. if (Character.isWhitespace((char) c)) { c = xml.read(); break; } switch (c) { case '?': c = xml.read(); state = IGNORE; break; case '!': // Enter an ignored section unless eligible for comment. c = xml.read(); sawBang = true; if (commentMatch == null) state = IGNORE; break; case '-': // Enter an ignored section unless eligible for comment. c = xml.read(); if (commentMatch == null) state = IGNORE; break; case '>': // Return to OUTSIDE state immediately c = xml.read(); state = OUTSIDE; break; default: // State depends on whether we saw a ! or not. if (sawBang) state = IGNORE; else state = TAG; tagBuffer = new StringBuffer(); } break; case COMMENT: // Did we match the next expected end-of-comment character? if (c == commentMatch.charAt(0)) { c = xml.read(); if (commentMatch.length() == 1) // Done with the comment state = OUTSIDE; else commentMatch = commentMatch.substring(1, commentMatch.length()); } // If not, restart our quest for the end-of-comment character. else { c = xml.read(); commentMatch = "-->"; } break; case IGNORE: // Drop out on a close >. Ignore all other characters. if (c == '>') { c = xml.read(); state = OUTSIDE; } else c = xml.read(); break; case TAG: // Store characters in the tag buffer until we hit whitespace. // When we hit whitespace or '>' or '/', return the name of the tag. if (Character.isWhitespace((char) c) || c == '>' || c == '/') return tagBuffer.toString(); else { tagBuffer.append((char) c); c = xml.read(); } break; } } } catch (IOException ex) { // On exception, we can't determine the first tag, so return null. return null; } }
From source file:com.vmware.bdd.utils.CommonUtil.java
public static String readJsonFile(URL fileURL) { StringBuilder jsonBuff = new StringBuilder(); if (fileURL != null) { String fileName = fileURL.getFile(); InputStream in = null;/*from w w w . j a va 2 s . c om*/ try { in = new BufferedInputStream(fileURL.openStream()); Reader rd = new InputStreamReader(in, "UTF-8"); int c = 0; while ((c = rd.read()) != -1) { jsonBuff.append((char) c); } } catch (IOException e) { logger.error(e.getMessage() + "\n Can not find " + fileName + " or IO read error."); } finally { try { if (in != null) { in.close(); } } catch (IOException e) { logger.error(e.getMessage() + "\n Can not close " + fileName + "."); } } } return jsonBuff.toString(); }
From source file:jeeves.utils.BinaryFile.java
static String readInput(String path) { StringBuffer buffer = new StringBuffer(); Reader in = null; try {// w w w .ja v a 2 s .c o m FileInputStream fis = new FileInputStream(path); InputStreamReader isr = new InputStreamReader(fis, "UTF8"); in = new BufferedReader(isr); int ch; int numRead = 0; while (((ch = in.read()) > -1) && (numRead < 2000)) { buffer.append((char) ch); numRead++; } return buffer.toString(); } catch (IOException e) { e.printStackTrace(); return null; } finally { IOUtils.closeQuietly(in); } }
From source file:com.ieasy.basic.util.file.FileUtils.java
/** * ???// ww w . j a va 2s . c om * * @param fileName * ?? */ public static void readFileByChars(String fileName) { File file = new File(fileName); Reader reader = null; try { System.out.println("???"); // reader = new InputStreamReader(new FileInputStream(file)); int tempchar; while ((tempchar = reader.read()) != -1) { // windowsrn? // ? // ?r?n? if (((char) tempchar) != 'r') { System.out.print((char) tempchar); } } reader.close(); } catch (Exception e) { e.printStackTrace(); } try { System.out.println("???"); // char[] tempchars = new char[30]; int charread = 0; reader = new InputStreamReader(new FileInputStream(fileName)); // charread? while ((charread = reader.read(tempchars)) != -1) { // ??r? if ((charread == tempchars.length) && (tempchars[tempchars.length - 1] != 'r')) { System.out.print(tempchars); } else { for (int i = 0; i < charread; i++) { if (tempchars[i] == 'r') { continue; } else { System.out.print(tempchars[i]); } } } } } catch (Exception e1) { e1.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } }
From source file:Main.java
public Main() { JPanel inputPanel = new JPanel(new BorderLayout(3, 3)); go.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { URL pageURL = new URL("http://www.google.com"); HttpURLConnection urlConnection = (HttpURLConnection) pageURL.openConnection(); int respCode = urlConnection.getResponseCode(); String response = urlConnection.getResponseMessage(); codeArea.setText("HTTP/1.x " + respCode + " " + response + "\n"); int count = 1; while (true) { String header = urlConnection.getHeaderField(count); String key = urlConnection.getHeaderFieldKey(count); if (header == null || key == null) { break; }/*from w w w . j av a 2 s .co m*/ codeArea.append(urlConnection.getHeaderFieldKey(count) + ": " + header + "\n"); count++; } InputStream in = new BufferedInputStream(urlConnection.getInputStream()); Reader r = new InputStreamReader(in); int c; while ((c = r.read()) != -1) { codeArea.append(String.valueOf((char) c)); } codeArea.setCaretPosition(1); } catch (Exception ee) { } } }); inputPanel.add(BorderLayout.EAST, go); JScrollPane codeScroller = new JScrollPane(codeArea); add(BorderLayout.NORTH, inputPanel); add(BorderLayout.CENTER, codeScroller); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(700, 500); this.setVisible(true); }
From source file:Base64Encoder.java
private int mapCharToInt(Reader input) throws IOException { int c;/* ww w . j ava 2 s. c o m*/ while ((c = input.read()) != -1) { int result = REVERSE_MAPPING[c]; if (result != 0) return result - 1; if (c == '=') return -1; } return -1; }