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;
}
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());
}
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());
}
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;
}
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;
}
Aggregations