Example usage for java.io LineNumberReader readLine

List of usage examples for java.io LineNumberReader readLine

Introduction

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

Prototype

public String readLine() throws IOException 

Source Link

Document

Read a line of text.

Usage

From source file:org.kalypso.dwd.DWDRasterHelper.java

public static DWDRasterGeoLayer loadGeoRaster(final URL url, final String targetEpsg) throws Exception {
    LineNumberReader reader = null;
    try {//from  w  w w .jav  a 2s  .  c  o  m
        reader = new LineNumberReader(new InputStreamReader(url.openStream()));
        String line = null;
        DWDRaster raster = null;
        DWDRaster xRaster = null;
        DWDRaster yRaster = null;
        final double factor = DWDRasterHelper.getFactorForDwdKey(DWDRaster.KEY_100000_LAT);
        final double offset = DWDRasterHelper.getOffsetForDwdKey(DWDRaster.KEY_100000_LAT);
        while ((line = reader.readLine()) != null) {
            final Matcher staticHeaderMatcher = HEADER_STATIC.matcher(line);
            if (staticHeaderMatcher.matches()) {
                if (raster != null && raster.getKey() == DWDRaster.KEY_100000_LAT)
                    yRaster = raster;
                if (raster != null && raster.getKey() == DWDRaster.KEY_100000_LON)
                    xRaster = raster;
                final Date date = DATEFORMAT_RASTER.parse(staticHeaderMatcher.group(1));
                final int key = Integer.parseInt(staticHeaderMatcher.group(2));
                raster = new DWDRaster(date, key);
                continue;
            }

            final String[] values = (line.trim()).split(" +", 13);

            if (raster != null) {
                for (final String value : values)
                    raster.addValue((Double.parseDouble(value) + offset) * factor);
            }

        }
        if (raster != null && raster.getKey() == DWDRaster.KEY_100000_LAT)
            yRaster = raster;
        if (raster != null && raster.getKey() == DWDRaster.KEY_100000_LON)
            xRaster = raster;
        return new DWDRasterGeoLayer(targetEpsg, xRaster, yRaster);
    } finally {
        IOUtils.closeQuietly(reader);
    }
}

From source file:org.opensha.commons.util.FileUtils.java

/**
 * Prints a Text file//from   w w  w.  j a  v  a 2 s. co  m
 * @param pjob PrintJob  created using getToolkit().getPrintJob(JFrame,String,Properties);
 * @param pg Graphics
 * @param textToPrint String
 */
public static void print(PrintJob pjob, Graphics pg, String textToPrint) {

    int margin = 60;

    int pageNum = 1;
    int linesForThisPage = 0;
    int linesForThisJob = 0;
    // Note: String is immutable so won't change while printing.
    if (!(pg instanceof PrintGraphics)) {
        throw new IllegalArgumentException("Graphics context not PrintGraphics");
    }
    StringReader sr = new StringReader(textToPrint);
    LineNumberReader lnr = new LineNumberReader(sr);
    String nextLine;
    int pageHeight = pjob.getPageDimension().height - margin;
    Font helv = new Font("Monaco", Font.PLAIN, 12);
    //have to set the font to get any output
    pg.setFont(helv);
    FontMetrics fm = pg.getFontMetrics(helv);
    int fontHeight = fm.getHeight();
    int fontDescent = fm.getDescent();
    int curHeight = margin;
    try {
        do {
            nextLine = lnr.readLine();
            if (nextLine != null) {
                if ((curHeight + fontHeight) > pageHeight) {
                    // New Page
                    if (linesForThisPage == 0)
                        break;

                    pageNum++;
                    linesForThisPage = 0;
                    pg.dispose();
                    pg = pjob.getGraphics();
                    if (pg != null) {
                        pg.setFont(helv);
                    }
                    curHeight = 0;
                }
                curHeight += fontHeight;
                if (pg != null) {
                    pg.drawString(nextLine, margin, curHeight - fontDescent);
                    linesForThisPage++;

                    linesForThisJob++;
                }
            }
        } while (nextLine != null);
    } catch (EOFException eof) {
        // Fine, ignore
    } catch (Throwable t) { // Anything else
        t.printStackTrace();
    }
}

From source file:com.sds.acube.ndisc.xadmin.XNDiscAdminUtil.java

/**
 * XNDisc Admin ? ?//from  w ww  .j a  va  2s .c  o m
 */
private static void readVersionFromFile() {
    XNDiscAdmin_PublishingVersion = "<unknown>";
    XNDiscAdmin_PublishingDate = "<unknown>";
    InputStreamReader isr = null;
    LineNumberReader lnr = null;
    try {
        isr = new InputStreamReader(
                XNDiscAdminUtil.class.getResourceAsStream("/com/sds/acube/ndisc/xadmin/version.txt"));
        if (isr != null) {
            lnr = new LineNumberReader(isr);
            String line = null;
            do {
                line = lnr.readLine();
                if (line != null) {
                    if (line.startsWith("Publishing-Version=")) {
                        XNDiscAdmin_PublishingVersion = line
                                .substring("Publishing-Version=".length(), line.length()).trim();
                    } else if (line.startsWith("Publishing-Date=")) {
                        XNDiscAdmin_PublishingDate = line.substring("Publishing-Date=".length(), line.length())
                                .trim();
                    }
                }
            } while (line != null);
            lnr.close();
        }
    } catch (IOException ioe) {
        XNDiscAdmin_PublishingVersion = "<unknown>";
        XNDiscAdmin_PublishingDate = "<unknown>";
    } finally {
        try {
            if (lnr != null) {
                lnr.close();
            }
            if (isr != null) {
                isr.close();
            }
        } catch (IOException ioe) {
        }
    }
}

From source file:net.sf.keystore_explorer.crypto.signing.JarSigner.java

private static String getManifestMainAttrs(JarFile jar) throws IOException {

    // Get full manifest content
    String manifestContent = getManifest(jar);

    LineNumberReader lnr = new LineNumberReader(new StringReader(manifestContent));

    try {/*from www  .ja  v  a 2 s . c om*/
        StringBuilder sb = new StringBuilder();

        String line = null;

        // Keep reading until a blank line is found - the end of the main
        // attributes
        while ((line = lnr.readLine()) != null) {
            if (line.trim().length() == 0) {
                break;
            }

            // Append attribute line
            sb.append(line);
            sb.append(CRLF);
        }

        return sb.toString();
    } finally {
        IOUtils.closeQuietly(lnr);
    }
}

From source file:com.github.javarch.persistence.orm.test.DataBaseTestBuilder.java

private String readNextSqlScript(Resource rs) throws IOException {
    EncodedResource encoded = new EncodedResource(rs);
    LineNumberReader lnr = new LineNumberReader(encoded.getReader());
    String currentStatement = lnr.readLine();
    StringBuilder scriptBuilder = new StringBuilder();
    while (currentStatement != null) {
        if (StringUtils.hasText(currentStatement)
                && (SQL_COMMENT_PREFIX != null && !currentStatement.startsWith(SQL_COMMENT_PREFIX))) {
            if (scriptBuilder.length() > 0) {
                scriptBuilder.append('\n');
            }//from  w  w  w .j a v  a 2  s.c o m
            scriptBuilder.append(currentStatement);
        }
        currentStatement = lnr.readLine();
    }
    return scriptBuilder.toString();
}

From source file:org.nordapp.web.util.StateReader.java

/**
 * Reads the state file//  w  w w.jav  a2  s  .  c o  m
 * 
 * @param dest The destination of the file
 * @return The list of valid states
 * @throws IOException
 */
public List<String> getState(String dest) throws IOException {

    //Get the resource
    ServiceReference<ResourceService> rsr = context.getServiceReference(ResourceService.class);
    if (rsr == null)
        throw new IOException("The resource service is not available (maybe down or a version conflict).");

    ResourceService rs = context.getService(rsr);
    if (rs == null)
        throw new IOException("The resource service is not available (maybe down or a version conflict).");

    Map<String, String> props = new HashMap<String, String>();
    List<String> list = new ArrayList<String>();

    InputStream in = rs.getResourceAsStream(ctrl.getMandatorID(), ctrl.getGroupID(), ctrl.getArtifactID(), dest,
            ResourceService.FILE_RESOURCE, props);

    //use an input stream that supports mark.
    LineNumberReader r = new LineNumberReader(new InputStreamReader(in));
    String line = r.readLine();
    while (line != null) {
        line = line.trim();
        //skip empty lines and comments
        if (!(line.equals("") || line.startsWith("#"))) {
            list.add(line);
        }
        line = r.readLine();
    }

    return list;
}

From source file:org.springframework.xd.jdbc.SingleTableDatabaseInitializer.java

private Resource substitutePlaceholdersForResource(Resource resource) {

    StringBuilder script = new StringBuilder();

    try {/*  w  w w. j a v  a 2 s  . c o m*/
        EncodedResource er = new EncodedResource(resource);
        LineNumberReader lnr = new LineNumberReader(er.getReader());
        String line = lnr.readLine();
        while (line != null) {
            if (tableName != null && line.contains(TABLE_PLACEHOLDER)) {
                logger.debug(
                        "Substituting '" + TABLE_PLACEHOLDER + "' with '" + tableName + "' in '" + line + "'");
                line = line.replace(TABLE_PLACEHOLDER, tableName);
            }
            if (line.contains(COLUMNS_PLACEHOLDER)) {
                logger.debug(
                        "Substituting '" + COLUMNS_PLACEHOLDER + "' with '" + columns + "' in '" + line + "'");
                line = line.replace(COLUMNS_PLACEHOLDER, columns);
            }
            script.append(line + "\n");
            line = lnr.readLine();
        }
        lnr.close();
        return new ByteArrayResource(script.toString().getBytes());
    } catch (IOException e) {
        throw new InvalidDataAccessResourceUsageException("Unable to read script " + resource, e);
    }
}

From source file:org.kalypso.dwd.DWDRasterHelper.java

public static DWDObservationRaster loadObservationRaster(final URL url, final int dwdKey) throws Exception {
    final double factor = getFactorForDwdKey(dwdKey);
    final double offset = getOffsetForDwdKey(dwdKey);
    final String unit = getUnitForDwdKey(dwdKey);

    LineNumberReader lineNumberReader = null;
    try {// ww  w.ja va2s . co  m
        /* Create the reader. */
        InputStream inputStream = IOUtilities.getInputStream(url);
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        lineNumberReader = new LineNumberReader(inputStreamReader);

        int lmVersion = 0;
        String line = null;
        DWDObservationRaster raster = null;
        long blockDate = -1;
        int cellpos = 0;
        boolean rightBlock = false; // Is only used by the dynamic header (lmVersion = 1)
        while ((line = lineNumberReader.readLine()) != null) {
            final Matcher dynamicHeaderMatcher = HEADER_DYNAMIC.matcher(line);
            if (dynamicHeaderMatcher.matches()) // lm1
            {
                final Date startDate = DATEFORMAT_RASTER.parse(dynamicHeaderMatcher.group(1));
                final Calendar startCal = Calendar.getInstance();
                startCal.setTime(startDate);
                final int key = Integer.parseInt(dynamicHeaderMatcher.group(2));
                final int hour = Integer.parseInt(dynamicHeaderMatcher.group(3));

                startCal.add(Calendar.HOUR_OF_DAY, hour + 1);
                blockDate = startCal.getTimeInMillis();

                if (key == dwdKey) {
                    rightBlock = true;
                    if (raster == null) // if not already loading
                        raster = new DWDObservationRaster(key, unit);
                } else
                    rightBlock = false; // wrong key, but reading the file must be continued, the key can appear again

                lmVersion = 1;
                cellpos = 0;
                continue;
            }

            final Matcher staticHeaderMatcher = HEADER_STATIC.matcher(line);
            if (staticHeaderMatcher.matches()) // lm2 ??
            {
                // TODO: this is stil not the correct way to parse the date...
                // Google, how to do it correctly...
                blockDate = DATEFORMAT_RASTER.parse(staticHeaderMatcher.group(1)).getTime();
                final int key = Integer.parseInt(staticHeaderMatcher.group(2));

                if (key == dwdKey)
                    raster = new DWDObservationRaster(key, unit);
                else if (raster != null)
                    return raster;

                lmVersion = 2;
                cellpos = 0;
                continue;
            }

            if (raster == null)
                continue;

            /* If we are reading lmVersion = 1 and we have a wrong key, continue. */
            if (lmVersion == 1 && rightBlock == false)
                continue;

            switch (lmVersion) {
            case 1: {
                final String[] values = readValues(line, 5); // Do not trim the line...
                for (final String value2 : values) {
                    final double value = Double.parseDouble(value2);
                    raster.setValueFor(new Date(blockDate), cellpos, (value + offset) * factor);
                    cellpos++;
                }
                break;
            }

            case 2: {
                /* One line represents all 78 values for one position. */
                final String[] values = readValues(line, 5); // Do not trim the line...
                final Calendar valueDate = Calendar.getInstance();
                valueDate.setTimeInMillis(blockDate);
                for (final String valueStr : values) {
                    valueDate.add(Calendar.HOUR_OF_DAY, 1); // starting with hour 1, so add first here!
                    final double value = Double.parseDouble(valueStr);
                    raster.setValueFor(valueDate.getTime(), cellpos, (value + offset) * factor);
                }

                cellpos++;
                break;
            }
            }
        }

        return raster;
    } finally {
        IOUtils.closeQuietly(lineNumberReader);
    }
}

From source file:com.esd.vs.interceptor.LoginInterceptor.java

public String getMACAddress(String ip) {
    String str = "";
    String macAddress = "";
    try {/* w ww.  j  a  va  2s  . c o m*/
        Process p = Runtime.getRuntime().exec("nbtstat -A " + ip);
        InputStreamReader ir = new InputStreamReader(p.getInputStream());
        LineNumberReader input = new LineNumberReader(ir);
        for (int i = 1; i < 100; i++) {
            str = input.readLine();
            if (str != null) {
                if (str.indexOf("MAC Address") > 1) {
                    macAddress = str.substring(str.indexOf("MAC Address") + 14, str.length());
                    break;
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace(System.out);
    }
    return macAddress;
}

From source file:com.hs.mail.web.util.DataImporter.java

public void importAccount(UserManager manager, InputStream is) throws IOException {
    LineNumberReader reader = new LineNumberReader(new InputStreamReader(is));
    String line;/*  w  ww .  java  2  s  .  com*/
    while ((line = reader.readLine()) != null) {
        try {
            User user = parseAccount(line);
            manager.addUser(user);
        } catch (Exception e) {
            addError(reader.getLineNumber(), line, e);
        }
    }
}