/** @tests java.io.BufferedReader#mark(int) */ public void test_markI() throws IOException { // Test for method void java.io.BufferedReader.mark(int) char[] buf = null; br = new BufferedReader(new Support_StringReader(testString)); br.skip(500); br.mark(1000); br.skip(250); br.reset(); buf = new char[testString.length()]; br.read(buf, 0, 500); assertTrue( "Failed to set mark properly", testString.substring(500, 1000).equals(new String(buf, 0, 500))); try { br = new BufferedReader(new Support_StringReader(testString), 800); br.skip(500); br.mark(250); br.read(buf, 0, 1000); br.reset(); fail("Failed to invalidate mark properly"); } catch (IOException x) { // Expected } char[] chars = new char[256]; for (int i = 0; i < 256; i++) chars[i] = (char) i; Reader in = new BufferedReader(new Support_StringReader(new String(chars)), 12); in.skip(6); in.mark(14); in.read(new char[14], 0, 14); in.reset(); assertTrue("Wrong chars", in.read() == (char) 6 && in.read() == (char) 7); in = new BufferedReader(new Support_StringReader(new String(chars)), 12); in.skip(6); in.mark(8); in.skip(7); in.reset(); assertTrue("Wrong chars 2", in.read() == (char) 6 && in.read() == (char) 7); BufferedReader br = new BufferedReader(new StringReader("01234"), 2); br.mark(3); char[] carray = new char[3]; int result = br.read(carray); assertEquals(3, result); assertEquals("Assert 0:", '0', carray[0]); assertEquals("Assert 1:", '1', carray[1]); assertEquals("Assert 2:", '2', carray[2]); assertEquals("Assert 3:", '3', br.read()); br = new BufferedReader(new StringReader("01234"), 2); br.mark(3); carray = new char[4]; result = br.read(carray); assertEquals("Assert 4:", 4, result); assertEquals("Assert 5:", '0', carray[0]); assertEquals("Assert 6:", '1', carray[1]); assertEquals("Assert 7:", '2', carray[2]); assertEquals("Assert 8:", '3', carray[3]); assertEquals("Assert 9:", '4', br.read()); assertEquals("Assert 10:", -1, br.read()); BufferedReader reader = new BufferedReader(new StringReader("01234")); reader.mark(Integer.MAX_VALUE); reader.read(); reader.close(); }
/** @tests java.io.BufferedReader#reset() */ public void test_reset() { // Test for method void java.io.BufferedReader.reset() try { br = new BufferedReader(new Support_StringReader(testString)); br.skip(500); br.mark(900); br.skip(500); br.reset(); char[] buf = new char[testString.length()]; br.read(buf, 0, 500); assertTrue( "Failed to reset properly", testString.substring(500, 1000).equals(new String(buf, 0, 500))); } catch (java.io.IOException e) { fail("Exception during reset test"); } try { br = new BufferedReader(new Support_StringReader(testString)); br.skip(500); br.reset(); fail("Reset succeeded on unmarked stream"); } catch (IOException x) { return; } }
public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new FileReader(args[0])); long numskip = 0; try { numskip = Long.parseLong(br.readLine()); } catch (NumberFormatException e) { System.out.println(e); } // this computation is based on Unix newlines (won't work // on files with Windows newlines) numskip += Math.ceil(numskip / 80.0); br.skip(numskip); int numpredictions = Integer.parseInt(br.readLine()); Interval[] activities = new Interval[numpredictions]; for (int i = 0; i < numpredictions; ++i) { String[] line = br.readLine().split("\\s+"); activities[i] = new Interval( Integer.parseInt(line[0]), Integer.parseInt(line[1]), Integer.parseInt(line[2])); } System.out.println(selection(activities)); }
public void testSkipHeader() throws Exception { ByteArrayInputStream source = new ByteArrayInputStream("<?xml version=\"1.0\" encoding=\"UTF-8\"?>abc".getBytes()); Xml11InputStream in = new Xml11InputStream(source); BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8")); assertEquals(38, reader.skip(38)); assertEquals("abc", reader.readLine()); assertEquals(null, reader.readLine()); }
/** @tests java.io.BufferedReader#skip(long) */ public void test_skipJ() { // Test for method long java.io.BufferedReader.skip(long) try { br = new BufferedReader(new Support_StringReader(testString)); br.skip(500); char[] buf = new char[testString.length()]; br.read(buf, 0, 500); assertTrue( "Failed to set skip properly", testString.substring(500, 1000).equals(new String(buf, 0, 500))); } catch (java.io.IOException e) { fail("Exception during skip test"); } }
/* (non-Javadoc) * @see java.lang.Runnable#run() */ @Override public void run() { BufferedReader reader = null; final File logFile = new File(logFileName); try { reader = new BufferedReader(new FileReader(logFile)); reader.skip(logFile.length()); } catch (FileNotFoundException e1) { logger.warn("Log file not found. End file logging."); interrupt(); } catch (IOException e) { throw new RuntimeException(e); } // Main loop. while (!isInterrupted()) { String line; try { if (reader != null) { line = reader.readLine(); while (line != null && !logAppender.getDisplay().isDisposed()) { final String tmp = line; logAppender .getDisplay() .syncExec( new Runnable() { @Override public void run() { logAppender.appendToLog(tmp); logAppender.appendToLog("\n"); } }); line = reader.readLine(); } } } catch (IOException e1) { logger.error("While logging: " + e1.getMessage(), e1); interrupt(); } try { Thread.sleep(1000); } catch (InterruptedException e) { logger.debug("Thread interrupted."); interrupt(); } } IOUtils.closeQuietly(reader); }
/* * 得到页面数 */ public void getPageNum() { threadcount = 10; URL urlnow = null; try { urlnow = new URL(url); } catch (MalformedURLException e) { // TODO Auto-generated catch block System.out.println("url wrong"); e.printStackTrace(); } InputStreamReader isr = null; try { isr = new InputStreamReader(urlnow.openStream()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } BufferedReader bfr = new BufferedReader(isr); String row = null; String temp = null; try { bfr.skip(62512); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } while (true) { try { temp = bfr.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (temp.matches(".*当前页面是.*")) { pageNum = Integer.parseInt( temp.split(";")[0] .split(",")[1] .trim() .replace("共", "") .replace("页", "") .replace(" ", "") .trim()); System.out.println("pageNum" + pageNum); break; } } }
public static String fromFile(File f, int position, int size) throws IOException { BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(f)); reader.skip(position); String line = null; StringBuilder stringBuilder = new StringBuilder(); String ls = System.getProperty("line.separator"); while ((line = reader.readLine()) != null && stringBuilder.length() < size) { stringBuilder.append(line); stringBuilder.append(ls); } return stringBuilder.toString(); } finally { if (reader != null) { reader.close(); } } }
public String getNextWord(int minLength, int maxLength) { String str = null; int tries = 0; do { try { BufferedReader br = new BufferedReader(new InputStreamReader(ctx.getAssets().open(fileName)), 10000); br.skip(random.nextInt(fileSize)); br.readLine(); str = br.readLine(); br.close(); } catch (IOException e) { str = null; Log.e(LOG_TAG, "reading file failed", e); } tries++; } while (str != null && (str.length() < minLength || str.length() > maxLength) && tries < DictionaryFactory.MAX_TRIES); return str; }
public String readLine() throws java.io.IOException { if (eof()) { throw new java.io.EOFException(); } // Needed to write my own version to keep track of the File's Position java.io.BufferedReader buffReader = currentFile.reader(); String retString = ""; char charbuff[] = new char[80]; int charsRead = 80; int skip = 1; while (charsRead == 80) { buffReader.mark(82); charsRead = buffReader.read(charbuff, 0, 80); for (int i = 0; i < charsRead; i++) { // 'i' will never be 80 if (charbuff[i] == '\r') { if ((i < 79 && charbuff[i + 1] == '\n') || (i == 79 && buffReader.read() == '\n')) { skip = 2; } charsRead = i; } else if (charbuff[i] == '\n') { charsRead = i; } } currentFile.pos_$eq(currentFile.pos() + charsRead); retString += new String(charbuff, 0, charsRead); } buffReader.reset(); // Doesn't include 'skip' when at the end of file currentFile.pos_$eq(currentFile.pos() + buffReader.skip(charsRead + skip) - charsRead); return retString; }
protected boolean set(InputStream in, boolean onlyHeaders) { try { BufferedReader reader = new BufferedReader(new InputStreamReader(in)); // try{ String firstLine = reader.readLine(); /* * }catch(SocketException se){ System.out.println("[HTTP ERROR START]"); System.out.println(this); * System.out.println("[HTTP ERROR END]"); throw se; } */ if (firstLine == null || firstLine.length() <= 0) return false; setFirstLine(firstLine); // Thanks for Giordano Sassaroli <*****@*****.**> (09/03/03) HTTPStatus httpStatus = new HTTPStatus(firstLine); int statCode = httpStatus.getStatusCode(); if (statCode == HTTPStatus.CONTINUE) { // ad hoc code for managing iis non-standard behaviour // iis sends 100 code response and a 200 code response in the same // stream, so the code should check the presence of the actual // response in the stream. // skip all header lines String headerLine = reader.readLine(); while ((headerLine != null) && (0 < headerLine.length())) { HTTPHeader header = new HTTPHeader(headerLine); if (header.hasName() == true) setHeader(header); headerLine = reader.readLine(); } // look forward another first line String actualFirstLine = reader.readLine(); if ((actualFirstLine != null) && (0 < actualFirstLine.length())) { // this is the actual first line setFirstLine(actualFirstLine); } else { return true; } } String headerLine = reader.readLine(); while ((headerLine != null) && (0 < headerLine.length())) { HTTPHeader header = new HTTPHeader(headerLine); if (header.hasName() == true) setHeader(header); headerLine = reader.readLine(); } if (onlyHeaders == true) { setContent("", false); return true; } boolean isChunkedRequest = isChunked(); long contentLen = 0; if (isChunkedRequest == true) { try { String chunkSizeLine = reader.readLine(); contentLen = Long.parseLong(new String(chunkSizeLine.getBytes(), 0, chunkSizeLine.length() - 2)); } catch (Exception e) { } ; } else { if (getHeader(HTTP.CONTENT_LENGTH) != null) { contentLen = getContentLength(); } else if ((getHeader(HTTP.CONNECTION) != null) && (getHeader(HTTP.CONNECTION).getValue().toLowerCase().indexOf("keep-alive") != -1) && (getHeader(HTTP.CONTENT_LENGTH) == null)) { contentLen = 0; } else { StringBuffer sb = new StringBuffer(""); int ch; while ((ch = reader.read()) != -1) { sb.append((char) ch); } setContent(sb.toString()); return true; } } StringBuffer contentBuf = new StringBuffer(); while (0 < contentLen) { int chunkSize = HTTP.getChunkSize(); char readBuf[] = new char[chunkSize]; long readCnt = 0; while (readCnt < contentLen) { try { // Thanks for Mark Retallack (02/02/05) long bufReadLen = contentLen - readCnt; if (chunkSize < bufReadLen) bufReadLen = chunkSize; int readLen = reader.read(readBuf, 0, (int) bufReadLen); if (readLen < 0) break; contentBuf.append(new String(readBuf, 0, readLen)); readCnt += readLen; } catch (Exception e) { Debug.warning(e); break; } } if (isChunkedRequest == true) { // skip CRLF long skipLen = 0; do { long skipCnt = reader.skip(HTTP.CRLF.length() - skipLen); if (skipCnt < 0) break; skipLen += skipCnt; } while (skipLen < HTTP.CRLF.length()); // read next chunk size try { String chunkSizeLine = reader.readLine(); contentLen = Long.parseLong(new String(chunkSizeLine.getBytes(), 0, chunkSizeLine.length() - 2)); } catch (Exception e) { contentLen = 0; } ; } else contentLen = 0; } // Thanks for Ralf G. R. Bergs (02/09/04) String contentStr = contentBuf.toString(); setContent(contentStr.getBytes(), false); } catch (Exception e) { Debug.warning(e); return false; } return true; }
public static void main(String[] args) throws IOException { System.out.println( "* * ***** * **** **** * * ***** ******* **** ***** * * * * * *******"); System.out.println( "* * * * * * * * * * * * * * * * * * * * * * * * * * * *"); System.out.println( "* * * *** * * * * * * * *** * * * * * ***** * * * ***** ***** *"); System.out.println( " * * * * * * * * * * * * * * * * * * * * * * * * * * * *"); System.out.println( " * * ***** ***** **** **** * * ***** * **** ***** * * * * * * * * *"); // String x = ""; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int sum = 0, t = 0; String m[] = new String[100]; int n[] = new int[100]; do { System.out.println("Enter 'vm' for vegetarian menu and 'nm' for non vegetarian menu"); String a = (br.readLine()); if (a.equalsIgnoreCase("vm")) { System.out.println( "Enter 'i' for Indian foods,'ch' for chinese foods, 'co' for continental foods and 'be' for beverages"); String b = (br.readLine()); if (b.equalsIgnoreCase("i")) { System.out.println( "Enter 'r' for rice,'b' for Indian bread,'cu' for curries,'sw' for sweets"); String c = (br.readLine()); if (c.equalsIgnoreCase("r")) { System.out.println("1.Steamed Rice Rs 20"); System.out.println("2.Jeera Rice Rs 40"); System.out.println("3.Curd Rice Rs 40"); System.out.println("4.Pulav Rs 80"); System.out.println("5.Veg Biryani Rs 120"); char d = (char) (br.read()); switch (d) { case '1': m[t] = ("Steamed Rice"); n[t] = 20; break; case '2': m[t] = ("Jeera Rice"); n[t] = 40; break; case '3': m[t] = ("Curd Rice"); n[t] = 40; break; case '4': m[t] = ("Pulav"); n[t] = 80; break; case '5': m[t] = ("Veg Biryani"); n[t] = 120; break; } } } } br.skip(2); System.out.println("Do you want to continue(y/n)"); } while (br.readLine().equalsIgnoreCase("y")); System.out.println("Sr No \t Name \t cost"); for (int i = 0; i <= t; i++) { sum = n[i] + sum; if (m[i] != "") System.out.println((i + 1) + "\t" + m[i] + "\t" + n[i]); } System.out.println("\t Total=" + sum); }