Search in sources :

Example 56 with LineNumberReader

use of java.io.LineNumberReader in project cxf by apache.

the class IdlPreprocessorReaderTest method consumeReader.

private void consumeReader(final Reader includeReader) throws IOException {
    LineNumberReader oReader = new LineNumberReader(includeReader);
    String line = null;
    do {
        line = oReader.readLine();
    } while (line != null);
}
Also used : LineNumberReader(java.io.LineNumberReader)

Example 57 with LineNumberReader

use of java.io.LineNumberReader in project chipKIT32-MAX by chipKIT32.

the class StdXMLReader method startNewStream.

/**
    * Starts a new stream from a Java reader. The new stream is used
    * temporary to read data from. If that stream is exhausted, control
    * returns to the parent stream.
    *
    * @param reader the non-null reader to read the new data from
    * @param isInternalEntity true if the reader is produced by resolving
    *                         an internal entity
    */
public void startNewStream(Reader reader, boolean isInternalEntity) {
    StackedReader oldReader = this.currentReader;
    this.readers.push(this.currentReader);
    this.currentReader = new StackedReader();
    if (isInternalEntity) {
        this.currentReader.lineReader = null;
        this.currentReader.pbReader = new PushbackReader(reader, 2);
    } else {
        this.currentReader.lineReader = new LineNumberReader(reader);
        this.currentReader.pbReader = new PushbackReader(this.currentReader.lineReader, 2);
    }
    this.currentReader.systemId = oldReader.systemId;
    this.currentReader.publicId = oldReader.publicId;
}
Also used : PushbackReader(java.io.PushbackReader) LineNumberReader(java.io.LineNumberReader)

Example 58 with LineNumberReader

use of java.io.LineNumberReader in project jdk8u_jdk by JetBrains.

the class J2DBench method loadOptions.

public static String loadOptions(FileReader fr, String filename) {
    LineNumberReader lnr = new LineNumberReader(fr);
    Group.restoreAllDefaults();
    String line;
    try {
        while ((line = lnr.readLine()) != null) {
            String reason = Group.root.setOption(line);
            if (reason != null) {
                System.err.println("Option " + line + " at line " + lnr.getLineNumber() + " ignored: " + reason);
            }
        }
    } catch (IOException e) {
        Group.restoreAllDefaults();
        return ("IO Error reading " + filename + " at line " + lnr.getLineNumber());
    }
    return null;
}
Also used : IOException(java.io.IOException) LineNumberReader(java.io.LineNumberReader)

Example 59 with LineNumberReader

use of java.io.LineNumberReader in project sling by apache.

the class ControlListener method configure.

/**
     * Read the port from the config file
     * @return The port or null
     */
private boolean configure(final boolean fromConfigFile) {
    boolean result = false;
    if (fromConfigFile) {
        final File configFile = this.getConfigFile();
        if (configFile.canRead()) {
            try (final LineNumberReader lnr = new LineNumberReader(new FileReader(configFile))) {
                this.socketAddress = getSocketAddress(lnr.readLine());
                this.secretKey = lnr.readLine();
                result = true;
            } catch (final IOException ignore) {
            // ignore
            }
        }
    } else {
        this.socketAddress = getSocketAddress(this.listenSpec);
        this.secretKey = generateKey();
        result = true;
    }
    return result;
}
Also used : FileReader(java.io.FileReader) IOException(java.io.IOException) File(java.io.File) LineNumberReader(java.io.LineNumberReader)

Example 60 with LineNumberReader

use of java.io.LineNumberReader in project sling by apache.

the class LauncherCallable method stop.

public static void stop(final Log LOG, final ProcessDescription cfg) throws Exception {
    boolean isNew = false;
    if (cfg.getProcess() != null || isNew) {
        LOG.info("Stopping Launchpad '" + cfg.getId() + "'");
        boolean destroy = true;
        final int twoMinutes = 2 * 60 * 1000;
        final File controlPortFile = getControlPortFile(cfg.getDirectory());
        LOG.debug("Control port file " + controlPortFile + " exists: " + controlPortFile.exists());
        if (controlPortFile.exists()) {
            // reading control port
            int controlPort = -1;
            String secretKey = null;
            LineNumberReader lnr = null;
            String serverName = null;
            try {
                lnr = new LineNumberReader(new FileReader(controlPortFile));
                final String portLine = lnr.readLine();
                final int pos = portLine.indexOf(':');
                controlPort = Integer.parseInt(portLine.substring(pos + 1));
                if (pos > 0) {
                    serverName = portLine.substring(0, pos);
                }
                secretKey = lnr.readLine();
            } catch (final NumberFormatException ignore) {
                // we ignore this
                LOG.debug("Error reading control port file " + controlPortFile, ignore);
            } catch (final IOException ignore) {
                // we ignore this
                LOG.debug("Error reading control port file " + controlPortFile, ignore);
            } finally {
                IOUtils.closeQuietly(lnr);
            }
            if (controlPort != -1) {
                final List<String> hosts = new ArrayList<String>();
                if (serverName != null) {
                    hosts.add(serverName);
                }
                hosts.add("localhost");
                hosts.add("127.0.0.1");
                LOG.debug("Found control port " + controlPort);
                int index = 0;
                while (destroy && index < hosts.size()) {
                    final String hostName = hosts.get(index);
                    Socket clientSocket = null;
                    DataOutputStream out = null;
                    BufferedReader in = null;
                    try {
                        LOG.debug("Trying to connect to " + hostName + ":" + controlPort);
                        clientSocket = new Socket();
                        // set a socket timeout
                        clientSocket.connect(new InetSocketAddress(hostName, controlPort), twoMinutes);
                        // without that, read() call on the InputStream associated with this Socket is infinite
                        clientSocket.setSoTimeout(twoMinutes);
                        LOG.debug(hostName + ":" + controlPort + " connection estabilished, sending the 'stop' command...");
                        out = new DataOutputStream(clientSocket.getOutputStream());
                        in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                        if (secretKey != null) {
                            out.writeBytes(secretKey);
                            out.write(' ');
                        }
                        out.writeBytes("stop\n");
                        in.readLine();
                        destroy = false;
                        LOG.debug("'stop' command sent to " + hostName + ":" + controlPort);
                    } catch (final Throwable ignore) {
                        // catch Throwable because InetSocketAddress and Socket#connect throws unchecked exceptions
                        // we ignore this for now
                        LOG.debug("Error sending 'stop' command to " + hostName + ":" + controlPort + " due to: " + ignore.getMessage());
                    } finally {
                        IOUtils.closeQuietly(in);
                        IOUtils.closeQuietly(out);
                        IOUtils.closeQuietly(clientSocket);
                    }
                    index++;
                }
            }
        }
        if (cfg.getProcess() != null) {
            final Process process = cfg.getProcess();
            if (!destroy) {
                // as shutdown might block forever, we use a timeout
                final long now = System.currentTimeMillis();
                final long end = now + twoMinutes;
                LOG.debug("Waiting for process to stop...");
                while (isAlive(process) && (System.currentTimeMillis() < end)) {
                    try {
                        Thread.sleep(2500);
                    } catch (InterruptedException e) {
                    // ignore
                    }
                }
                if (isAlive(process)) {
                    LOG.debug("Process timeout out after 2 minutes");
                    destroy = true;
                } else {
                    LOG.debug("Process stopped");
                }
            }
            if (destroy) {
                LOG.debug("Destroying process...");
                process.destroy();
                LOG.debug("Process destroyed");
            }
            cfg.setProcess(null);
        }
    } else {
        LOG.warn("Launchpad already stopped");
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) DataOutputStream(java.io.DataOutputStream) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) IOException(java.io.IOException) LineNumberReader(java.io.LineNumberReader) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File) Socket(java.net.Socket)

Aggregations

LineNumberReader (java.io.LineNumberReader)401 IOException (java.io.IOException)203 InputStreamReader (java.io.InputStreamReader)167 FileReader (java.io.FileReader)102 File (java.io.File)79 InputStream (java.io.InputStream)64 StringReader (java.io.StringReader)64 ArrayList (java.util.ArrayList)54 FileInputStream (java.io.FileInputStream)33 BufferedReader (java.io.BufferedReader)27 PrintWriter (java.io.PrintWriter)25 HashMap (java.util.HashMap)22 FileNotFoundException (java.io.FileNotFoundException)20 BufferedWriter (java.io.BufferedWriter)16 FileWriter (java.io.FileWriter)16 Pattern (java.util.regex.Pattern)16 Test (org.junit.jupiter.api.Test)16 ByteArrayInputStream (java.io.ByteArrayInputStream)15 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 Reader (java.io.Reader)14