Example usage for java.io LineNumberReader ready

List of usage examples for java.io LineNumberReader ready

Introduction

In this page you can find the example usage for java.io LineNumberReader ready.

Prototype

public boolean ready() throws IOException 

Source Link

Document

Tells whether this stream is ready to be read.

Usage

From source file:Main.java

/**
 * Loads an index (Hashtable) from a file.
 * //  ww w .j  a v  a  2s. c  o  m
 * @param root_
 *            The file where the index is stored in.
 * @return The indextable.
 * @exception IOException
 *                If an internal error prevents the file from being read.
 */
public static Hashtable loadIndex(File root_, String name) throws IOException {
    File indexfile = new File(root_, name);
    Hashtable index = new Hashtable();
    if (indexfile.exists()) {
        LineNumberReader in = new LineNumberReader(new FileReader(indexfile));
        while (in.ready())
            index.put(in.readLine(), new Long(in.readLine()));
        in.close();
    }
    return index;
}

From source file:com.netscape.cms.logging.LogFile.java

/**
 * This method actually does the logging, and is not overridden
 * by subclasses, so you can call it and know that it will do exactly
 * what you see below./* ww w. j  a  v a 2  s  .  c om*/
 */
private synchronized void doLog(ILogEvent event, boolean noFlush) throws ELogException {

    String entry = logEvt2String(event);

    if (mLogWriter == null) {
        String[] params = { mFileName, entry };

        if (mLogSigning) {
            ConsoleError.send(new SystemEvent(CMS.getUserMessage("CMS_LOG_LOGFILE_CLOSED", params)));
            // Failed to write to audit log, shut down CMS
            shutdownCMS();
        }
        throw new ELogException(CMS.getUserMessage("CMS_LOG_LOGFILE_CLOSED", params));
    } else {
        try {
            mLogWriter.write(entry, 0/*offset*/, entry.length());

            if (mLogSigning == true) {
                if (mSignature != null) {
                    // include newline for calculating MAC
                    mSignature.update(entry.getBytes("UTF-8"));
                } else {
                    CMS.debug("LogFile: mSignature is not yet ready... null in log()");
                }
            }
            if (mTrace) {
                CharArrayWriter cw = new CharArrayWriter(200);
                PrintWriter pw = new PrintWriter(cw);
                Exception e = new Exception();
                e.printStackTrace(pw);
                char[] c = cw.toCharArray();
                cw.close();
                pw.close();

                CharArrayReader cr = new CharArrayReader(c);
                LineNumberReader lr = new LineNumberReader(cr);

                String text = null;
                String method = null;
                String fileAndLine = null;
                if (lr.ready()) {
                    text = lr.readLine();
                    do {
                        text = lr.readLine();
                    } while (text.indexOf("logging") != -1);
                    int p = text.indexOf("(");
                    fileAndLine = text.substring(p);

                    String classandmethod = text.substring(0, p);
                    int q = classandmethod.lastIndexOf(".");
                    method = classandmethod.substring(q + 1);
                    mLogWriter.write(fileAndLine, 0/*offset*/, fileAndLine.length());
                    mLogWriter.write(" ", 0/*offset*/, " ".length());
                    mLogWriter.write(method, 0/*offset*/, method.length());
                }
            }
            mLogWriter.newLine();

            if (mLogSigning == true) {
                if (mSignature != null) {
                    mSignature.update(LINE_SEP_BYTE);
                } else {
                    CMS.debug("LogFile: mSignature is null in log() 2");
                }
            }
        } catch (IOException e) {
            ConsoleError.send(new SystemEvent(
                    CMS.getUserMessage("CMS_LOG_WRITE_FAILED", mFileName, entry, e.toString())));
            if (mLogSigning) {
                // Failed to write to audit log, shut down CMS
                e.printStackTrace();
                shutdownCMS();
            }
        } catch (IllegalStateException e) {
            CMS.debug("LogFile: exception thrown in log(): " + e.toString());
            ConsoleError
                    .send(new SignedAuditEvent(CMS.getLogMessage(LOG_SIGNED_AUDIT_EXCEPTION, e.toString())));
        } catch (GeneralSecurityException gse) {
            // DJN: handle error
            CMS.debug("LogFile: exception thrown in log(): " + gse.toString());
            gse.printStackTrace();
            ConsoleError
                    .send(new SignedAuditEvent(CMS.getLogMessage(LOG_SIGNED_AUDIT_EXCEPTION, gse.toString())));
        } catch (Exception ee) { // Make darn sure we got everything
            ConsoleError
                    .send(new SignedAuditEvent(CMS.getLogMessage(LOG_SIGNED_AUDIT_EXCEPTION, ee.toString())));
            if (mLogSigning) {
                // Failed to write to audit log, shut down CMS
                ee.printStackTrace();
                shutdownCMS();
            }

        }

        // XXX
        // Although length will be in Unicode dual-bytes, the PrintWriter
        // will only print out 1 byte per character.  I suppose this could
        // be dependent on the encoding of your log file, but it ain't that
        // smart yet.  Also, add one for the newline. (hmm, on NT, CR+LF)
        int nBytes = entry.length() + 1;

        mBytesWritten += nBytes;
        mBytesUnflushed += nBytes;

        if (mBufferSize > 0 && mBytesUnflushed > mBufferSize && !noFlush) {
            flush();
        }
    }
}

From source file:org.apache.hadoop.yarn.server.nodemanager.TestDockerContainerExecutorWithMocks.java

@Test
//Test that a container launch correctly wrote the session script with the
//commands we expected
public void testContainerLaunch() throws IOException {
    String appSubmitter = "nobody";
    String appSubmitterFolder = "nobodysFolder";
    String appId = "APP_ID";
    String containerId = "CONTAINER_ID";
    String testImage = "\"sequenceiq/hadoop-docker:2.4.1\"";

    Container container = mock(Container.class, RETURNS_DEEP_STUBS);
    ContainerId cId = mock(ContainerId.class, RETURNS_DEEP_STUBS);
    ContainerLaunchContext context = mock(ContainerLaunchContext.class);
    HashMap<String, String> env = new HashMap<String, String>();

    when(container.getContainerId()).thenReturn(cId);
    when(container.getLaunchContext()).thenReturn(context);
    when(cId.getApplicationAttemptId().getApplicationId().toString()).thenReturn(appId);
    when(cId.toString()).thenReturn(containerId);

    when(context.getEnvironment()).thenReturn(env);
    env.put(YarnConfiguration.NM_DOCKER_CONTAINER_EXECUTOR_IMAGE_NAME, testImage);
    Path scriptPath = new Path("file:///bin/echo");
    Path tokensPath = new Path("file:///dev/null");

    Path pidFile = new Path(workDir, "pid");

    dockerContainerExecutor.activateContainer(cId, pidFile);
    int ret = dockerContainerExecutor.launchContainer(new ContainerStartContext.Builder()
            .setContainer(container).setNmPrivateContainerScriptPath(scriptPath)
            .setNmPrivateTokensPath(tokensPath).setUser(appSubmitter).setAppId(appId)
            .setContainerWorkDir(workDir).setLocalDirs(dirsHandler.getLocalDirs())
            .setLogDirs(dirsHandler.getLogDirs()).setUserFolder(appSubmitterFolder).build());
    assertEquals(0, ret);//from  w ww  .j  a  v a  2  s.c  o m
    //get the script
    Path sessionScriptPath = new Path(workDir,
            Shell.appendScriptExtension(DockerContainerExecutor.DOCKER_CONTAINER_EXECUTOR_SESSION_SCRIPT));
    LineNumberReader lnr = new LineNumberReader(new FileReader(sessionScriptPath.toString()));
    boolean cmdFound = false;
    List<String> localDirs = dirsToMount(dirsHandler.getLocalDirs());
    List<String> logDirs = dirsToMount(dirsHandler.getLogDirs());
    List<String> workDirMount = dirsToMount(Collections.singletonList(workDir.toUri().getPath()));
    List<String> expectedCommands = new ArrayList<String>(
            Arrays.asList(DOCKER_LAUNCH_COMMAND, "run", "--rm", "--net=host", "--name", containerId));
    expectedCommands.addAll(localDirs);
    expectedCommands.addAll(logDirs);
    expectedCommands.addAll(workDirMount);
    String shellScript = workDir + "/launch_container.sh";

    expectedCommands
            .addAll(Arrays.asList(testImage.replaceAll("['\"]", ""), "bash", "\"" + shellScript + "\""));

    String expectedPidString = "echo `/bin/true inspect --format {{.State.Pid}} " + containerId + "` > "
            + pidFile.toString() + ".tmp";
    boolean pidSetterFound = false;
    while (lnr.ready()) {
        String line = lnr.readLine();
        LOG.debug("line: " + line);
        if (line.startsWith(DOCKER_LAUNCH_COMMAND)) {
            List<String> command = new ArrayList<String>();
            for (String s : line.split("\\s+")) {
                command.add(s.trim());
            }

            assertEquals(expectedCommands, command);
            cmdFound = true;
        } else if (line.startsWith("echo")) {
            assertEquals(expectedPidString, line);
            pidSetterFound = true;
        }

    }
    assertTrue(cmdFound);
    assertTrue(pidSetterFound);
}

From source file:org.kalypso.commons.runtime.LogStatusWrapper.java

/**
 * Constructor: summary is taken from the logfile. The file is parsed and each line which begins with '***' is
 * appended to the summary.//w w w  . j a va 2s  . c  o m
 */
public LogStatusWrapper(final File logFile, final String charsetName) {
    if (logFile == null)
        throw new IllegalStateException(Messages.getString("org.kalypso.commons.runtime.LogStatusWrapper.1")); //$NON-NLS-1$
    if (!logFile.exists())
        throw new IllegalStateException(
                Messages.getString("org.kalypso.commons.runtime.LogStatusWrapper.2", logFile.toString())); //$NON-NLS-1$

    m_logFile = logFile;
    m_charsetName = charsetName;

    final StringWriter sw = new StringWriter();
    final PrintWriter pw = new PrintWriter(sw);

    LineNumberReader reader = null;
    try {
        reader = new LineNumberReader(
                new InputStreamReader(new BufferedInputStream(new FileInputStream(logFile)), charsetName));
        while (reader.ready()) {
            final String line = reader.readLine();
            if (line == null)
                break;

            if (line.startsWith(SUMMARY_BEGIN_TOKEN) && line.length() > 3)
                pw.println(line.substring(3));
        }

        pw.close();
    } catch (final IOException e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(reader);
        pw.close();
        m_summary = sw.toString();
    }
}

From source file:org.kalypso.kalypsomodel1d2d.sim.IterationInfo.java

@Override
public void readIterFile() throws IOException {
    m_itrFile.refresh();/*www  .  j a  v a  2s.  c o  m*/
    if (!m_itrFile.exists())
        return;

    /* Read file and write outputs */
    LineNumberReader lnr = null;
    try {
        // final InputStream inputStream = m_itrFile.getContent().getInputStream();
        final byte[] content = FileUtil.getContent(m_itrFile);
        // lnr = new LineNumberReader( new BufferedReader( new InputStreamReader( inputStream ) ) );
        lnr = new LineNumberReader(new StringReader(new String(content, Charset.defaultCharset())));
        while (lnr.ready()) {
            final String line = lnr.readLine();
            if (line == null)
                break;

            processLine(line, lnr.getLineNumber());
        }
    } catch (final FileNotFoundException e) {
        // FIXME: stati are never used; what happened here?!
        //      if( lnr == null )
        //        StatusUtilities.createStatus( IStatus.WARNING, ISimulation1D2DConstants.CODE_RMA10S, Messages.getString( "org.kalypso.kalypsomodel1d2d.sim.IterationInfo.1" ), e ); //$NON-NLS-1$
        //
        //      final String msg = Messages.getString( "org.kalypso.kalypsomodel1d2d.sim.IterationInfo.2", lnr.getLineNumber() ); //$NON-NLS-1$
        //      StatusUtilities.createStatus( IStatus.WARNING, ISimulation1D2DConstants.CODE_RMA10S, msg, e );
    } finally {
        IOUtils.closeQuietly(lnr);
    }
}

From source file:org.kalypso.kalypsomodel1d2d.sim.IterationInfoSWAN.java

@Override
public void readIterFile() throws IOException {
    m_itrFile.refresh();//from   ww  w  . ja v  a 2  s  .c  om
    if (!m_itrFile.exists())
        return;

    /* Read file and write outputs */
    LineNumberReader lnr = null;
    try {
        final byte[] content = FileUtil.getContent(m_itrFile);
        lnr = new LineNumberReader(new StringReader(new String(content, Charset.defaultCharset())));
        while (lnr.ready()) {
            final String line = lnr.readLine();
            if (line == null)
                break;
            processLine(line, lnr.getLineNumber());
        }
    } catch (final FileNotFoundException e) {
        // FIXME: these stati are never used; what happened here?
        //      if( lnr == null )
        //        StatusUtilities.createStatus( IStatus.WARNING, ISimulation1D2DConstants.CODE_RMA10S, Messages.getString( "org.kalypso.kalypsomodel1d2d.sim.IterationInfo.1" ), e ); //$NON-NLS-1$
        //
        //      final String msg = Messages.getString( "org.kalypso.kalypsomodel1d2d.sim.IterationInfo.2", lnr.getLineNumber() ); //$NON-NLS-1$
        //      StatusUtilities.createStatus( IStatus.WARNING, ISimulation1D2DConstants.CODE_RMA10S, msg, e );
    } finally {
        IOUtils.closeQuietly(lnr);
    }
}

From source file:org.kalypso.kalypsomodel1d2d.sim.IterationInfoTelemac.java

@Override
public void readIterFile() throws IOException {
    m_itrFile.refresh();//from  w w w .  j a  va 2  s  .  co  m
    if (!m_itrFile.exists())
        return;

    /* Read file and write outputs */
    LineNumberReader lnr = null;
    try {
        final byte[] content = FileUtil.getContent(m_itrFile);
        lnr = new LineNumberReader(new StringReader(new String(content, Charset.defaultCharset())));
        while (lnr.ready()) {
            final String line = lnr.readLine();
            if (line == null)
                break;
            processLine(line, lnr.getLineNumber());
        }
    } catch (final FileNotFoundException e) {
        if (lnr == null)
            StatusUtilities.createStatus(IStatus.WARNING, ISimulation1D2DConstants.CODE_RMA10S,
                    Messages.getString("org.kalypso.kalypsomodel1d2d.sim.IterationInfo.1"), e); //$NON-NLS-1$

        final String msg = Messages.getString("org.kalypso.kalypsomodel1d2d.sim.IterationInfo.2", //$NON-NLS-1$
                lnr.getLineNumber());
        StatusUtilities.createStatus(IStatus.WARNING, ISimulation1D2DConstants.CODE_RMA10S, msg, e);
    } finally {
        IOUtils.closeQuietly(lnr);
    }
}

From source file:org.kalypso.model.hydrology.internal.postprocessing.Block.java

public void readValues(final LineNumberReader reader, final int numValues) throws IOException {
    int valueCount = 0;

    while (reader.ready()) {
        final String line = reader.readLine();
        if (line == null)
            break;

        if (line.startsWith("#")) //$NON-NLS-1$
            continue;

        if (StringUtils.isBlank(line))
            continue;

        final String[] values = StringUtils.split(line, null);
        for (final String item : values) {
            m_timeStep.step(m_currentStep);

            final Date valueDate = m_currentStep.getTime();

            final double value = NumberUtils.parseQuietDouble(item);

            m_data.put(valueDate, value);

            valueCount++;//from   w  ww  . ja  v  a  2 s.co  m

            if (valueCount >= numValues)
                return;
        }
    }
}

From source file:org.kalypso.model.hydrology.internal.postprocessing.BlockTimeSeries.java

/**
 * imports all ts from given blockfile//w  w w .  ja  v a2  s .c o  m
 */
public void importBlockFile(final File blockFile) {
    LineNumberReader reader = null;
    try {
        reader = new LineNumberReader(new FileReader(blockFile));

        final BlockTimeStep timeStep = searchTimeoffset(reader);

        while (reader.ready()) {
            final Entry<String, Integer> blockInfo = searchBlockHeader(reader);
            if (blockInfo == null)
                break;

            final String key = blockInfo.getKey();
            final int valuesCount = blockInfo.getValue();

            // Add values to existing block
            final Block block = getOrCreateBlock(key, timeStep);
            block.readValues(reader, valuesCount);
        }
        reader.close();
    } catch (final Exception e) {
        e.printStackTrace();
        System.out.println("could not read blockfile "); //$NON-NLS-1$
    } finally {
        IOUtils.closeQuietly(reader);
    }
}

From source file:org.kalypso.model.hydrology.internal.postprocessing.BlockTimeSeries.java

private BlockTimeStep searchTimeoffset(final LineNumberReader reader) throws ParseException, IOException {
    while (reader.ready()) {
        final String line = reader.readLine();
        if (line == null)
            break;

        if (line.startsWith("#")) //$NON-NLS-1$
            continue;

        // final Matcher m = pTime.matcher( line );
        final Matcher m = m_resultsFormat.getSimulationPeriodPattern().matcher(line);
        final Matcher synthM = pSynthTime.matcher(line);
        if (m.matches()) {
            final String sDate = m.group(1);
            final String sTime = m.group(2);
            final String sStep = m.group(3);

            final Calendar startCal = parseDate24(sDate, sTime);
            final Duration timestep = parseDuration(sStep);
            return new BlockTimeStep(startCal, timestep);
        } else if (synthM.matches()) {
            // synthetisches Ereignis hat kein Anfangsdatum, daher wird 01.01.2000 angenommen!
            final Calendar startCal = m_dateFormat.getCalendar();
            startCal.set(2000, 1, 1, 0, 0);

            final String sStep = synthM.group(3);
            final Duration timestep = parseDuration(sStep);
            return new BlockTimeStep(startCal, timestep);
        }//  ww  w  .j  av a2s .  com
    }

    return null;
}