Example usage for java.util Scanner nextLong

List of usage examples for java.util Scanner nextLong

Introduction

In this page you can find the example usage for java.util Scanner nextLong.

Prototype

public long nextLong() 

Source Link

Document

Scans the next token of the input as a long .

Usage

From source file:org.apache.james.sieverepository.file.SieveFileRepository.java

/**
 * The default quota, if any, is stored in file '.quota' in the sieve root directory. Quotas for
 * specific users are stored in file '.quota' in the user's directory.
 * <p/>//from w w w  .j  a v a  2 s .co m
 * <p>The '.quota' file contains a single positive integer value representing the quota in octets.
 *
 * @see SieveRepository#haveSpace(java.lang.String, java.lang.String, long)
 */
@Override
public void haveSpace(String user, String name, long size) throws QuotaExceededException, StorageException {
    long usedSpace = 0;
    for (File file : getUserDirectory(user).listFiles()) {
        if (!(file.getName().equals(name) || SYSTEM_FILES.contains(file.getName()))) {
            usedSpace = usedSpace + file.length();
        }
    }

    long quota = Long.MAX_VALUE;
    File file = getQuotaFile(user);
    if (!file.exists()) {
        file = getQuotaFile();
    }
    if (file.exists()) {
        Scanner scanner = null;
        try {
            scanner = new Scanner(file, UTF_8);
            quota = scanner.nextLong();
        } catch (FileNotFoundException ex) {
            // no op
        } catch (NoSuchElementException ex) {
            // no op
        } finally {
            if (null != scanner) {
                scanner.close();
            }
        }
    }
    if ((usedSpace + size) > quota) {
        throw new QuotaExceededException(" Quota: " + quota + " Used: " + usedSpace + " Requested: " + size);
    }
}

From source file:org.apache.james.filesystem.api.SieveFileRepository.java

/**
 * @see org.apache.james.managesieve.api.SieveRepository#getQuota()
 *//*w  ww  .  ja  v  a 2s. c  om*/
public long getQuota() throws QuotaNotFoundException {
    Long quota = null;
    File file = getQuotaFile();
    if (file.exists()) {
        Scanner scanner = null;
        try {
            scanner = new Scanner(file, UTF_8);
            quota = scanner.nextLong();
        } catch (FileNotFoundException ex) {
            // no op
        } catch (NoSuchElementException ex) {
            // no op
        } finally {
            if (null != scanner) {
                scanner.close();
            }
        }
    }
    if (null == quota) {
        throw new QuotaNotFoundException("No default quota");
    }
    return quota;
}

From source file:org.apache.james.filesystem.api.SieveFileRepository.java

/**
 * @throws QuotaNotFoundException //from   w  w  w.ja  v  a 2s  . co  m
 * @see org.apache.james.managesieve.api.SieveRepository#getQuota(java.lang.String)
 */
public long getQuota(final String user) throws UserNotFoundException, QuotaNotFoundException {
    Long quota = null;
    File file = getQuotaFile(user);
    if (file.exists()) {
        Scanner scanner = null;
        try {
            scanner = new Scanner(file, UTF_8);
            quota = scanner.nextLong();
        } catch (FileNotFoundException ex) {
            // no op
        } catch (NoSuchElementException ex) {
            // no op
        } finally {
            if (null != scanner) {
                scanner.close();
            }
        }
    }
    if (null == quota) {
        throw new QuotaNotFoundException("No quota for user: " + user);
    }
    return quota;
}

From source file:org.apache.james.filesystem.api.SieveFileRepository.java

/**
 * The default quota, if any, is stored in file '.quota' in the sieve root directory. Quotas for
 * specific users are stored in file '.quota' in the user's directory.
 * /*from w ww  .j ava 2 s  .c om*/
 * <p>The '.quota' file contains a single positive integer value representing the quota in octets. 
 * 
 * @see org.apache.james.managesieve.api.SieveRepository#haveSpace(java.lang.String, java.lang.String, long)
 */
public void haveSpace(final String user, final String name, final long size)
        throws UserNotFoundException, QuotaExceededException {
    long usedSpace = 0;
    for (File file : getUserDirectory(user).listFiles()) {
        if (!(file.getName().equals(name) || SYSTEM_FILES.contains(file.getName()))) {
            usedSpace = usedSpace + file.length();
        }
    }

    long quota = Long.MAX_VALUE;
    File file = getQuotaFile(user);
    if (!file.exists()) {
        file = getQuotaFile();
    }
    if (file.exists()) {
        Scanner scanner = null;
        try {
            scanner = new Scanner(file, UTF_8);
            quota = scanner.nextLong();
        } catch (FileNotFoundException ex) {
            // no op
        } catch (NoSuchElementException ex) {
            // no op
        } finally {
            if (null != scanner) {
                scanner.close();
            }
        }
    }
    if ((usedSpace + size) > quota) {
        throw new QuotaExceededException(" Quota: " + quota + " Used: " + usedSpace + " Requested: " + size);
    }
}

From source file:juicebox.windowui.QCDialog.java

public QCDialog(MainWindow mainWindow, HiC hic, String title) {
    super(mainWindow);

    Dataset dataset = hic.getDataset();//ww  w .  j  av a2  s .c  o m

    String text = dataset.getStatistics();
    String textDescription = null;
    String textStatistics = null;
    String graphs = dataset.getGraphs();
    JTextPane description = null;
    JTabbedPane tabbedPane = new JTabbedPane();
    HTMLEditorKit kit = new HTMLEditorKit();

    StyleSheet styleSheet = kit.getStyleSheet();
    styleSheet.addRule("table { border-collapse: collapse;}");
    styleSheet.addRule("body {font-family: Sans-Serif; font-size: 12;}");
    styleSheet.addRule("td { padding: 2px; }");
    styleSheet.addRule(
            "th {border-bottom: 1px solid #000; text-align: left; background-color: #D8D8D8; font-weight: normal;}");

    if (text != null) {
        int split = text.indexOf("</table>") + 8;
        textDescription = text.substring(0, split);
        textStatistics = text.substring(split);
        description = new JTextPane();
        description.setEditable(false);
        description.setContentType("text/html");
        description.setEditorKit(kit);
        description.setText(textDescription);
        tabbedPane.addTab("About Library", description);

        JTextPane textPane = new JTextPane();
        textPane.setEditable(false);
        textPane.setContentType("text/html");

        textPane.setEditorKit(kit);
        textPane.setText(textStatistics);
        JScrollPane pane = new JScrollPane(textPane);
        tabbedPane.addTab("Statistics", pane);
    }
    boolean success = true;
    if (graphs != null) {

        long[] A = new long[2000];
        long sumA = 0;
        long[] mapq1 = new long[201];
        long[] mapq2 = new long[201];
        long[] mapq3 = new long[201];
        long[] intraCount = new long[100];
        final XYSeries intra = new XYSeries("Intra Count");
        final XYSeries leftRead = new XYSeries("Left");
        final XYSeries rightRead = new XYSeries("Right");
        final XYSeries innerRead = new XYSeries("Inner");
        final XYSeries outerRead = new XYSeries("Outer");
        final XYSeries allMapq = new XYSeries("All MapQ");
        final XYSeries intraMapq = new XYSeries("Intra MapQ");
        final XYSeries interMapq = new XYSeries("Inter MapQ");

        Scanner scanner = new Scanner(graphs);
        try {
            while (!scanner.next().equals("["))
                ;

            for (int idx = 0; idx < 2000; idx++) {
                A[idx] = scanner.nextLong();
                sumA += A[idx];
            }

            while (!scanner.next().equals("["))
                ;
            for (int idx = 0; idx < 201; idx++) {
                mapq1[idx] = scanner.nextInt();
                mapq2[idx] = scanner.nextInt();
                mapq3[idx] = scanner.nextInt();

            }

            for (int idx = 199; idx >= 0; idx--) {
                mapq1[idx] = mapq1[idx] + mapq1[idx + 1];
                mapq2[idx] = mapq2[idx] + mapq2[idx + 1];
                mapq3[idx] = mapq3[idx] + mapq3[idx + 1];
                allMapq.add(idx, mapq1[idx]);
                intraMapq.add(idx, mapq2[idx]);
                interMapq.add(idx, mapq3[idx]);
            }
            while (!scanner.next().equals("["))
                ;
            for (int idx = 0; idx < 100; idx++) {
                int tmp = scanner.nextInt();
                if (tmp != 0)
                    innerRead.add(logXAxis[idx], tmp);
                intraCount[idx] = tmp;
                tmp = scanner.nextInt();
                if (tmp != 0)
                    outerRead.add(logXAxis[idx], tmp);
                intraCount[idx] += tmp;
                tmp = scanner.nextInt();
                if (tmp != 0)
                    rightRead.add(logXAxis[idx], tmp);
                intraCount[idx] += tmp;
                tmp = scanner.nextInt();
                if (tmp != 0)
                    leftRead.add(logXAxis[idx], tmp);
                intraCount[idx] += tmp;
                if (idx > 0)
                    intraCount[idx] += intraCount[idx - 1];
                if (intraCount[idx] != 0)
                    intra.add(logXAxis[idx], intraCount[idx]);
            }
        } catch (NoSuchElementException exception) {
            JOptionPane.showMessageDialog(getParent(), "Graphing file improperly formatted", "Error",
                    JOptionPane.ERROR_MESSAGE);
            success = false;
        }

        if (success) {
            final XYSeriesCollection readTypeCollection = new XYSeriesCollection();
            readTypeCollection.addSeries(innerRead);
            readTypeCollection.addSeries(outerRead);
            readTypeCollection.addSeries(leftRead);
            readTypeCollection.addSeries(rightRead);

            final JFreeChart readTypeChart = ChartFactory.createXYLineChart("Types of reads vs distance", // chart title
                    "Distance (log)", // domain axis label
                    "Binned Reads (log)", // range axis label
                    readTypeCollection, // data
                    PlotOrientation.VERTICAL, true, // include legend
                    true, false);

            final XYPlot readTypePlot = readTypeChart.getXYPlot();

            readTypePlot.setDomainAxis(new LogarithmicAxis("Distance (log)"));
            readTypePlot.setRangeAxis(new LogarithmicAxis("Binned Reads (log)"));
            readTypePlot.setBackgroundPaint(Color.white);
            readTypePlot.setRangeGridlinePaint(Color.lightGray);
            readTypePlot.setDomainGridlinePaint(Color.lightGray);
            readTypeChart.setBackgroundPaint(Color.white);
            readTypePlot.setOutlinePaint(Color.black);
            final ChartPanel chartPanel = new ChartPanel(readTypeChart);

            final XYSeriesCollection reCollection = new XYSeriesCollection();
            final XYSeries reDistance = new XYSeries("Distance");

            for (int i = 0; i < A.length; i++) {
                if (A[i] != 0)
                    reDistance.add(i, A[i] / (float) sumA);
            }
            reCollection.addSeries(reDistance);

            final JFreeChart reChart = ChartFactory.createXYLineChart(
                    "Distance from closest restriction enzyme site", // chart title
                    "Distance (bp)", // domain axis label
                    "Fraction of Reads (log)", // range axis label
                    reCollection, // data
                    PlotOrientation.VERTICAL, true, // include legend
                    true, false);

            final XYPlot rePlot = reChart.getXYPlot();
            rePlot.setDomainAxis(new NumberAxis("Distance (bp)"));
            rePlot.setRangeAxis(new LogarithmicAxis("Fraction of Reads (log)"));
            rePlot.setBackgroundPaint(Color.white);
            rePlot.setRangeGridlinePaint(Color.lightGray);
            rePlot.setDomainGridlinePaint(Color.lightGray);
            reChart.setBackgroundPaint(Color.white);
            rePlot.setOutlinePaint(Color.black);
            final ChartPanel chartPanel2 = new ChartPanel(reChart);

            final XYSeriesCollection intraCollection = new XYSeriesCollection();

            intraCollection.addSeries(intra);

            final JFreeChart intraChart = ChartFactory.createXYLineChart("Intra reads vs distance", // chart title
                    "Distance (log)", // domain axis label
                    "Cumulative Sum of Binned Reads (log)", // range axis label
                    intraCollection, // data
                    PlotOrientation.VERTICAL, true, // include legend
                    true, false);

            final XYPlot intraPlot = intraChart.getXYPlot();
            intraPlot.setDomainAxis(new LogarithmicAxis("Distance (log)"));
            intraPlot.setRangeAxis(new NumberAxis("Cumulative Sum of Binned Reads (log)"));
            intraPlot.setBackgroundPaint(Color.white);
            intraPlot.setRangeGridlinePaint(Color.lightGray);
            intraPlot.setDomainGridlinePaint(Color.lightGray);
            intraChart.setBackgroundPaint(Color.white);
            intraPlot.setOutlinePaint(Color.black);
            final ChartPanel chartPanel3 = new ChartPanel(intraChart);

            final XYSeriesCollection mapqCollection = new XYSeriesCollection();
            mapqCollection.addSeries(allMapq);
            mapqCollection.addSeries(intraMapq);
            mapqCollection.addSeries(interMapq);

            final JFreeChart mapqChart = ChartFactory.createXYLineChart("MapQ Threshold Count", // chart title
                    "MapQ threshold", // domain axis label
                    "Count", // range axis label
                    mapqCollection, // data
                    PlotOrientation.VERTICAL, true, // include legend
                    true, // include tooltips
                    false);

            final XYPlot mapqPlot = mapqChart.getXYPlot();
            mapqPlot.setBackgroundPaint(Color.white);
            mapqPlot.setRangeGridlinePaint(Color.lightGray);
            mapqPlot.setDomainGridlinePaint(Color.lightGray);
            mapqChart.setBackgroundPaint(Color.white);
            mapqPlot.setOutlinePaint(Color.black);
            final ChartPanel chartPanel4 = new ChartPanel(mapqChart);

            tabbedPane.addTab("Pair Type", chartPanel);
            tabbedPane.addTab("Restriction", chartPanel2);
            tabbedPane.addTab("Intra vs Distance", chartPanel3);
            tabbedPane.addTab("MapQ", chartPanel4);
        }
    }

    final ExpectedValueFunction df = hic.getDataset().getExpectedValues(hic.getZoom(),
            hic.getNormalizationType());
    if (df != null) {
        double[] expected = df.getExpectedValues();
        final XYSeriesCollection collection = new XYSeriesCollection();
        final XYSeries expectedValues = new XYSeries("Expected");
        for (int i = 0; i < expected.length; i++) {
            if (expected[i] > 0)
                expectedValues.add(i + 1, expected[i]);
        }
        collection.addSeries(expectedValues);
        String title1 = "Expected at " + hic.getZoom() + " norm " + hic.getNormalizationType();
        final JFreeChart readTypeChart = ChartFactory.createXYLineChart(title1, // chart title
                "Distance between reads (log)", // domain axis label
                "Genome-wide expected (log)", // range axis label
                collection, // data
                PlotOrientation.VERTICAL, false, // include legend
                true, false);
        final XYPlot readTypePlot = readTypeChart.getXYPlot();

        readTypePlot.setDomainAxis(new LogarithmicAxis("Distance between reads (log)"));
        readTypePlot.setRangeAxis(new LogarithmicAxis("Genome-wide expected (log)"));
        readTypePlot.setBackgroundPaint(Color.white);
        readTypePlot.setRangeGridlinePaint(Color.lightGray);
        readTypePlot.setDomainGridlinePaint(Color.lightGray);
        readTypeChart.setBackgroundPaint(Color.white);
        readTypePlot.setOutlinePaint(Color.black);
        final ChartPanel chartPanel5 = new ChartPanel(readTypeChart);

        tabbedPane.addTab("Expected", chartPanel5);
    }

    if (text == null && graphs == null) {
        JOptionPane.showMessageDialog(this, "Sorry, no metrics are available for this dataset", "Error",
                JOptionPane.ERROR_MESSAGE);
        setVisible(false);
        dispose();

    } else {
        getContentPane().add(tabbedPane);
        pack();
        setModal(false);
        setLocation(100, 100);
        setTitle(title);
        setVisible(true);
    }
}

From source file:org.apache.hadoop.hdfs.server.datanode.fsdataset.impl.BlockPoolSlice.java

private void addReplicaToReplicasMap(Block block, ReplicaMap volumeMap, boolean isFinalized)
        throws IOException {
    ReplicaInfo newReplica = null;// w w w .  ja v a 2 s. c om
    long blockId = block.getBlockId();
    long genStamp = block.getGenerationStamp();
    if (isFinalized) {
        newReplica = new FinalizedReplica(blockId, block.getNumBytes(), genStamp, volume,
                DatanodeUtil.idToBlockDir(finalizedDir, blockId));
    } else {
        File file = new File(rbwDir, block.getBlockName());
        boolean loadRwr = true;
        File restartMeta = new File(file.getParent() + File.pathSeparator + "." + file.getName() + ".restart");
        Scanner sc = null;
        try {
            sc = new Scanner(restartMeta, "UTF-8");
            // The restart meta file exists
            if (sc.hasNextLong() && (sc.nextLong() > Time.now())) {
                // It didn't expire. Load the replica as a RBW.
                // We don't know the expected block length, so just use 0
                // and don't reserve any more space for writes.
                newReplica = new ReplicaBeingWritten(blockId, validateIntegrityAndSetLength(file, genStamp),
                        genStamp, volume, file.getParentFile(), null, 0);
                loadRwr = false;
            }
            sc.close();
            if (!restartMeta.delete()) {
                FsDatasetImpl.LOG.warn("Failed to delete restart meta file: " + restartMeta.getPath());
            }
        } catch (FileNotFoundException fnfe) {
            // nothing to do hereFile dir =
        } finally {
            if (sc != null) {
                sc.close();
            }
        }
        // Restart meta doesn't exist or expired.
        if (loadRwr) {
            newReplica = new ReplicaWaitingToBeRecovered(blockId, validateIntegrityAndSetLength(file, genStamp),
                    genStamp, volume, file.getParentFile());
        }
    }

    ReplicaInfo oldReplica = volumeMap.get(bpid, newReplica.getBlockId());
    if (oldReplica == null) {
        volumeMap.add(bpid, newReplica);
    } else {
        FsDatasetImpl.LOG.warn("Two block files with the same block id exist " + "on disk: "
                + oldReplica.getBlockFile() + " and " + newReplica.getBlockFile());
    }
}

From source file:javaapplication1.Prog.java

public void run(String[] args) throws JSONException, IOException {
    int k = 0;//w  w w.  j  a v  a2  s. co  m
    while (k == 0) {
        Scanner sc = new Scanner(System.in);
        System.out.print("your command:");
        String i = sc.nextLine();

        if ("exit".equals(i))
            k = 1;
        else {
            if ("help".equals(i)) {
                System.out.print(help);
            } else if ("transfer".equals(i)) {
                System.out.print("user:");
                String user = sc.nextLine();
                System.out.print("from account:");
                String account1 = sc.nextLine();
                System.out.print("to account:");
                String account2 = sc.nextLine();
                System.out.print("amount:");
                Long amount = sc.nextLong();
                Long amount1 = getAmountfromAcc(account1, user);
                Long amount2 = getAmountfromAcc(account2, user);
                JSONObject obj1 = new JSONObject();
                JSONObject obj2 = new JSONObject();
                obj1.put("name", account1);
                obj1.put("amount", amount1 - amount);
                String obj11 = obj1.toString();
                transferStuff(obj11, user);

                obj2.put("name", account2);
                obj2.put("amount", amount2 + amount);
                String obj22 = obj2.toString();
                transferStuff(obj22, user);
                System.out.print("accounts updated\n");

            } else
                System.out.print("incorrect command\n");
        }
    }
}

From source file:org.apache.hadoop.hdfs.server.datanode.fsdataset.impl.BlockPoolSlice.java

/**
 * Read in the cached DU value and return it if it is less than 600 seconds
 * old (DU update interval). Slight imprecision of dfsUsed is not critical
 * and skipping DU can significantly shorten the startup time.
 * If the cached value is not available or too old, -1 is returned.
 *//*from www  .j a v a 2  s  .  co  m*/
long loadDfsUsed() {
    long cachedDfsUsed;
    long mtime;
    Scanner sc;

    try {
        sc = new Scanner(new File(currentDir, DU_CACHE_FILE), "UTF-8");
    } catch (FileNotFoundException fnfe) {
        return -1;
    }

    try {
        // Get the recorded dfsUsed from the file.
        if (sc.hasNextLong()) {
            cachedDfsUsed = sc.nextLong();
        } else {
            return -1;
        }
        // Get the recorded mtime from the file.
        if (sc.hasNextLong()) {
            mtime = sc.nextLong();
        } else {
            return -1;
        }

        // Return the cached value if mtime is okay.
        if (mtime > 0 && (Time.now() - mtime < 600000L)) {
            FsDatasetImpl.LOG.info("Cached dfsUsed found for " + currentDir + ": " + cachedDfsUsed);
            return cachedDfsUsed;
        }
        return -1;
    } finally {
        sc.close();
    }
}

From source file:com.tesora.dve.tools.CLIBuilder.java

protected Long scanLong(Scanner scanner, String exists) throws PEException {
    if (hasRequiredArg(scanner, exists)) {
        try {//from   w ww .j a  v a 2 s.  co m
            return scanner.nextLong();
        } catch (final Exception e) {
            throw new PEException("Failed to parse long parameter", e);
        }
    }

    return null;
}

From source file:org.apache.hadoop.hdfs.TestDFSShell.java

private static void runCount(String path, long dirs, long files, FsShell shell) throws IOException {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    PrintStream out = new PrintStream(bytes);
    PrintStream oldOut = System.out;
    System.setOut(out);/* w w w  . j  a  va  2s  .  co  m*/
    Scanner in = null;
    String results = null;
    try {
        runCmd(shell, "-count", path);
        results = bytes.toString();
        in = new Scanner(results);
        assertEquals(dirs, in.nextLong());
        assertEquals(files, in.nextLong());
    } finally {
        if (in != null) {
            in.close();
        }
        IOUtils.closeStream(out);
        System.setOut(oldOut);
        System.out.println("results:\n" + results);
    }
}