Example usage for java.io PrintStream close

List of usage examples for java.io PrintStream close

Introduction

In this page you can find the example usage for java.io PrintStream close.

Prototype

public void close() 

Source Link

Document

Closes the stream.

Usage

From source file:edu.umn.cs.sthadoop.trajectory.KNNDTW.java

private static <S extends Shape> long knnLocal(Path inFile, Path outPath, OperationsParams params)
        throws IOException, InterruptedException {
    int iterations = 0;
    FileSystem fs = inFile.getFileSystem(params);
    Point queryPoint = (Point) OperationsParams.getShape(params, "point");
    int k = params.getInt("k", 1);
    // Top-k objects are retained in this object
    PriorityQueue<ShapeWithDistance<S>> knn = new KNNObjects<ShapeWithDistance<S>>(k);

    SpatialInputFormat3<Rectangle, Shape> inputFormat = new SpatialInputFormat3<Rectangle, Shape>();

    final GlobalIndex<Partition> gIndex = SpatialSite.getGlobalIndex(fs, inFile);
    double kthDistance = Double.MAX_VALUE;
    if (gIndex != null) {
        // There is a global index, use it
        PriorityQueue<ShapeWithDistance<Partition>> partitionsToProcess = new PriorityQueue<KNNDTW.ShapeWithDistance<Partition>>() {
            {//from ww  w . java 2s. co  m
                initialize(gIndex.size());
            }

            @Override
            protected boolean lessThan(Object a, Object b) {
                return ((ShapeWithDistance<Partition>) a).distance < ((ShapeWithDistance<Partition>) b).distance;
            }
        };
        for (Partition p : gIndex) {
            double distance = p.getMinDistanceTo(queryPoint.x, queryPoint.y);
            partitionsToProcess.insert(new ShapeWithDistance<Partition>(p.clone(), distance));
        }

        while (partitionsToProcess.size() > 0 && partitionsToProcess.top().distance <= kthDistance) {

            ShapeWithDistance<Partition> partitionToProcess = partitionsToProcess.pop();
            // Process this partition
            Path partitionPath = new Path(inFile, partitionToProcess.shape.filename);
            long length = fs.getFileStatus(partitionPath).getLen();
            FileSplit fsplit = new FileSplit(partitionPath, 0, length, new String[0]);
            RecordReader<Rectangle, Iterable<Shape>> reader = inputFormat.createRecordReader(fsplit, null);
            if (reader instanceof SpatialRecordReader3) {
                ((SpatialRecordReader3) reader).initialize(fsplit, params);
            } else if (reader instanceof RTreeRecordReader3) {
                ((RTreeRecordReader3) reader).initialize(fsplit, params);
            } else if (reader instanceof HDFRecordReader) {
                ((HDFRecordReader) reader).initialize(fsplit, params);
            } else {
                throw new RuntimeException("Unknown record reader");
            }
            iterations++;

            while (reader.nextKeyValue()) {
                Iterable<Shape> shapes = reader.getCurrentValue();
                for (Shape shape : shapes) {
                    double distance = shape.distanceTo(queryPoint.x, queryPoint.y);
                    if (distance <= kthDistance)
                        knn.insert(new ShapeWithDistance<S>((S) shape.clone(), distance));
                }
            }
            reader.close();

            if (knn.size() >= k)
                kthDistance = knn.top().distance;
        }
    } else {
        // No global index, have to scan the whole file
        Job job = new Job(params);
        SpatialInputFormat3.addInputPath(job, inFile);
        List<InputSplit> splits = inputFormat.getSplits(job);

        for (InputSplit split : splits) {
            RecordReader<Rectangle, Iterable<Shape>> reader = inputFormat.createRecordReader(split, null);
            if (reader instanceof SpatialRecordReader3) {
                ((SpatialRecordReader3) reader).initialize(split, params);
            } else if (reader instanceof RTreeRecordReader3) {
                ((RTreeRecordReader3) reader).initialize(split, params);
            } else if (reader instanceof HDFRecordReader) {
                ((HDFRecordReader) reader).initialize(split, params);
            } else {
                throw new RuntimeException("Unknown record reader");
            }
            iterations++;

            while (reader.nextKeyValue()) {
                Iterable<Shape> shapes = reader.getCurrentValue();
                for (Shape shape : shapes) {
                    double distance = shape.distanceTo(queryPoint.x, queryPoint.y);
                    knn.insert(new ShapeWithDistance<S>((S) shape.clone(), distance));
                }
            }

            reader.close();
        }
        if (knn.size() >= k)
            kthDistance = knn.top().distance;
    }
    long resultCount = knn.size();
    if (outPath != null && params.getBoolean("output", true)) {
        FileSystem outFS = outPath.getFileSystem(params);
        PrintStream ps = new PrintStream(outFS.create(outPath));
        Vector<ShapeWithDistance<S>> resultsOrdered = new Vector<ShapeWithDistance<S>>((int) resultCount);
        resultsOrdered.setSize((int) resultCount);
        while (knn.size() > 0) {
            ShapeWithDistance<S> nextAnswer = knn.pop();
            resultsOrdered.set(knn.size(), nextAnswer);
        }

        Text text = new Text();
        for (ShapeWithDistance<S> answer : resultsOrdered) {
            text.clear();
            TextSerializerHelper.serializeDouble(answer.distance, text, ',');
            answer.shape.toText(text);
            ps.println(text);
        }
        ps.close();
    }
    TotalIterations.addAndGet(iterations);
    return resultCount;

}

From source file:com.at.lic.LicenseControl.java

private String getMAC() throws Exception {
    String mac = "1:2:3:4:5:6:7:8"; // default mac address

    InetAddress addr = InetAddress.getLocalHost();
    NetworkInterface ni = NetworkInterface.getByInetAddress(addr);

    if (ni.isLoopback() || ni.isVirtual()) {
        ni = null;/*from  w w  w.j a  v a2 s. com*/
        Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
        while (nis.hasMoreElements()) {
            NetworkInterface aNI = (NetworkInterface) nis.nextElement();
            if (!aNI.isLoopback() && !aNI.isVirtual()) {
                ni = aNI;
                break;
            }
        }
    }

    if (ni != null) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintStream ps = new PrintStream(baos);
        byte[] HAs = ni.getHardwareAddress();
        for (int i = 0; i < HAs.length; i++) {
            ps.format("%02X:", HAs[i]);
        }
        mac = baos.toString();
        if (mac.length() > 0) {
            mac = mac.replaceFirst(":$", "");
        }

        ps.close();
        baos.close();

    }

    return mac;
}

From source file:Client.java

public void run() {
    if (clientSocket == null) {
        return;//from www .j  a v a2s .c  om
    }

    PrintStream out = null;

    Utilities.printMsg("creating output stream");

    try {
        out = new PrintStream(clientSocket.getOutputStream());
    } catch (IOException e) {
        System.err.println("Error binding output to socket, " + e);
        System.exit(1);
    }

    Utilities.printMsg("writing current date");

    Date d = new Date();
    out.println(d);

    try {
        out.close();
        clientSocket.close();
    } catch (IOException e) {
    }
}

From source file:org.apache.oodt.cas.filemgr.cli.action.TestIngestProductCliAction.java

private File createFlatReference() throws IOException {
    File reference = new File(tmpDir, FLAT_REF_NAME);
    PrintStream ps = null;
    try {//from   w  w  w .  j  a v a  2s  .  co m
        ps = new PrintStream(new FileOutputStream(reference));
        ps.println("This is a test sub-reference file");
    } finally {
        ps.close();
    }
    return reference;
}

From source file:de.uni_koblenz.jgralab.utilities.tg2whatever.Tg2Whatever.java

public void convert() throws IOException {
    if ((outputName == null) || outputName.equals("")) {
        convert(System.out);/*from   w w w  . ja v  a 2 s . c om*/
    } else {
        PrintStream out = new PrintStream(new FileOutputStream(outputName));
        convert(out);
        out.close();
    }
}

From source file:com.starit.diamond.server.service.TimerTaskService.java

private void productConfigInfoCheckBatchFile(List<ConfigInfo> configInfos) {
    String GROUP = "$GROUP$";
    String DATAID = "$DATAID$";
    String urlPattern = "$GROUP$\t$DATAID$";
    try {//from ww  w. j  a  v a 2s .c  om
        File f = new File("slf_check_urls.txt");
        fn = f.getAbsolutePath();
        System.out.println(" :" + f.getAbsolutePath());
        PrintStream out = new PrintStream(new FileOutputStream(f));
        for (ConfigInfo configInfo : configInfos) {
            String url = urlPattern.replace(GROUP, configInfo.getGroup());
            url = url.replace(DATAID, configInfo.getDataId());
            out.println(url);
        }
        out.flush();
        out.close();
    } catch (FileNotFoundException e) {
        throw new RuntimeException("can not create check.sh");
    }
}

From source file:org.apache.pig.test.TestFilterOpNumeric.java

@Test
public void testNumericNeq() throws Throwable {
    File tmpFile = File.createTempFile("test", "txt");
    PrintStream ps = new PrintStream(new FileOutputStream(tmpFile));
    for (int i = 0; i < LOOP_COUNT; i++) {
        if (i % 5 == 0) {
            ps.println("1:1");
        } else {/*from   w  w  w .ja v a2  s  .  c  om*/
            ps.println("2:3");
        }
    }
    ps.close();
    pig.registerQuery("A=load '" + Util.encodeEscape(Util.generateURI(tmpFile.toString(), pig.getPigContext()))
            + "' using " + PigStorage.class.getName() + "(':');");
    String query = "A = filter A by $0 != $1;";
    log.info(query);
    pig.registerQuery(query);
    Iterator<Tuple> it = pig.openIterator("A");
    tmpFile.delete();
    while (it.hasNext()) {
        Tuple t = it.next();
        Double first = Double.valueOf(t.get(0).toString());
        Double second = Double.valueOf(t.get(1).toString());
        assertFalse(first.equals(second));
    }
}

From source file:edu.oregonstate.eecs.mcplan.ml.KernelPrincipalComponentsAnalysis.java

public void writeModel(final File directory, final int Nbases) {
    try {//w ww. ja  v a  2 s.  c  o m
        final PrintStream eigenvectors_out = new PrintStream(
                new FileOutputStream(new File(directory, "kpca-eigenvectors.csv")));
        System.out.println("!!! eigenvectors.size() = " + eigenvectors.size());
        Csv.write(eigenvectors_out, eigenvectors.subList(0, Nbases));
        eigenvectors_out.close();

        final PrintStream eigenvalues_out = new PrintStream(
                new FileOutputStream(new File(directory, "kpca-eigenvalues.csv")));
        Csv.write(eigenvalues_out, eigenvalues);
        eigenvalues_out.close();
    } catch (final Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:edu.umn.cs.sthadoop.operations.HSPKNNQ.java

private static <S extends Shape> long knnLocal(Path inFile, Path outPath, OperationsParams params)
        throws IOException, InterruptedException {
    int iterations = 0;
    FileSystem fs = inFile.getFileSystem(params);
    Point queryPoint = (Point) OperationsParams.getShape(params, "point");
    int k = params.getInt("k", 1);
    // Top-k objects are retained in this object
    PriorityQueue<ShapeWithDistance<S>> knn = new KNNObjects<ShapeWithDistance<S>>(k);

    SpatialInputFormat3<Rectangle, Shape> inputFormat = new SpatialInputFormat3<Rectangle, Shape>();

    final GlobalIndex<Partition> gIndex = SpatialSite.getGlobalIndex(fs, inFile);
    double kthDistance = Double.MAX_VALUE;
    if (gIndex != null) {
        // There is a global index, use it
        PriorityQueue<ShapeWithDistance<Partition>> partitionsToProcess = new PriorityQueue<HSPKNNQ.ShapeWithDistance<Partition>>() {
            {/* w ww .  j a  va2s .c o m*/
                initialize(gIndex.size());
            }

            @Override
            protected boolean lessThan(Object a, Object b) {
                return ((ShapeWithDistance<Partition>) a).distance < ((ShapeWithDistance<Partition>) b).distance;
            }
        };
        for (Partition p : gIndex) {
            double distance = p.getMinDistanceTo(queryPoint.x, queryPoint.y);
            partitionsToProcess.insert(new ShapeWithDistance<Partition>(p.clone(), distance));
        }

        while (partitionsToProcess.size() > 0 && partitionsToProcess.top().distance <= kthDistance) {

            ShapeWithDistance<Partition> partitionToProcess = partitionsToProcess.pop();
            // Process this partition
            Path partitionPath = new Path(inFile, partitionToProcess.shape.filename);
            long length = fs.getFileStatus(partitionPath).getLen();
            FileSplit fsplit = new FileSplit(partitionPath, 0, length, new String[0]);
            RecordReader<Rectangle, Iterable<Shape>> reader = inputFormat.createRecordReader(fsplit, null);
            if (reader instanceof SpatialRecordReader3) {
                ((SpatialRecordReader3) reader).initialize(fsplit, params);
            } else if (reader instanceof RTreeRecordReader3) {
                ((RTreeRecordReader3) reader).initialize(fsplit, params);
            } else if (reader instanceof HDFRecordReader) {
                ((HDFRecordReader) reader).initialize(fsplit, params);
            } else {
                throw new RuntimeException("Unknown record reader");
            }
            iterations++;

            while (reader.nextKeyValue()) {
                Iterable<Shape> shapes = reader.getCurrentValue();
                for (Shape shape : shapes) {
                    double distance = shape.distanceTo(queryPoint.x, queryPoint.y);
                    if (distance <= kthDistance)
                        knn.insert(new ShapeWithDistance<S>((S) shape.clone(), distance));
                }
            }
            reader.close();

            if (knn.size() >= k)
                kthDistance = knn.top().distance;
        }
    } else {
        // No global index, have to scan the whole file
        Job job = new Job(params);
        SpatialInputFormat3.addInputPath(job, inFile);
        List<InputSplit> splits = inputFormat.getSplits(job);

        for (InputSplit split : splits) {
            RecordReader<Rectangle, Iterable<Shape>> reader = inputFormat.createRecordReader(split, null);
            if (reader instanceof SpatialRecordReader3) {
                ((SpatialRecordReader3) reader).initialize(split, params);
            } else if (reader instanceof RTreeRecordReader3) {
                ((RTreeRecordReader3) reader).initialize(split, params);
            } else if (reader instanceof HDFRecordReader) {
                ((HDFRecordReader) reader).initialize(split, params);
            } else {
                throw new RuntimeException("Unknown record reader");
            }
            iterations++;

            while (reader.nextKeyValue()) {
                Iterable<Shape> shapes = reader.getCurrentValue();
                for (Shape shape : shapes) {
                    double distance = shape.distanceTo(queryPoint.x, queryPoint.y);
                    knn.insert(new ShapeWithDistance<S>((S) shape.clone(), distance));
                }
            }

            reader.close();
        }
        if (knn.size() >= k)
            kthDistance = knn.top().distance;
    }
    long resultCount = knn.size();
    if (outPath != null && params.getBoolean("output", true)) {
        FileSystem outFS = outPath.getFileSystem(params);
        PrintStream ps = new PrintStream(outFS.create(outPath));
        Vector<ShapeWithDistance<S>> resultsOrdered = new Vector<ShapeWithDistance<S>>((int) resultCount);
        resultsOrdered.setSize((int) resultCount);
        while (knn.size() > 0) {
            ShapeWithDistance<S> nextAnswer = knn.pop();
            resultsOrdered.set(knn.size(), nextAnswer);
        }

        Text text = new Text();
        for (ShapeWithDistance<S> answer : resultsOrdered) {
            text.clear();
            TextSerializerHelper.serializeDouble(answer.distance, text, ',');
            answer.shape.toText(text);
            ps.println(text);
        }
        ps.close();
    }
    TotalIterations.addAndGet(iterations);
    return resultCount;

}

From source file:org.apache.accumulo.server.test.randomwalk.shard.BulkInsert.java

private void sort(State state, FileSystem fs, String tableName, String seqFile, String outputDir,
        String workDir, int maxSplits) throws Exception {

    PrintStream out = new PrintStream(new BufferedOutputStream(fs.create(new Path(workDir + "/splits.txt"))));

    Connector conn = state.getConnector();

    Collection<Text> splits = conn.tableOperations().getSplits(tableName, maxSplits);
    for (Text split : splits)
        out.println(new String(Base64.encodeBase64(TextUtil.getBytes(split))));

    out.close();

    SortTool sortTool = new SortTool(seqFile, outputDir, workDir + "/splits.txt", splits);

    String[] args = new String[2];
    args[0] = "-libjars";
    args[1] = state.getMapReduceJars();//www.j  av a 2s  .  c  o m

    if (ToolRunner.run(CachedConfiguration.getInstance(), sortTool, args) != 0) {
        throw new Exception("Failed to run map/red verify");
    }
}