use of java.io.OutputStreamWriter in project jersey by jersey.
the class FreemarkerViewProcessor method writeTo.
@Override
public void writeTo(final Template template, final Viewable viewable, final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders, final OutputStream out) throws IOException {
try {
Object model = viewable.getModel();
if (!(model instanceof Map)) {
model = new HashMap<String, Object>() {
{
put("model", viewable.getModel());
}
};
}
Charset encoding = setContentType(mediaType, httpHeaders);
template.process(model, new OutputStreamWriter(out, encoding));
} catch (TemplateException te) {
throw new ContainerException(te);
}
}
use of java.io.OutputStreamWriter in project languagetool by languagetool-org.
the class HTTPTools method checkAtUrlByPost.
static String checkAtUrlByPost(URL url, String postData) throws IOException {
String keepAlive = System.getProperty("http.keepAlive");
try {
// without this, there's an overhead of about 1 second - not sure why
System.setProperty("http.keepAlive", "false");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
try (OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream())) {
writer.write(postData);
writer.flush();
return StringTools.streamToString(connection.getInputStream(), "UTF-8");
}
} finally {
if (keepAlive != null) {
System.setProperty("http.keepAlive", keepAlive);
}
}
}
use of java.io.OutputStreamWriter in project languagetool by languagetool-org.
the class DictionaryBuilder method addFreqData.
protected File addFreqData(File dictFile, boolean useSeparator) throws IOException {
if (!isOptionTrue("fsa.dict.frequency-included")) {
throw new IOException("In order to use frequency data add the line 'fsa.dict.frequency-included=true' to the dictionary info file.");
}
String separator = getOption("fsa.dict.separator");
if (separator == null || separator.trim().isEmpty()) {
throw new IOException("A separator character (fsa.dict.separator) must be defined in the dictionary info file.");
}
File tempFile = File.createTempFile(DictionaryBuilder.class.getSimpleName(), "WithFrequencies.txt");
String encoding = getOption("fsa.dict.encoding");
int freqValuesApplied = 0;
try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tempFile.getAbsoluteFile()), encoding));
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(dictFile.getAbsoluteFile()), encoding))) {
String line;
int maxFreq = Collections.max(freqList.values());
double maxFreqLog = Math.log(maxFreq);
while ((line = br.readLine()) != null) {
Matcher m = pTaggerEntry.matcher(line);
if (m.matches()) {
int freq = 0;
String key = m.group(1);
if (freqList.containsKey(key)) {
freq = freqList.get(key);
freqValuesApplied++;
}
int normalizedFreq = freq;
if (freq > 0 && maxFreq > 255) {
// spread number better over the range
double freqZeroToOne = Math.log(freq) / maxFreqLog;
// 0 to 255
normalizedFreq = (int) (freqZeroToOne * (FREQ_RANGES_IN - 1));
}
if (normalizedFreq < 0 || normalizedFreq > 255) {
throw new RuntimeException("Frequency out of range (0-255): " + normalizedFreq + " in word " + key);
}
// Convert integers 0-255 to ranges A-Z, and write output
String freqChar = Character.toString((char) (FIRST_RANGE_CODE + normalizedFreq * FREQ_RANGES_OUT / FREQ_RANGES_IN));
//add separator only in speller dictionaries
if (useSeparator) {
bw.write(line + separator + freqChar + "\n");
} else {
bw.write(line + freqChar + "\n");
}
}
}
System.out.println(freqList.size() + " frequency values applied in " + freqValuesApplied + " word forms.");
} catch (IOException e) {
throw new RuntimeException("Cannot read file: " + dictFile.getAbsolutePath());
}
tempFile.deleteOnExit();
return tempFile;
}
use of java.io.OutputStreamWriter in project languagetool by languagetool-org.
the class DictionaryExporter method outputSeparatorToTab.
protected void outputSeparatorToTab(File inputFile) throws RuntimeException, IOException {
File outputFile = new File(getOutputFilename());
String separator = getOption("fsa.dict.separator");
if (separator == null || separator.trim().isEmpty()) {
throw new IOException("A separator character (fsa.dict.separator) must be defined in the dictionary info file.");
}
boolean hasFrequency = isOptionTrue("fsa.dict.frequency-included");
String encoding = getOption("fsa.dict.encoding");
try (Scanner scanner = new Scanner(inputFile, encoding);
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile), encoding))) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] parts = line.split(Pattern.quote(separator));
if (parts.length == 3) {
if (hasFrequency) {
// remove frequency data in the last byte
parts[2] = parts[2].substring(0, parts[2].length() - 1);
}
out.write(parts[1] + "\t" + parts[0] + "\t" + parts[2] + "\n");
} else if (parts.length == 2) {
if (hasFrequency) {
out.write(parts[1] + "\n");
}
out.write(parts[1] + "\t" + parts[0] + "\n");
} else if (parts.length == 1) {
out.write(parts[0]);
} else {
System.err.println("Invalid input, expected one, two or three columns separated with " + separator + " in " + inputFile + ": " + line + " => ignoring");
}
}
}
}
use of java.io.OutputStreamWriter in project pinot by linkedin.
the class AbstractBaseAdminCommand method postQuery.
public static JSONObject postQuery(String query, String brokerBaseApiUrl) throws Exception {
final JSONObject json = new JSONObject();
json.put("pql", query);
final long start = System.currentTimeMillis();
final URLConnection conn = new URL(brokerBaseApiUrl + "/query").openConnection();
conn.setDoOutput(true);
final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8"));
final String reqStr = json.toString();
System.out.println("reqStr = " + reqStr);
writer.write(reqStr, 0, reqStr.length());
writer.flush();
final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
final StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
final long stop = System.currentTimeMillis();
final String res = sb.toString();
System.out.println("res = " + res);
final JSONObject ret = new JSONObject(res);
ret.put("totalTime", (stop - start));
return ret;
}
Aggregations