use of java.io.InputStreamReader in project CoreNLP by stanfordnlp.
the class ChineseSimWordAvgDepGrammar method getMap.
public Map<Pair<Integer, String>, List<Triple<Integer, String, Double>>> getMap(String filename) {
Map<Pair<Integer, String>, List<Triple<Integer, String, Double>>> hashMap = Generics.newHashMap();
try {
BufferedReader wordMapBReader = new BufferedReader(new InputStreamReader(new FileInputStream(filename), "UTF-8"));
String wordMapLine;
Pattern linePattern = Pattern.compile("sim\\((.+)/(.+):(.+)/(.+)\\)=(.+)");
while ((wordMapLine = wordMapBReader.readLine()) != null) {
Matcher m = linePattern.matcher(wordMapLine);
if (!m.matches()) {
log.info("Ill-formed line in similar word map file: " + wordMapLine);
continue;
}
Pair<Integer, String> iTW = new Pair<>(wordIndex.addToIndex(m.group(1)), m.group(2));
double score = Double.parseDouble(m.group(5));
List<Triple<Integer, String, Double>> tripleList = hashMap.get(iTW);
if (tripleList == null) {
tripleList = new ArrayList<>();
hashMap.put(iTW, tripleList);
}
tripleList.add(new Triple<>(wordIndex.addToIndex(m.group(3)), m.group(4), score));
}
} catch (IOException e) {
throw new RuntimeException("Problem reading similar words file!");
}
return hashMap;
}
use of java.io.InputStreamReader in project platform_frameworks_base by android.
the class ZygoteInit method preloadClasses.
/**
* Performs Zygote process initialization. Loads and initializes
* commonly used classes.
*
* Most classes only cause a few hundred bytes to be allocated, but
* a few will allocate a dozen Kbytes (in one case, 500+K).
*/
private static void preloadClasses() {
final VMRuntime runtime = VMRuntime.getRuntime();
InputStream is;
try {
is = new FileInputStream(PRELOADED_CLASSES);
} catch (FileNotFoundException e) {
Log.e(TAG, "Couldn't find " + PRELOADED_CLASSES + ".");
return;
}
Log.i(TAG, "Preloading classes...");
long startTime = SystemClock.uptimeMillis();
// Drop root perms while running static initializers.
final int reuid = Os.getuid();
final int regid = Os.getgid();
// We need to drop root perms only if we're already root. In the case of "wrapped"
// processes (see WrapperInit), this function is called from an unprivileged uid
// and gid.
boolean droppedPriviliges = false;
if (reuid == ROOT_UID && regid == ROOT_GID) {
try {
Os.setregid(ROOT_GID, UNPRIVILEGED_GID);
Os.setreuid(ROOT_UID, UNPRIVILEGED_UID);
} catch (ErrnoException ex) {
throw new RuntimeException("Failed to drop root", ex);
}
droppedPriviliges = true;
}
// Alter the target heap utilization. With explicit GCs this
// is not likely to have any effect.
float defaultUtilization = runtime.getTargetHeapUtilization();
runtime.setTargetHeapUtilization(0.8f);
try {
BufferedReader br = new BufferedReader(new InputStreamReader(is), 256);
int count = 0;
String line;
while ((line = br.readLine()) != null) {
// Skip comments and blank lines.
line = line.trim();
if (line.startsWith("#") || line.equals("")) {
continue;
}
Trace.traceBegin(Trace.TRACE_TAG_DALVIK, line);
try {
if (false) {
Log.v(TAG, "Preloading " + line + "...");
}
// Load and explicitly initialize the given class. Use
// Class.forName(String, boolean, ClassLoader) to avoid repeated stack lookups
// (to derive the caller's class-loader). Use true to force initialization, and
// null for the boot classpath class-loader (could as well cache the
// class-loader of this class in a variable).
Class.forName(line, true, null);
count++;
} catch (ClassNotFoundException e) {
Log.w(TAG, "Class not found for preloading: " + line);
} catch (UnsatisfiedLinkError e) {
Log.w(TAG, "Problem preloading " + line + ": " + e);
} catch (Throwable t) {
Log.e(TAG, "Error preloading " + line + ".", t);
if (t instanceof Error) {
throw (Error) t;
}
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
}
throw new RuntimeException(t);
}
Trace.traceEnd(Trace.TRACE_TAG_DALVIK);
}
Log.i(TAG, "...preloaded " + count + " classes in " + (SystemClock.uptimeMillis() - startTime) + "ms.");
} catch (IOException e) {
Log.e(TAG, "Error reading " + PRELOADED_CLASSES + ".", e);
} finally {
IoUtils.closeQuietly(is);
// Restore default.
runtime.setTargetHeapUtilization(defaultUtilization);
// Fill in dex caches with classes, fields, and methods brought in by preloading.
Trace.traceBegin(Trace.TRACE_TAG_DALVIK, "PreloadDexCaches");
runtime.preloadDexCaches();
Trace.traceEnd(Trace.TRACE_TAG_DALVIK);
// Bring back root. We'll need it later if we're in the zygote.
if (droppedPriviliges) {
try {
Os.setreuid(ROOT_UID, ROOT_UID);
Os.setregid(ROOT_GID, ROOT_GID);
} catch (ErrnoException ex) {
throw new RuntimeException("Failed to restore root", ex);
}
}
}
}
use of java.io.InputStreamReader in project platform_frameworks_base by android.
the class Credentials method convertFromPem.
/**
* Convert objects from PEM format, which is used for
* CA_CERTIFICATE and USER_CERTIFICATE entries.
*/
public static List<X509Certificate> convertFromPem(byte[] bytes) throws IOException, CertificateException {
ByteArrayInputStream bai = new ByteArrayInputStream(bytes);
Reader reader = new InputStreamReader(bai, StandardCharsets.US_ASCII);
PemReader pr = new PemReader(reader);
try {
CertificateFactory cf = CertificateFactory.getInstance("X509");
List<X509Certificate> result = new ArrayList<X509Certificate>();
PemObject o;
while ((o = pr.readPemObject()) != null) {
if (o.getType().equals("CERTIFICATE")) {
Certificate c = cf.generateCertificate(new ByteArrayInputStream(o.getContent()));
result.add((X509Certificate) c);
} else {
throw new IllegalArgumentException("Unknown type " + o.getType());
}
}
return result;
} finally {
pr.close();
}
}
use of java.io.InputStreamReader in project platform_frameworks_base by android.
the class GraphReader method readGraphResource.
public FilterGraph readGraphResource(Context context, int resourceId) throws GraphIOException {
InputStream inputStream = context.getResources().openRawResource(resourceId);
InputStreamReader reader = new InputStreamReader(inputStream);
StringWriter writer = new StringWriter();
char[] buffer = new char[1024];
try {
int bytesRead;
while ((bytesRead = reader.read(buffer, 0, 1024)) > 0) {
writer.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
throw new RuntimeException("Could not read specified resource file!");
}
return readGraphString(writer.toString());
}
use of java.io.InputStreamReader in project platform_frameworks_base by android.
the class RedirectListener method run.
@Override
public void run() {
int count = 0;
synchronized (mLock) {
mListening = true;
mLock.notifyAll();
}
boolean terminate = false;
for (; ; ) {
count++;
try (Socket instance = mServerSocket.accept()) {
try (BufferedReader in = new BufferedReader(new InputStreamReader(instance.getInputStream(), StandardCharsets.UTF_8))) {
boolean detected = false;
StringBuilder sb = new StringBuilder();
String s;
while ((s = in.readLine()) != null) {
sb.append(s).append('\n');
if (!detected && s.startsWith("GET")) {
String[] segments = s.split(" ");
if (segments.length == 3 && segments[2].startsWith("HTTP/") && segments[1].regionMatches(1, mPath, 0, mPath.length())) {
detected = true;
}
}
if (s.length() == 0) {
break;
}
}
Log.d(TAG, "Redirect receive: " + sb);
String response = null;
if (detected) {
response = status(OSUOperationStatus.UserInputComplete);
if (response == null) {
response = GoodBye;
terminate = true;
}
}
try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(instance.getOutputStream(), StandardCharsets.UTF_8))) {
out.write(HTTPResponseHeader);
if (response != null) {
out.write(response);
}
}
if (terminate) {
break;
} else if (count > MaxRetry) {
status(OSUOperationStatus.UserInputAborted);
break;
}
}
} catch (IOException ioe) {
if (mAborted) {
return;
} else if (count > MaxRetry) {
status(OSUOperationStatus.UserInputAborted);
break;
}
}
}
}
Aggregations