Code example for PrintStream

Methods: PrintStream.println, checkError, println

0
        String path = SNAPSHOT_DIR + "/" + name + "-" + startMillis + ".snapshot";
        long start = System.currentTimeMillis();
        OutputStream outputStream = null;
        try { 
            outputStream = new BufferedOutputStream(new FileOutputStream(path));
            PrintStream out = new PrintStream(outputStream);
            generateSnapshotHeader(name, packageInfo, out);
            if (out.checkError()) {
                throw new IOException();
            } 
            BinaryHprofWriter.write(samplingProfiler.getHprofData(), outputStream);
        } catch (IOException e) {
            Log.e(TAG, "Error writing snapshot to " + path, e);
            return; 
        } finally { 
            IoUtils.closeQuietly(outputStream);
        } 
        // set file readable to the world so that SamplingProfilerService 
        // can put it to dropbox 
        new File(path).setReadable(true, false);
 
        long elapsed = System.currentTimeMillis() - start;
        Log.i(TAG, "Wrote snapshot " + path + " in " + elapsed + "ms.");
        samplingProfiler.start(samplingProfilerMilliseconds);
    } 
 
    /** 
     * generate header for snapshots, with the following format 
     * (like an HTTP header but without the \r): 
     * 
     * Version: <version number of profiler>\n 
     * Process: <process name>\n 
     * Package: <package name, if exists>\n 
     * Package-Version: <version number of the package, if exists>\n 
     * Build: <fingerprint>\n 
     * \n 
     * <the actual snapshot content begins here...> 
     */ 
    private static void generateSnapshotHeader(String processName, PackageInfo packageInfo,
            PrintStream out) {
        // profiler version 
        out.println("Version: 3");
        out.println("Process: " + processName);
        if (packageInfo != null) {
            out.println("Package: " + packageInfo.packageName);
            out.println("Package-Version: " + packageInfo.versionCode);
        } 
        out.println("Build: " + Build.FINGERPRINT);
        // single blank line means the end of snapshot header. 
        out.println();
    } 
}