Search in sources :

Example 86 with InputStreamReader

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

the class BandwidthTestUtil method parseIntValueFromFile.

/**
     * Parses the first line in a file if exists.
     *
     * @param file {@link File} the input
     * @return the integer value of the first line of the file.
     */
public static int parseIntValueFromFile(File file) {
    int value = 0;
    if (file.exists()) {
        try {
            FileInputStream fstream = new FileInputStream(file);
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine = br.readLine();
            if (strLine != null) {
                value = Integer.parseInt(strLine);
            }
            // Close the input stream
            in.close();
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }
    }
    return value;
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException)

Example 87 with InputStreamReader

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

the class BackupDataTest method testReadMockData.

public void testReadMockData() throws IOException {
    copyAssetToFile("backup_mock.dat", "backup_read_mock_test.dat");
    openForReading();
    BackupDataInput bdi = new BackupDataInput(mDataFile.getFileDescriptor());
    BufferedReader truth = new BufferedReader(new InputStreamReader(mAssets.openFd("backup_mock.gld").createInputStream()));
    while (bdi.readNextHeader()) {
        String[] expected = truth.readLine().split(":");
        byte[] expectedBytes = null;
        if (expected.length > 1) {
            expectedBytes = Base64.decode(expected[1], Base64.DEFAULT);
        }
        String key = bdi.getKey();
        int dataSize = bdi.getDataSize();
        assertEquals("wrong key", expected[0], key);
        assertEquals("wrong length for key " + key, (expectedBytes == null ? -1 : expectedBytes.length), dataSize);
        if (dataSize != -1) {
            byte[] buffer = new byte[dataSize];
            bdi.readEntityData(buffer, 0, dataSize);
            assertEquals("wrong data for key " + key, expected[1], Base64.encodeToString(buffer, 0, dataSize, Base64.NO_WRAP));
        }
    }
    assertNull("there are unused entries in the golden file", truth.readLine());
}
Also used : BackupDataInput(android.app.backup.BackupDataInput) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader)

Example 88 with InputStreamReader

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

the class BackupDataTest method testReadRealData.

public void testReadRealData() throws IOException {
    copyAssetToFile("backup_real.dat", "backup_read_real_test.dat");
    openForReading();
    BackupDataInput bdi = new BackupDataInput(mDataFile.getFileDescriptor());
    BufferedReader truth = new BufferedReader(new InputStreamReader(mAssets.openFd("backup_real.gld").createInputStream()));
    while (bdi.readNextHeader()) {
        String[] expected = truth.readLine().split(":");
        byte[] expectedBytes = null;
        if (expected.length > 1) {
            expectedBytes = Base64.decode(expected[1], Base64.DEFAULT);
        }
        String key = bdi.getKey();
        int dataSize = bdi.getDataSize();
        assertEquals("wrong key", expected[0], key);
        assertEquals("wrong length for key " + key, (expectedBytes == null ? -1 : expectedBytes.length), dataSize);
        if (dataSize != -1) {
            byte[] buffer = new byte[dataSize];
            bdi.readEntityData(buffer, 0, dataSize);
            assertEquals("wrong data for key " + key, expected[1], Base64.encodeToString(buffer, 0, dataSize, Base64.NO_WRAP));
        }
    }
    assertNull("there are unused entries in the golden file", truth.readLine());
}
Also used : BackupDataInput(android.app.backup.BackupDataInput) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader)

Example 89 with InputStreamReader

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

the class BasePrintTest method getEnabledImes.

private String[] getEnabledImes() throws IOException {
    List<String> imeList = new ArrayList<>();
    ParcelFileDescriptor pfd = getInstrumentation().getUiAutomation().executeShellCommand(COMMAND_LIST_ENABLED_IME_COMPONENTS);
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(pfd.getFileDescriptor())))) {
        String line;
        while ((line = reader.readLine()) != null) {
            imeList.add(line);
        }
    }
    String[] imeArray = new String[imeList.size()];
    imeList.toArray(imeArray);
    return imeArray;
}
Also used : InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) ParcelFileDescriptor(android.os.ParcelFileDescriptor) BufferedReader(java.io.BufferedReader) FileInputStream(java.io.FileInputStream)

Example 90 with InputStreamReader

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

the class Utils method readFile.

public static char[] readFile(String fileName, String encoding) throws IOException {
    File f = new File(fileName);
    int size = (int) f.length();
    InputStreamReader isr;
    FileInputStream fis = new FileInputStream(fileName);
    if (encoding != null) {
        isr = new InputStreamReader(fis, encoding);
    } else {
        isr = new InputStreamReader(fis);
    }
    char[] data = null;
    try {
        data = new char[size];
        int n = isr.read(data);
        if (n < data.length) {
            data = Arrays.copyOf(data, n);
        }
    } finally {
        isr.close();
    }
    return data;
}
Also used : InputStreamReader(java.io.InputStreamReader) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

InputStreamReader (java.io.InputStreamReader)4861 BufferedReader (java.io.BufferedReader)3402 IOException (java.io.IOException)2108 InputStream (java.io.InputStream)1272 FileInputStream (java.io.FileInputStream)857 URL (java.net.URL)605 ArrayList (java.util.ArrayList)559 Reader (java.io.Reader)518 File (java.io.File)514 Test (org.junit.Test)451 HttpURLConnection (java.net.HttpURLConnection)290 ByteArrayInputStream (java.io.ByteArrayInputStream)282 OutputStreamWriter (java.io.OutputStreamWriter)241 FileNotFoundException (java.io.FileNotFoundException)240 URLConnection (java.net.URLConnection)227 HashMap (java.util.HashMap)192 Socket (java.net.Socket)178 OutputStream (java.io.OutputStream)175 StringWriter (java.io.StringWriter)148 BufferedWriter (java.io.BufferedWriter)138