Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #38956

Re: readLine() and newline problem

From Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: readLine() and newline problem
Date 2019-05-10 18:50 +0200
Organization A noiseless patient Spider
Message-ID <qb4a59$tpt$1@dont-email.me> (permalink)
References <8beb1189-8953-4072-9b4b-0d54cf64eaa5@googlegroups.com> <qb37mg$oj8$1@dont-email.me> <e082e8d0-0d47-4728-a112-f0ab82d4eda1@googlegroups.com>

Show all headers | View raw


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 =
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
        + "    <hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
        + "      <capabilities>\n"
        + "        <capability>\n"
        + "          urn:ietf:params:netconf:base:1.1\n"
        + "        </capability>\n"
        + "        <capability>\n"
        + "          urn:ietf:params:ns:netconf:capability:startup:1.0\n"
        + "        </capability>\n"
        + "      </capabilities>\n"
        + "      <session-id>4</session-id>\n"
        + "    </hello>\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.

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

readLine() and newline problem mike <mikaelpetterson@hotmail.com> - 2019-05-09 23:16 -0700
  Re: readLine() and newline problem Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2019-05-10 09:02 +0200
    Re: readLine() and newline problem mike <mikaelpetterson@hotmail.com> - 2019-05-10 01:29 -0700
      Re: readLine() and newline problem Martin Gregorie <martin@mydomain.invalid> - 2019-05-10 10:22 +0000
      Re: readLine() and newline problem Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2019-05-10 18:50 +0200

csiph-web