use of java.io.InputStreamReader in project platform_frameworks_base by android.
the class DropBoxTest method testDropBoxEntrySerialization.
public void testDropBoxEntrySerialization() throws Exception {
// Make sure DropBoxManager.Entry can be serialized to a Parcel and back
// under a variety of conditions.
Parcel parcel = Parcel.obtain();
File dir = getEmptyDir("testDropBoxEntrySerialization");
new DropBoxManager.Entry("empty", 1000000).writeToParcel(parcel, 0);
new DropBoxManager.Entry("string", 2000000, "String Value").writeToParcel(parcel, 0);
new DropBoxManager.Entry("bytes", 3000000, "Bytes Value".getBytes(), DropBoxManager.IS_TEXT).writeToParcel(parcel, 0);
new DropBoxManager.Entry("zerobytes", 4000000, new byte[0], 0).writeToParcel(parcel, 0);
new DropBoxManager.Entry("emptybytes", 5000000, (byte[]) null, DropBoxManager.IS_EMPTY).writeToParcel(parcel, 0);
try {
new DropBoxManager.Entry("badbytes", 99999, "Bad Bytes Value".getBytes(), DropBoxManager.IS_EMPTY).writeToParcel(parcel, 0);
fail("IllegalArgumentException expected for non-null byte[] and IS_EMPTY flags");
} catch (IllegalArgumentException e) {
// expected
}
try {
new DropBoxManager.Entry("badbytes", 99999, (byte[]) null, 0).writeToParcel(parcel, 0);
fail("IllegalArgumentException expected for null byte[] and non-IS_EMPTY flags");
} catch (IllegalArgumentException e) {
// expected
}
File f = new File(dir, "file.dat");
FileOutputStream os = new FileOutputStream(f);
os.write("File Value".getBytes());
os.close();
new DropBoxManager.Entry("file", 6000000, f, DropBoxManager.IS_TEXT).writeToParcel(parcel, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
new DropBoxManager.Entry("binfile", 7000000, f, 0).writeToParcel(parcel, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
new DropBoxManager.Entry("emptyfile", 8000000, (ParcelFileDescriptor) null, DropBoxManager.IS_EMPTY).writeToParcel(parcel, 0);
try {
new DropBoxManager.Entry("badfile", 99999, new File(dir, "nonexist.dat"), 0);
fail("IOException expected for nonexistent file");
} catch (IOException e) {
// expected
}
try {
new DropBoxManager.Entry("badfile", 99999, f, DropBoxManager.IS_EMPTY).writeToParcel(parcel, 0);
fail("IllegalArgumentException expected for non-null file and IS_EMPTY flags");
} catch (IllegalArgumentException e) {
// expected
}
try {
new DropBoxManager.Entry("badfile", 99999, (ParcelFileDescriptor) null, 0);
fail("IllegalArgumentException expected for null PFD and non-IS_EMPTY flags");
} catch (IllegalArgumentException e) {
// expected
}
File gz = new File(dir, "file.gz");
GZIPOutputStream gzout = new GZIPOutputStream(new FileOutputStream(gz));
gzout.write("Gzip File Value".getBytes());
gzout.close();
new DropBoxManager.Entry("gzipfile", 9000000, gz, DropBoxManager.IS_TEXT | DropBoxManager.IS_GZIPPED).writeToParcel(parcel, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
new DropBoxManager.Entry("gzipbinfile", 10000000, gz, DropBoxManager.IS_GZIPPED).writeToParcel(parcel, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
//
// Switch from writing to reading
//
parcel.setDataPosition(0);
DropBoxManager.Entry e;
e = DropBoxManager.Entry.CREATOR.createFromParcel(parcel);
assertEquals("empty", e.getTag());
assertEquals(1000000, e.getTimeMillis());
assertEquals(DropBoxManager.IS_EMPTY, e.getFlags());
assertEquals(null, e.getText(100));
assertEquals(null, e.getInputStream());
e.close();
e = DropBoxManager.Entry.CREATOR.createFromParcel(parcel);
assertEquals("string", e.getTag());
assertEquals(2000000, e.getTimeMillis());
assertEquals(DropBoxManager.IS_TEXT, e.getFlags());
assertEquals("String Value", e.getText(100));
assertEquals("String Value", new BufferedReader(new InputStreamReader(e.getInputStream())).readLine());
e.close();
e = DropBoxManager.Entry.CREATOR.createFromParcel(parcel);
assertEquals("bytes", e.getTag());
assertEquals(3000000, e.getTimeMillis());
assertEquals(DropBoxManager.IS_TEXT, e.getFlags());
assertEquals("Bytes Value", e.getText(100));
e.close();
e = DropBoxManager.Entry.CREATOR.createFromParcel(parcel);
assertEquals("zerobytes", e.getTag());
assertEquals(4000000, e.getTimeMillis());
assertEquals(0, e.getFlags());
assertEquals(null, e.getText(100));
assertEquals(null, new BufferedReader(new InputStreamReader(e.getInputStream())).readLine());
e.close();
e = DropBoxManager.Entry.CREATOR.createFromParcel(parcel);
assertEquals("emptybytes", e.getTag());
assertEquals(5000000, e.getTimeMillis());
assertEquals(DropBoxManager.IS_EMPTY, e.getFlags());
assertEquals(null, e.getText(100));
assertEquals(null, e.getInputStream());
e.close();
e = DropBoxManager.Entry.CREATOR.createFromParcel(parcel);
assertEquals("file", e.getTag());
assertEquals(6000000, e.getTimeMillis());
assertEquals(DropBoxManager.IS_TEXT, e.getFlags());
assertEquals("File Value", e.getText(100));
e.close();
e = DropBoxManager.Entry.CREATOR.createFromParcel(parcel);
assertEquals("binfile", e.getTag());
assertEquals(7000000, e.getTimeMillis());
assertEquals(0, e.getFlags());
assertEquals(null, e.getText(100));
assertEquals("File Value", new BufferedReader(new InputStreamReader(e.getInputStream())).readLine());
e.close();
e = DropBoxManager.Entry.CREATOR.createFromParcel(parcel);
assertEquals("emptyfile", e.getTag());
assertEquals(8000000, e.getTimeMillis());
assertEquals(DropBoxManager.IS_EMPTY, e.getFlags());
assertEquals(null, e.getText(100));
assertEquals(null, e.getInputStream());
e.close();
e = DropBoxManager.Entry.CREATOR.createFromParcel(parcel);
assertEquals("gzipfile", e.getTag());
assertEquals(9000000, e.getTimeMillis());
assertEquals(DropBoxManager.IS_TEXT, e.getFlags());
assertEquals("Gzip File Value", e.getText(100));
e.close();
e = DropBoxManager.Entry.CREATOR.createFromParcel(parcel);
assertEquals("gzipbinfile", e.getTag());
assertEquals(10000000, e.getTimeMillis());
assertEquals(0, e.getFlags());
assertEquals(null, e.getText(100));
assertEquals("Gzip File Value", new BufferedReader(new InputStreamReader(e.getInputStream())).readLine());
e.close();
assertEquals(0, parcel.dataAvail());
parcel.recycle();
}
use of java.io.InputStreamReader in project antlr4 by antlr.
the class TestUnicodeGrammar method binaryGrammar.
@Test
public void binaryGrammar() throws Exception {
String grammarText = "grammar Binary;\n" + "r : HEADER PACKET+ FOOTER;\n" + "HEADER : '\\u0002\\u0000\\u0001\\u0007';\n" + "PACKET : '\\u00D0' ('\\u00D1' | '\\u00D2' | '\\u00D3') +;\n" + "FOOTER : '\\u00FF';\n";
byte[] toParse = new byte[] { (byte) 0x02, (byte) 0x00, (byte) 0x01, (byte) 0x07, (byte) 0xD0, (byte) 0xD2, (byte) 0xD2, (byte) 0xD3, (byte) 0xD3, (byte) 0xD3, (byte) 0xD0, (byte) 0xD3, (byte) 0xD3, (byte) 0xD1, (byte) 0xFF };
CharStream charStream;
try (ByteArrayInputStream is = new ByteArrayInputStream(toParse);
// U+0000 to U+00FF.
InputStreamReader isr = new InputStreamReader(is, StandardCharsets.ISO_8859_1)) {
charStream = new ANTLRInputStream(isr);
}
Grammar grammar = new Grammar(grammarText);
LexerInterpreter lexEngine = grammar.createLexerInterpreter(charStream);
CommonTokenStream tokens = new CommonTokenStream(lexEngine);
GrammarParserInterpreter parser = grammar.createGrammarParserInterpreter(tokens);
ParseTree parseTree = parser.parse(grammar.rules.get("r").index);
InterpreterTreeTextProvider nodeTextProvider = new InterpreterTreeTextProvider(grammar.getRuleNames());
String result = Trees.toStringTree(parseTree, nodeTextProvider);
assertEquals("(r:1 ÐÒÒÓÓÓ ÐÓÓÑ ÿ)", result);
}
use of java.io.InputStreamReader in project jstorm by alibaba.
the class SandBoxMaker method generatePolicyFile.
public String generatePolicyFile(Map<String, String> replaceMap) throws IOException {
// dynamic generate policy file, no static file
String tmpPolicy = StormConfig.supervisorTmpDir(conf) + File.separator + UUID.randomUUID().toString();
InputStream inputStream = SandBoxMaker.class.getClassLoader().getResourceAsStream(SANBOX_TEMPLATE_NAME);
PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(tmpPolicy)));
BufferedReader reader = null;
InputStreamReader inputReader = null;
try {
inputReader = new InputStreamReader(inputStream);
reader = new BufferedReader(new LineNumberReader(inputReader));
String line = null;
while ((line = reader.readLine()) != null) {
String replaced = replaceLine(line, replaceMap);
writer.println(replaced);
}
return tmpPolicy;
} catch (Exception e) {
LOG.error("Failed to generate policy file\n", e);
throw new IOException(e);
} finally {
if (inputStream != null) {
inputStream.close();
}
if (writer != null) {
writer.close();
}
if (reader != null) {
reader.close();
}
if (inputReader != null) {
inputReader.close();
}
}
}
use of java.io.InputStreamReader in project graphhopper by graphhopper.
the class PrinctonReader method read.
public void read() {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, Helper.UTF_CS), 8 * (1 << 10));
int lineNo = 0;
try {
lineNo++;
int nodes = Integer.parseInt(reader.readLine());
lineNo++;
int edges = Integer.parseInt(reader.readLine());
for (int i = 0; i < edges; i++) {
lineNo++;
String line = reader.readLine();
if (line == null)
throw new IllegalStateException("Cannot read line " + lineNo);
String[] args = line.split(" ");
int from = -1;
int to = -1;
double dist = -1;
int counter = 0;
for (int j = 0; j < args.length; j++) {
if (Helper.isEmpty(args[j])) {
continue;
}
if (counter == 0) {
from = Integer.parseInt(args[j]);
} else if (counter == 1) {
to = Integer.parseInt(args[j]);
} else {
dist = Double.parseDouble(args[j]);
}
counter++;
}
if (counter != 3) {
throw new RuntimeException("incorrect read!? from:" + from + ", to:" + to + ", dist:" + dist);
}
g.edge(from, to, dist, false);
}
} catch (Exception ex) {
throw new RuntimeException("Problem in line " + lineNo, ex);
} finally {
Helper.close(reader);
}
}
use of java.io.InputStreamReader in project groovy-core by groovy.
the class SourceExtensionHandler method getRegisteredExtensions.
public static Set<String> getRegisteredExtensions(ClassLoader loader) {
Set<String> extensions = new LinkedHashSet<String>();
extensions.add("groovy");
try {
Enumeration<URL> globalServices = loader.getResources("META-INF/services/org.codehaus.groovy.source.Extensions");
while (globalServices.hasMoreElements()) {
BufferedReader svcIn = null;
URL service = globalServices.nextElement();
try {
svcIn = new BufferedReader(new InputStreamReader(service.openStream()));
String extension = svcIn.readLine();
while (extension != null) {
extension = extension.trim();
if (!extension.startsWith("#") && extension.length() > 0) {
extensions.add(extension);
}
extension = svcIn.readLine();
}
} catch (IOException ex) {
throw new GroovyRuntimeException("IO Exception attempting to load registered source extension " + service.toExternalForm() + ". Exception: " + ex.toString());
} finally {
if (svcIn != null)
svcIn.close();
}
}
} catch (IOException ex) {
throw new GroovyRuntimeException("IO Exception getting registered source extensions. Exception: " + ex.toString());
}
return extensions;
}
Aggregations