Search in sources :

Example 36 with BufferedWriter

use of java.io.BufferedWriter in project platform_frameworks_base by android.

the class CSVWriterFilter method onProcess.

@Override
protected void onProcess() {
    Log.v(TAG, "in csv writer on process");
    FrameValue sharpnessValue = getConnectedInputPort("sharpness").pullFrame().asFrameValue();
    float sharpness = ((Float) sharpnessValue.getValue()).floatValue();
    FrameValue overExposureValue = getConnectedInputPort("overExposure").pullFrame().asFrameValue();
    float overExposure = ((Float) overExposureValue.getValue()).floatValue();
    FrameValue underExposureValue = getConnectedInputPort("underExposure").pullFrame().asFrameValue();
    float underExposure = ((Float) underExposureValue.getValue()).floatValue();
    FrameValue colorfulnessValue = getConnectedInputPort("colorfulness").pullFrame().asFrameValue();
    float colorfulness = ((Float) colorfulnessValue.getValue()).floatValue();
    FrameValue contrastValue = getConnectedInputPort("contrastRating").pullFrame().asFrameValue();
    float contrast = ((Float) contrastValue.getValue()).floatValue();
    FrameValue brightnessValue = getConnectedInputPort("brightness").pullFrame().asFrameValue();
    float brightness = ((Float) brightnessValue.getValue()).floatValue();
    FrameValue motionValuesFrameValue = getConnectedInputPort("motionValues").pullFrame().asFrameValue();
    float[] motionValues = (float[]) motionValuesFrameValue.getValue();
    float vectorAccel = (float) Math.sqrt(Math.pow(motionValues[0], 2) + Math.pow(motionValues[1], 2) + Math.pow(motionValues[2], 2));
    FrameValue imageFileNameFrameValue = getConnectedInputPort("imageFileName").pullFrame().asFrameValue();
    String imageFileName = ((String) imageFileNameFrameValue.getValue());
    FrameValue csvFilePathFrameValue = getConnectedInputPort("csvFilePath").pullFrame().asFrameValue();
    String csvFilePath = ((String) csvFilePathFrameValue.getValue());
    if (mFirstTime) {
        try {
            FileWriter fileWriter = new FileWriter(csvFilePath + "/CSVFile.csv");
            BufferedWriter csvWriter = new BufferedWriter(fileWriter);
            csvWriter.write("FileName,Sharpness,OverExposure,UnderExposure,Colorfulness," + "ContrastRating,Brightness,Motion");
            csvWriter.newLine();
            csvWriter.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        mFirstTime = false;
    }
    try {
        Log.v(TAG, "about to write to file");
        FileWriter fileWriter = new FileWriter(csvFilePath + mFileName, true);
        BufferedWriter csvWriter = new BufferedWriter(fileWriter);
        csvWriter.write(imageFileName + "," + sharpness + "," + overExposure + "," + underExposure + "," + colorfulness + "," + contrast + "," + brightness + "," + vectorAccel);
        Log.v(TAG, "" + imageFileName + "," + sharpness + "," + overExposure + "," + underExposure + "," + colorfulness + "," + contrast + "," + brightness + "," + vectorAccel);
        csvWriter.newLine();
        csvWriter.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}
Also used : FileWriter(java.io.FileWriter) IOException(java.io.IOException) FrameValue(androidx.media.filterfw.FrameValue) BufferedWriter(java.io.BufferedWriter)

Example 37 with BufferedWriter

use of java.io.BufferedWriter in project antlr4 by antlr.

the class LogManager method save.

public void save(String filename) throws IOException {
    FileWriter fw = new FileWriter(filename);
    BufferedWriter bw = new BufferedWriter(fw);
    try {
        bw.write(toString());
    } finally {
        bw.close();
    }
}
Also used : FileWriter(java.io.FileWriter) BufferedWriter(java.io.BufferedWriter)

Example 38 with BufferedWriter

use of java.io.BufferedWriter in project platform_frameworks_base by android.

the class TaskPersister method writePersistedTaskIdsForUser.

@VisibleForTesting
void writePersistedTaskIdsForUser(@NonNull SparseBooleanArray taskIds, int userId) {
    if (userId < 0) {
        return;
    }
    final File persistedTaskIdsFile = getUserPersistedTaskIdsFile(userId);
    synchronized (mIoLock) {
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new FileWriter(persistedTaskIdsFile));
            for (int i = 0; i < taskIds.size(); i++) {
                if (taskIds.valueAt(i)) {
                    writer.write(String.valueOf(taskIds.keyAt(i)));
                    writer.newLine();
                }
            }
        } catch (Exception e) {
            Slog.e(TAG, "Error while writing taskIds file for user " + userId, e);
        } finally {
            IoUtils.closeQuietly(writer);
        }
    }
}
Also used : FileWriter(java.io.FileWriter) AtomicFile(android.util.AtomicFile) File(java.io.File) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) BufferedWriter(java.io.BufferedWriter) VisibleForTesting(com.android.internal.annotations.VisibleForTesting)

Example 39 with BufferedWriter

use of java.io.BufferedWriter in project platform_frameworks_base by android.

the class AppLaunch method testMeasureStartUpTime.

public void testMeasureStartUpTime() throws RemoteException, NameNotFoundException, IOException, InterruptedException {
    InstrumentationTestRunner instrumentation = (InstrumentationTestRunner) getInstrumentation();
    Bundle args = instrumentation.getArguments();
    mAm = ActivityManagerNative.getDefault();
    String launchDirectory = args.getString(KEY_LAUNCH_DIRECTORY);
    mTraceDirectoryStr = args.getString(KEY_TRACE_DIRECTORY);
    mDropCache = Boolean.parseBoolean(args.getString(KEY_DROP_CACHE));
    mSimplePerfCmd = args.getString(KEY_SIMPLEPPERF_CMD);
    mLaunchOrder = args.getString(KEY_LAUNCH_ORDER, LAUNCH_ORDER_CYCLIC);
    createMappings();
    parseArgs(args);
    checkAccountSignIn();
    // Root directory for applaunch file to log the app launch output
    // Will be useful in case of simpleperf command is used
    File launchRootDir = null;
    if (null != launchDirectory && !launchDirectory.isEmpty()) {
        launchRootDir = new File(launchDirectory);
        if (!launchRootDir.exists() && !launchRootDir.mkdirs()) {
            throw new IOException("Unable to create the destination directory");
        }
    }
    try {
        File launchSubDir = new File(launchRootDir, LAUNCH_SUB_DIRECTORY);
        if (!launchSubDir.exists() && !launchSubDir.mkdirs()) {
            throw new IOException("Unable to create the lauch file sub directory");
        }
        mFile = new File(launchSubDir, LAUNCH_FILE);
        mOutputStream = new FileOutputStream(mFile);
        mBufferedWriter = new BufferedWriter(new OutputStreamWriter(mOutputStream));
        // Root directory for trace file during the launches
        File rootTrace = null;
        File rootTraceSubDir = null;
        int traceBufferSize = 0;
        int traceDumpInterval = 0;
        Set<String> traceCategoriesSet = null;
        if (null != mTraceDirectoryStr && !mTraceDirectoryStr.isEmpty()) {
            rootTrace = new File(mTraceDirectoryStr);
            if (!rootTrace.exists() && !rootTrace.mkdirs()) {
                throw new IOException("Unable to create the trace directory");
            }
            rootTraceSubDir = new File(rootTrace, TRACE_SUB_DIRECTORY);
            if (!rootTraceSubDir.exists() && !rootTraceSubDir.mkdirs()) {
                throw new IOException("Unable to create the trace sub directory");
            }
            assertNotNull("Trace iteration parameter is mandatory", args.getString(KEY_TRACE_ITERATIONS));
            mTraceLaunchCount = Integer.parseInt(args.getString(KEY_TRACE_ITERATIONS));
            String traceCategoriesStr = args.getString(KEY_TRACE_CATEGORY, DEFAULT_TRACE_CATEGORIES);
            traceBufferSize = Integer.parseInt(args.getString(KEY_TRACE_BUFFERSIZE, DEFAULT_TRACE_BUFFER_SIZE));
            traceDumpInterval = Integer.parseInt(args.getString(KEY_TRACE_DUMPINTERVAL, DEFAULT_TRACE_DUMP_INTERVAL));
            traceCategoriesSet = new HashSet<String>();
            if (!traceCategoriesStr.isEmpty()) {
                String[] traceCategoriesSplit = traceCategoriesStr.split(DELIMITER);
                for (int i = 0; i < traceCategoriesSplit.length; i++) {
                    traceCategoriesSet.add(traceCategoriesSplit[i]);
                }
            }
        }
        // Get the app launch order based on launch order, trial launch,
        // launch iterations and trace iterations
        setLaunchOrder();
        for (LaunchOrder launch : mLaunchOrderList) {
            // launch time calculations.
            if (launch.getLaunchReason().equals(TRIAL_LAUNCH)) {
                // In the "applaunch.txt" file, trail launches is referenced using
                // "TRIAL_LAUNCH"
                long launchTime = startApp(launch.getApp(), true, launch.getLaunchReason());
                if (launchTime < 0) {
                    List<Long> appLaunchList = new ArrayList<Long>();
                    appLaunchList.add(-1L);
                    mNameToLaunchTime.put(launch.getApp(), appLaunchList);
                    // error should have already been logged by startApp
                    continue;
                }
                sleep(INITIAL_LAUNCH_IDLE_TIMEOUT);
                closeApp(launch.getApp(), true);
                dropCache();
                sleep(BETWEEN_LAUNCH_SLEEP_TIMEOUT);
            }
            // App launch times used for final calculation
            if (launch.getLaunchReason().contains(LAUNCH_ITERATION_PREFIX)) {
                long launchTime = -1;
                if (null != mNameToLaunchTime.get(launch.getApp())) {
                    long firstLaunchTime = mNameToLaunchTime.get(launch.getApp()).get(0);
                    if (firstLaunchTime < 0) {
                        // skip if the app has failures while launched first
                        continue;
                    }
                }
                // In the "applaunch.txt" file app launches are referenced using
                // "LAUNCH_ITERATION - ITERATION NUM"
                launchTime = startApp(launch.getApp(), true, launch.getLaunchReason());
                if (launchTime < 0) {
                    // if it fails once, skip the rest of the launches
                    List<Long> appLaunchList = new ArrayList<Long>();
                    appLaunchList.add(-1L);
                    mNameToLaunchTime.put(launch.getApp(), appLaunchList);
                    continue;
                } else {
                    if (null != mNameToLaunchTime.get(launch.getApp())) {
                        mNameToLaunchTime.get(launch.getApp()).add(launchTime);
                    } else {
                        List<Long> appLaunchList = new ArrayList<Long>();
                        appLaunchList.add(launchTime);
                        mNameToLaunchTime.put(launch.getApp(), appLaunchList);
                    }
                }
                sleep(POST_LAUNCH_IDLE_TIMEOUT);
                closeApp(launch.getApp(), true);
                dropCache();
                sleep(BETWEEN_LAUNCH_SLEEP_TIMEOUT);
            }
            // launch time calculations.
            if (launch.getLaunchReason().contains(TRACE_ITERATION_PREFIX)) {
                AtraceLogger atraceLogger = AtraceLogger.getAtraceLoggerInstance(getInstrumentation());
                // Start the trace
                try {
                    atraceLogger.atraceStart(traceCategoriesSet, traceBufferSize, traceDumpInterval, rootTraceSubDir, String.format("%s-%s", launch.getApp(), launch.getLaunchReason()));
                    startApp(launch.getApp(), true, launch.getLaunchReason());
                    sleep(POST_LAUNCH_IDLE_TIMEOUT);
                } finally {
                    // Stop the trace
                    atraceLogger.atraceStop();
                    closeApp(launch.getApp(), true);
                    dropCache();
                    sleep(BETWEEN_LAUNCH_SLEEP_TIMEOUT);
                }
            }
        }
    } finally {
        if (null != mBufferedWriter) {
            mBufferedWriter.close();
        }
    }
    for (String app : mNameToResultKey.keySet()) {
        StringBuilder launchTimes = new StringBuilder();
        for (Long launch : mNameToLaunchTime.get(app)) {
            launchTimes.append(launch);
            launchTimes.append(",");
        }
        mResult.putString(mNameToResultKey.get(app), launchTimes.toString());
    }
    instrumentation.sendStatus(0, mResult);
}
Also used : Bundle(android.os.Bundle) ArrayList(java.util.ArrayList) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) AtraceLogger(android.support.test.rule.logging.AtraceLogger) InstrumentationTestRunner(android.test.InstrumentationTestRunner) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File)

Example 40 with BufferedWriter

use of java.io.BufferedWriter in project androidannotations by androidannotations.

the class ViewServer method writeValue.

private static boolean writeValue(Socket client, String value) {
    boolean result;
    BufferedWriter out = null;
    try {
        OutputStream clientStream = client.getOutputStream();
        out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
        out.write(value);
        out.write("\n");
        out.flush();
        result = true;
    } catch (Exception e) {
        result = false;
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                result = false;
            }
        }
    }
    return result;
}
Also used : OutputStream(java.io.OutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter)

Aggregations

BufferedWriter (java.io.BufferedWriter)1723 IOException (java.io.IOException)824 FileWriter (java.io.FileWriter)801 File (java.io.File)718 OutputStreamWriter (java.io.OutputStreamWriter)654 FileOutputStream (java.io.FileOutputStream)313 BufferedReader (java.io.BufferedReader)242 Writer (java.io.Writer)157 InputStreamReader (java.io.InputStreamReader)143 PrintWriter (java.io.PrintWriter)137 ArrayList (java.util.ArrayList)114 Test (org.junit.Test)109 FileNotFoundException (java.io.FileNotFoundException)108 FileReader (java.io.FileReader)98 Path (org.apache.hadoop.fs.Path)90 OutputStream (java.io.OutputStream)79 InputStream (java.io.InputStream)67 Date (java.util.Date)64 Path (java.nio.file.Path)63 FileInputStream (java.io.FileInputStream)59