InputStream sockIS = sock.getInputStream(); System.out.println("Connected to node."); LineReadingInputStream lis = new LineReadingInputStream(sockIS); OutputStreamWriter osw = new OutputStreamWriter(sockOS, "UTF-8"); osw.write("ClientHello\nExpectedVersion=0.7\nName=BootstrapPullTest-"+System.currentTimeMillis()+"\nEnd\n"); osw.flush(); String name = lis.readLine(65536, 128, true); SimpleFieldSet fs = new SimpleFieldSet(lis, 65536, 128, true, false, true); if(!name.equals("NodeHello")) { System.out.println("Sent data"); while(true) { name = lis.readLine(65536, 128, true); fs = new SimpleFieldSet(lis, 65536, 128, true, false, true); System.out.println("Got FCP message: \n"+name);
bufferSize = maxLength + 1; // Buffer too big, shrink it (add 1 for the optional \r) if(!markSupported()) return readLineWithoutMarking(maxLength, bufferSize, utf); mark(maxLength + 2); // in case we have both a \r and a \n while(true) { assert(buf.length - ctr > 0); int x = read(buf, ctr, buf.length - ctr); if(x < 0) { if(ctr == 0) toReturn = new String(buf, 0, (removeCR ? ctr - 1 : ctr), utf ? "UTF-8" : "ISO-8859-1"); reset(); skip(ctr + 1); return toReturn;
/** * * @param lis * @return */ protected SimpleFieldSet getMessage(LineReadingInputStream lis){ SimpleFieldSet sfs = new SimpleFieldSet(true); try { while(lis.available()>0){ String line = lis.readLine(128, 128, true); int index = line.indexOf('='); if(index == -1 || line.startsWith("End")) return sfs; sfs.putSingle(line.substring(0, index), line.substring(index+1)); } }catch(IOException e){ return sfs; } return sfs; } }
try { fis = new FileInputStream(logfile); lis = new LineReadingInputStream(fis); if (skip > 0) { lis.skip(skip); lis.readLine(100000, 200, true);
public void testBothImplementation() throws Exception { ByteArrayInputStream bis1 = new ByteArrayInputStream(BLOCK.getBytes("ISO-8859-1")); ByteArrayInputStream bis2 = new ByteArrayInputStream(BLOCK.getBytes("ISO-8859-1")); LineReadingInputStream lris1 = new LineReadingInputStream(bis1); LineReadingInputStream lris2 = new LineReadingInputStream(bis2); while(bis1.available() > 0 || bis2.available() > 0) { String stringWithoutMark =lris2.readLineWithoutMarking(MAX_LENGTH*10, BUFFER_SIZE, true); String stringWithMark = lris1.readLine(MAX_LENGTH*10, BUFFER_SIZE, true); assertEquals(stringWithMark, stringWithoutMark); } assertNull(lris1.readLine(MAX_LENGTH, BUFFER_SIZE, true)); assertNull(lris2.readLineWithoutMarking(MAX_LENGTH, BUFFER_SIZE, true)); } }
LineReadingInputStream lis = new LineReadingInputStream(is); OutputStream os = fcpSocket.getOutputStream(); os.flush(); String messageName = lis.readLine(128, 128, true); sfs = getMessage(lis); fcpm = FCPMessage.create(messageName, sfs); lis.close(); is.close(); os.close();
public void testReadLineWithoutMarking() throws Exception { LineReadingInputStream instance = new LineReadingInputStream(is); assertEquals("", instance.readLineWithoutMarking(MAX_LENGTH, BUFFER_SIZE, true)); assertEquals("\u0114", instance.readLineWithoutMarking(MAX_LENGTH, BUFFER_SIZE, true)); assertNull(instance.readLineWithoutMarking(MAX_LENGTH, BUFFER_SIZE, true)); instance = new LineReadingInputStream(is); for(String expectedLine : LINES) { assertEquals(expectedLine, instance.readLineWithoutMarking(MAX_LENGTH, BUFFER_SIZE, false)); assertNull(instance.readLineWithoutMarking(MAX_LENGTH, BUFFER_SIZE, false)); instance = new LineReadingInputStream(is); assertNull(instance.readLineWithoutMarking(0, BUFFER_SIZE, false)); assertNull(instance.readLineWithoutMarking(0, 0, false)); instance = new LineReadingInputStream(is); try { instance.readLineWithoutMarking(LENGTH_CHECKING_LINE_LF - 1, BUFFER_SIZE, true); fail(); } catch (TooLongException e) {} instance = new LineReadingInputStream(is); assertEquals(LENGTH_CHECKING_LINE.substring(0, LENGTH_CHECKING_LINE_LF), instance.readLineWithoutMarking(LENGTH_CHECKING_LINE_LF, BUFFER_SIZE, true)); instance = new LineReadingInputStream(is); assertEquals(NULL_LINE.substring(0, 5), instance.readLineWithoutMarking(BUFFER_SIZE, 1, true));
public static void maybeDisplayWrapperLogfile(ToadletContext ctx, HTMLNode contentNode) { final File logs = new File("wrapper.log"); long logSize = logs.length(); if(logs.exists() && logs.isFile() && logs.canRead() && (logSize > 0)) { HTMLNode logInfoboxContent = ctx.getPageMaker().getInfobox("infobox-info", "Current status", contentNode, "start-progress", true); LineReadingInputStream logreader = null; try { logreader = FileUtil.getLogTailReader(logs, 2000); String line; while ((line = logreader.readLine(100000, 200, true)) != null) { logInfoboxContent.addChild("#", line); logInfoboxContent.addChild("br"); } } catch(IOException e) {} finally { Closer.close(logreader); } } }
/** Load the config file into a SimpleFieldSet. * @throws IOException */ private static SimpleFieldSet initialLoad(File toRead) throws IOException { if(toRead == null) return null; FileInputStream fis = null; BufferedInputStream bis = null; LineReadingInputStream lis = null; try { fis = new FileInputStream(toRead); bis = new BufferedInputStream(fis); lis = new LineReadingInputStream(bis); // Config file is UTF-8 too! return new SimpleFieldSet(lis, 1024*1024, 128, true, true, true); // FIXME? advanced users may edit the config file, hence true? } finally { Closer.close(lis); Closer.close(bis); Closer.close(fis); } }
protected String readLineWithoutMarking(int maxLength, int bufferSize, boolean utf) throws IOException { if(maxLength < bufferSize) bufferSize = maxLength + 1; // Buffer too big, shrink it (add 1 for the optional \r) byte[] buf = new byte[Math.max(Math.min(128, maxLength), Math.min(1024, bufferSize))]; int ctr = 0; while(true) { int x = read(); if(x == -1) { if(ctr == 0) return null; return new String(buf, 0, ctr, utf ? "UTF-8" : "ISO-8859-1"); } // REDFLAG this is definitely safe with the above charsets, it may not be safe with some wierd ones. if(x == '\n') { if(ctr == 0) return ""; if(buf[ctr - 1] == '\r') ctr--; return new String(buf, 0, ctr, utf ? "UTF-8" : "ISO-8859-1"); } if(ctr >= maxLength) throw new TooLongException("We reached maxLength="+maxLength+ " parsing\n "+HexUtil.bytesToHex(buf, 0, ctr) + "\n" + new String(buf, 0, ctr, utf ? "UTF-8" : "ISO-8859-1")); if(ctr >= buf.length) { buf = Arrays.copyOf(buf, Math.min(buf.length * 2, maxLength)); } buf[ctr++] = (byte) x; } } }
public void testReadLine() throws Exception { LineReadingInputStream instance = new LineReadingInputStream(is); assertEquals("", instance.readLine(MAX_LENGTH, BUFFER_SIZE, true)); assertEquals("\u0114", instance.readLine(MAX_LENGTH, BUFFER_SIZE, true)); assertNull(instance.readLine(MAX_LENGTH, BUFFER_SIZE, true)); instance = new LineReadingInputStream(is); for(String expectedLine : LINES) { assertEquals(expectedLine, instance.readLine(MAX_LENGTH, BUFFER_SIZE, false)); assertNull(instance.readLine(MAX_LENGTH, BUFFER_SIZE, false)); instance = new LineReadingInputStream(is); assertNull(instance.readLine(0, BUFFER_SIZE, false)); assertNull(instance.readLine(0, 0, false)); instance = new LineReadingInputStream(is); try { instance.readLine(LENGTH_CHECKING_LINE_LF - 1, BUFFER_SIZE, true); fail(); } catch (TooLongException e) {} instance = new LineReadingInputStream(is); assertEquals(LENGTH_CHECKING_LINE.substring(0, LENGTH_CHECKING_LINE_LF), instance.readLine(LENGTH_CHECKING_LINE_LF, BUFFER_SIZE, true)); instance = new LineReadingInputStream(is); assertEquals(NULL_LINE.substring(0, 5), instance.readLine(BUFFER_SIZE, 1, true));
lis = new LineReadingInputStream(is); line = lis.readLine(100, 100, false); // really it's US-ASCII, but ISO-8859-1 is close enough. while((is.available() > 0) && !line.equals(boundary)) { line = lis.readLine(100, 100, false); contentType = null; while((line = lis.readLine(200, 200, true)) /* should be UTF-8 as we told the browser to send UTF-8 */ != null) { if(line.length() == 0) break;
InputStream is = new BufferedInputStream(sock.getInputStream(), 4096); LineReadingInputStream lis = new LineReadingInputStream(is); String firstLine = lis.readLine(32768, 128, false); // ISO-8859-1 or US-ASCII, _not_ UTF-8 if (firstLine == null) { sock.close(); String line = lis.readLine(32768, 128, false); // ISO-8859 or US-ASCII, not UTF-8 if (line == null) { sock.close();
public void realRun() throws IOException { InputStream is = new BufferedInputStream(handler.sock.getInputStream(), 4096); LineReadingInputStream lis = new LineReadingInputStream(is); String messageType = lis.readLine(128, 128, true); if(messageType == null) { Closer.close(is);