Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Daniele Futtorovic Newsgroups: comp.lang.java.programmer Subject: Re: readLine() and newline problem Date: Fri, 10 May 2019 18:50:16 +0200 Organization: A noiseless patient Spider Lines: 164 Message-ID: References: <8beb1189-8953-4072-9b4b-0d54cf64eaa5@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Injection-Date: Fri, 10 May 2019 16:50:49 -0000 (UTC) Injection-Info: reader02.eternal-september.org; posting-host="8294a533e94c49dad9df8e9f3a7fd957"; logging-data="30525"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/aJTTlsrcuOi8OPZtzcq9/" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 Cancel-Lock: sha1:4U//wGNN/smjQNiBII1BPFuprMY= In-Reply-To: X-Antivirus-Status: Clean Content-Language: en-US X-Antivirus: AVG (VPS 190510-2, 05/10/2019), Outbound message Xref: csiph.com comp.lang.java.programmer:38956 On 2019-05-10 10:29, mike wrote: > So how can I update test to simulate my error? That's a bit trickier, but the code below should give you an idea. > Yes I am also thinking of using read(char []). I think it is more appropriate. The only thing that will be a bit tricky is to find my separator char.I have to check if we have ] then another ] so there will be many ifs. Save yourself trouble and use a java.util.Scanner. See below. Code: ```` import org.junit.Test; import java.io.*; import java.util.Objects; import java.util.Scanner; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class NoEol { static final String DATA = "\n" + " \n" + " \n" + " \n" + " urn:ietf:params:netconf:base:1.1\n" + " \n" + " \n" + " urn:ietf:params:ns:netconf:capability:startup:1.0\n" + " \n" + " \n" + " 4\n" + " \n" + " ]]>]]>"; //NOTE no newline at the end @Test public void testBufferedReaderWithNewLine(){ try(Supplier s = Supplier.newInstance(DATA + "\n")){ globWithBufferedReader( s.input(), "]]>]]>"); } catch ( IOException e ) { e.printStackTrace(); } } @Test public void testBufferedReaderWithoutNewLine(){ try(Supplier s = Supplier.newInstance(DATA)){ globWithBufferedReader( s.input(), "]]>]]>"); } catch ( IOException e ) { e.printStackTrace(); } } @Test public void testScannerWithNewLine(){ try(Supplier s = Supplier.newInstance(DATA + "\n")){ globWithScanner( s.input(), "]]>]]>"); } } @Test public void testScannerWithoutNewLine(){ try(Supplier s = Supplier.newInstance(DATA)){ globWithScanner( s.input(), "]]>]]>"); } } static void globWithBufferedReader( Reader r, String sep) throws IOException { BufferedReader br = new BufferedReader(r); StringBuilder sb = new StringBuilder(); for(String line; null != (line = br.readLine()); ){ sb.append(line); if( line.contains( sep ) ){ break; } } System.out.println("Read finished: " + sb); } static void globWithScanner(Reader r, String sep){ Scanner sc = new Scanner(r); sc.useDelimiter( sep ); System.out.println("Read finished: " + sc.next()); } private static final class Supplier implements Runnable, AutoCloseable { static final ExecutorService executor = Executors.newCachedThreadPool(); private final String data; private final PipedReader pipeIn; private final PipedWriter pipeOut; private volatile Thread thread; Supplier( String data ) { this.data = Objects.requireNonNull( data, "data" ); pipeOut = new PipedWriter(); try { pipeIn = new PipedReader( pipeOut ); } catch ( IOException x ) { throw new AssertionError( "Could not cerate pipe", x ); } } public Reader input() { return pipeIn; } @Override public void close() { if ( thread != null ) { thread.interrupt(); thread = null; } } @Override public void run() { thread = Thread.currentThread(); try { pipeOut.write( data ); synchronized ( this ) { wait(); } } catch ( InterruptedException e ) { System.err.println( "Interrupted." ); Thread.currentThread().interrupt(); } catch ( IOException e ) { e.printStackTrace(); } System.err.println( "Supplier exiting." ); } public static Supplier newInstance( String data ) { Supplier ret = new Supplier(data); executor.submit( ret ); return ret; } } } ```` -- DF.