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.spatialHadoop.operations.DistributedJoin.java

private static long selfJoinLocal(Path in, Path out, OperationsParams params) throws IOException {
    if (isOneShotReadMode) {
        // Ensure all objects are read in one shot
        params.setInt(SpatialSite.MaxBytesInOneRead, -1);
        params.setInt(SpatialSite.MaxShapesInOneRead, -1);
    } else {/*from ww w. ja v  a  2 s  .  c  o  m*/
        params.setInt(SpatialSite.MaxBytesInOneRead, maxBytesInOneRead);
        params.setInt(SpatialSite.MaxShapesInOneRead, maxShapesInOneRead);
    }
    ShapeArrayInputFormat inputFormat = new ShapeArrayInputFormat();
    JobConf job = new JobConf(params);
    FileInputFormat.addInputPath(job, in);
    InputSplit[] splits = inputFormat.getSplits(job, 1);
    FileSystem outFs = out.getFileSystem(params);
    final PrintStream writer = new PrintStream(outFs.create(out));

    // Process all input files
    long resultSize = 0;
    for (InputSplit split : splits) {
        ShapeArrayRecordReader reader = new ShapeArrayRecordReader(job, (FileSplit) split);
        final Text temp = new Text();

        Rectangle key = reader.createKey();
        ArrayWritable value = reader.createValue();
        if (reader.next(key, value)) {
            Shape[] writables = (Shape[]) value.get();
            resultSize += SpatialAlgorithms.SelfJoin_planeSweep(writables, true,
                    new OutputCollector<Shape, Shape>() {
                        @Override
                        public void collect(Shape r, Shape s) throws IOException {
                            writer.print(r.toText(temp));
                            writer.print(",");
                            writer.println(s.toText(temp));
                        }
                    }, null);
            if (reader.next(key, value)) {
                throw new RuntimeException("Error! Not all values read in one shot.");
            }
        }

        reader.close();
    }
    writer.close();

    return resultSize;
}

From source file:org.apache.accumulo.examples.simple.mapreduce.bulk.BulkIngestExample.java

@Override
public int run(String[] args) {
    Opts opts = new Opts();
    opts.parseArgs(BulkIngestExample.class.getName(), args);

    Configuration conf = getConf();
    PrintStream out = null;
    try {/* ww w .j a v  a2  s.c om*/
        Job job = JobUtil.getJob(conf);
        job.setJobName("bulk ingest example");
        job.setJarByClass(this.getClass());

        job.setInputFormatClass(TextInputFormat.class);

        job.setMapperClass(MapClass.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        job.setReducerClass(ReduceClass.class);
        job.setOutputFormatClass(AccumuloFileOutputFormat.class);
        opts.setAccumuloConfigs(job);

        Connector connector = opts.getConnector();

        TextInputFormat.setInputPaths(job, new Path(opts.inputDir));
        AccumuloFileOutputFormat.setOutputPath(job, new Path(opts.workDir + "/files"));

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

        Collection<Text> splits = connector.tableOperations().listSplits(opts.tableName, 100);
        for (Text split : splits)
            out.println(new String(Base64.encodeBase64(TextUtil.getBytes(split))));

        job.setNumReduceTasks(splits.size() + 1);
        out.close();

        job.setPartitionerClass(RangePartitioner.class);
        RangePartitioner.setSplitFile(job, opts.workDir + "/splits.txt");

        job.waitForCompletion(true);
        Path failures = new Path(opts.workDir, "failures");
        fs.delete(failures, true);
        fs.mkdirs(new Path(opts.workDir, "failures"));
        connector.tableOperations().importDirectory(opts.tableName, opts.workDir + "/files",
                opts.workDir + "/failures", false);

    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (out != null)
            out.close();
    }

    return 0;
}

From source file:es.cnio.bioinfo.bicycle.gatk.MethylationFilePair.java

private void flushCrick() {
    PrintStream stream;
    try {//ww  w.  j a  v a  2s  . c om
        stream = new PrintStream(new BufferedOutputStream(new FileOutputStream(crickFile, true)));
        stream.print(crickBuffer.toString());
        crickBuffer.setLength(0);
        stream.flush();
        stream.close();
        printCrickCount = 0;
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    }
}

From source file:es.cnio.bioinfo.bicycle.gatk.MethylationFilePair.java

private void flushWatson() {
    PrintStream stream;
    try {//  w w w  .  j ava2 s.c o  m
        stream = new PrintStream(new BufferedOutputStream(new FileOutputStream(watsonFile, true)));
        stream.print(watsonBuffer.toString());
        watsonBuffer.setLength(0);
        stream.flush();
        stream.close();
        printWatsonCount = 0;
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.netscape.cms.publish.publishers.FileBasedPublisher.java

/**
 * Publishes a object to the ldap directory.
 *
 * @param conn a Ldap connection//from w  ww  . j a v a2 s. c  om
 *            (null if LDAP publishing is not enabled)
 * @param dn dn of the ldap entry to publish cert
 *            (null if LDAP publishing is not enabled)
 * @param object object to publish
 *            (java.security.cert.X509Certificate or,
 *            java.security.cert.X509CRL)
 */
public void publish(LDAPConnection conn, String dn, Object object) throws ELdapException {
    CMS.debug("FileBasedPublisher: publish");

    try {
        if (object instanceof X509Certificate) {
            X509Certificate cert = (X509Certificate) object;
            BigInteger sno = cert.getSerialNumber();
            String name = mDir + File.separator + "cert-" + sno.toString();
            if (mDerAttr) {
                FileOutputStream fos = null;
                try {
                    String fileName = name + ".der";
                    fos = new FileOutputStream(fileName);
                    fos.write(cert.getEncoded());
                } finally {
                    if (fos != null)
                        fos.close();
                }
            }
            if (mB64Attr) {
                String fileName = name + ".b64";
                PrintStream ps = null;
                Base64OutputStream b64 = null;
                FileOutputStream fos = null;
                try {
                    fos = new FileOutputStream(fileName);
                    ByteArrayOutputStream output = new ByteArrayOutputStream();
                    b64 = new Base64OutputStream(new PrintStream(new FilterOutputStream(output)));
                    b64.write(cert.getEncoded());
                    b64.flush();
                    ps = new PrintStream(fos);
                    ps.print(output.toString("8859_1"));
                } finally {
                    if (ps != null) {
                        ps.close();
                    }
                    if (b64 != null) {
                        b64.close();
                    }
                    if (fos != null)
                        fos.close();
                }
            }
        } else if (object instanceof X509CRL) {
            X509CRL crl = (X509CRL) object;
            String[] namePrefix = getCrlNamePrefix(crl, mTimeStamp.equals("GMT"));
            String baseName = mDir + File.separator + namePrefix[0];
            String tempFile = baseName + ".temp";
            ZipOutputStream zos = null;
            byte[] encodedArray = null;
            File destFile = null;
            String destName = null;
            File renameFile = null;

            if (mDerAttr) {
                FileOutputStream fos = null;
                try {
                    fos = new FileOutputStream(tempFile);
                    encodedArray = crl.getEncoded();
                    fos.write(encodedArray);
                } finally {
                    if (fos != null)
                        fos.close();
                }
                if (mZipCRL) {
                    try {
                        zos = new ZipOutputStream(new FileOutputStream(baseName + ".zip"));
                        zos.setLevel(mZipLevel);
                        zos.putNextEntry(new ZipEntry(baseName + ".der"));
                        zos.write(encodedArray, 0, encodedArray.length);
                        zos.closeEntry();
                    } finally {
                        if (zos != null)
                            zos.close();
                    }
                }
                destName = baseName + ".der";
                destFile = new File(destName);

                if (destFile.exists()) {
                    destFile.delete();
                }
                renameFile = new File(tempFile);
                renameFile.renameTo(destFile);

                if (mLatestCRL) {
                    String linkExt = ".";
                    if (mLinkExt != null && mLinkExt.length() > 0) {
                        linkExt += mLinkExt;
                    } else {
                        linkExt += "der";
                    }
                    String linkName = mDir + File.separator + namePrefix[1] + linkExt;
                    createLink(linkName, destName);
                    if (mZipCRL) {
                        linkName = mDir + File.separator + namePrefix[1] + ".zip";
                        createLink(linkName, baseName + ".zip");
                    }
                }
            }

            // output base64 file
            if (mB64Attr == true) {
                if (encodedArray == null)
                    encodedArray = crl.getEncoded();
                FileOutputStream fos = null;
                try {
                    fos = new FileOutputStream(tempFile);
                    fos.write(Utils.base64encode(encodedArray, true).getBytes());
                } finally {
                    if (fos != null)
                        fos.close();
                }
                destName = baseName + ".b64";
                destFile = new File(destName);

                if (destFile.exists()) {
                    destFile.delete();
                }
                renameFile = new File(tempFile);
                renameFile.renameTo(destFile);
            }
            purgeExpiredFiles();
            purgeExcessFiles();
        }
    } catch (IOException e) {
        mLogger.log(ILogger.EV_SYSTEM, ILogger.S_OTHER, ILogger.LL_FAILURE,
                CMS.getLogMessage("PUBLISH_FILE_PUBLISHER_ERROR", e.toString()));
    } catch (CertificateEncodingException e) {
        mLogger.log(ILogger.EV_SYSTEM, ILogger.S_OTHER, ILogger.LL_FAILURE,
                CMS.getLogMessage("PUBLISH_FILE_PUBLISHER_ERROR", e.toString()));
    } catch (CRLException e) {
        mLogger.log(ILogger.EV_SYSTEM, ILogger.S_OTHER, ILogger.LL_FAILURE,
                CMS.getLogMessage("PUBLISH_FILE_PUBLISHER_ERROR", e.toString()));
    }
}

From source file:org.apache.pig.backend.hadoop.executionengine.HExecutionEngine.java

@Override
public void explain(LogicalPlan lp, PigContext pc, PrintStream ps, String format, boolean verbose, File file,
        String suffix) throws PlanException, VisitorException, IOException, FrontendException {

    PrintStream pps = ps;
    PrintStream eps = ps;//  ww  w .j ava  2 s . c om
    boolean isFetchable = false;
    try {
        if (file != null) {
            pps = new PrintStream(new File(file, "physical_plan-" + suffix));
            eps = new PrintStream(new File(file, "exec_plan-" + suffix));
        }

        PhysicalPlan pp = compile(lp, pc.getProperties());
        pp.explain(pps, format, verbose);

        MapRedUtil.checkLeafIsStore(pp, pigContext);
        isFetchable = FetchOptimizer.isPlanFetchable(pc, pp);
        if (isFetchable) {
            new FetchLauncher(pigContext).explain(pp, pc, eps, format);
            return;
        }
        launcher.explain(pp, pigContext, eps, format, verbose);
    } finally {
        launcher.reset();
        if (isFetchable)
            pigContext.getProperties().remove(PigImplConstants.CONVERTED_TO_FETCH);
        //Only close the stream if we opened it.
        if (file != null) {
            pps.close();
            eps.close();
        }
    }
}

From source file:org.apache.pig.piggybank.test.storage.TestXMLLoader.java

/**
 * This test checks that a multi-line tag spanning two splits should be
 * matched.// ww  w  .j  a v a 2s  .  c  om
 * @throws Exception
 */
public void testXMLLoaderShouldMatchTagSpanningSplits() throws Exception {
    Configuration conf = new Configuration();
    long blockSize = 512;
    conf.setLong("fs.local.block.size", blockSize);
    conf.setLong(MRConfiguration.MAX_SPLIT_SIZE, blockSize);

    String tagName = "event";
    File tempFile = File.createTempFile("long-file", ".xml");
    FileSystem localFs = FileSystem.getLocal(conf);
    FSDataOutputStream directOut = localFs.create(new Path(tempFile.getAbsolutePath()), true);

    String matchingElement = "<event>\ndata\n</event>\n";
    long pos = 0;
    int matchingCount = 0;
    PrintStream ps = new PrintStream(directOut);
    // 1- Write some elements that fit completely in the first block
    while (pos + 2 * matchingElement.length() < blockSize) {
        ps.print(matchingElement);
        pos += matchingElement.length();
        matchingCount++;
    }
    // 2- Write a long element that spans multiple lines and multiple blocks
    String longElement = matchingElement.replace("data",
            "data\ndata\ndata\ndata\ndata\ndata\ndata\ndata\ndata\ndata\ndata\n");
    ps.print(longElement);
    pos += longElement.length();
    matchingCount++;
    // 3- Write some more elements to fill in the second block completely
    while (pos < 2 * blockSize) {
        ps.print(matchingElement);
        pos += matchingElement.length();
        matchingCount++;
    }
    ps.close();

    PigServer pig = new PigServer(LOCAL, conf);
    String tempFileName = tempFile.getAbsolutePath().replace("\\", "\\\\");
    String query = "A = LOAD '" + tempFileName
            + "' USING org.apache.pig.piggybank.storage.XMLLoader('event') as (doc:chararray);";
    pig.registerQuery(query);
    Iterator<?> it = pig.openIterator("A");

    int count = 0;
    while (it.hasNext()) {
        Tuple tuple = (Tuple) it.next();
        if (tuple == null)
            break;
        else {
            if (tuple.size() > 0) {
                count++;
                // Make sure the returned text is a proper XML element
                DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                Document doc = docBuilder.parse(new ByteArrayInputStream(((String) tuple.get(0)).getBytes()));
                assertTrue(doc.getDocumentElement().getNodeName().equals(tagName));
            }
        }
    }
    assertEquals(matchingCount, count);
}

From source file:net.morphbank.mbsvc3.webservices.RestService.java

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    // //from   w w  w. j  a  v a  2 s .com
    String method = request.getParameter(RequestParams.PARAM_METHOD);
    PrintStream out = new PrintStream(response.getOutputStream());
    try {
        out.println("<html><body>");
        if (RequestParams.METHOD_REMOTE_UPDATE.equals(method)) {
            UpdateRemote.update(request, response);
        } else if (RequestParams.METHOD_FIX_UUID.equals(method)) {
            //TODO  call fix uuid and fix id
            response.setContentType("text/html");
            UUIDServices uuidServices = new UUIDServices(out);
            int noUUIDFixed = uuidServices.fixAllMissingUUIDs();
            out.println("<p>No of fixed MissingUUIDs: " + noUUIDFixed + "</p>");
            int noIDFixed = uuidServices.fixAllMissingIds();
            out.println("<p>No of fixed IDs: " + noIDFixed + "</p>");
        } else {
            // TODO turn over to processrequest
            response.setContentType("text/xml");
            out.println("<p>Here I am</p>");

        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        out.println("</body></html>");
        out.close();
    }
}

From source file:org.apache.beam.sdk.io.TextIOTest.java

License:asdf

/**
 * Create a zip file with the given lines.
 *
 * @param expected A list of expected lines, populated in the zip file.
 * @param filename Optionally zip file name (can be null).
 * @param fieldsEntries Fields to write in zip entries.
 * @return The zip filename./*  w  w w  .  ja v a  2  s  .  co m*/
 * @throws Exception In case of a failure during zip file creation.
 */
private String createZipFile(List<String> expected, String filename, String[]... fieldsEntries)
        throws Exception {
    File tmpFile = tempFolder.resolve(filename).toFile();
    String tmpFileName = tmpFile.getPath();

    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(tmpFile));
    PrintStream writer = new PrintStream(out, true /* auto-flush on write */);

    int index = 0;
    for (String[] entry : fieldsEntries) {
        out.putNextEntry(new ZipEntry(Integer.toString(index)));
        for (String field : entry) {
            writer.println(field);
            expected.add(field);
        }
        out.closeEntry();
        index++;
    }

    writer.close();
    out.close();

    return tmpFileName;
}

From source file:at.ofai.music.util.WormFileParseException.java

public void writeBeatTrackFile(String fileName) throws Exception {
    if (fileName.endsWith(".txt") || fileName.endsWith(".csv"))
        writeBeatsAsText(fileName);//from  ww  w .j  av a2 s  .  c  om
    else {
        PrintStream out = new PrintStream(new File(fileName));
        out.println("MFile 0 1 500");
        out.println("MTrk");
        out.println("     0 Tempo 500000");
        int time = 0;
        for (Iterator<Event> it = iterator(); it.hasNext();) {
            Event e = it.next();
            time = (int) Math.round(1000 * e.keyDown);
            out.printf("%6d On   ch=%3d n=%3d v=%3d\n", time, e.midiChannel, e.midiPitch, e.midiVelocity);
            out.printf("%6d Off  ch=%3d n=%3d v=%3d\n", time, e.midiChannel, e.midiPitch, e.flags);
        }
        out.printf("%6d Meta TrkEnd\nTrkEnd\n", time);
        out.close();
    }
}