Search in sources :

Example 31 with OutputStreamWriter

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);
    }
}
Also used : TemplateException(freemarker.template.TemplateException) ContainerException(org.glassfish.jersey.server.ContainerException) Charset(java.nio.charset.Charset) OutputStreamWriter(java.io.OutputStreamWriter) HashMap(java.util.HashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Map(java.util.Map)

Example 32 with OutputStreamWriter

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);
        }
    }
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) URLConnection(java.net.URLConnection)

Example 33 with OutputStreamWriter

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;
}
Also used : InputStreamReader(java.io.InputStreamReader) Matcher(java.util.regex.Matcher) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) BufferedWriter(java.io.BufferedWriter) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File)

Example 34 with OutputStreamWriter

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");
            }
        }
    }
}
Also used : Scanner(java.util.Scanner) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) File(java.io.File) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter)

Example 35 with OutputStreamWriter

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;
}
Also used : JSONObject(org.json.JSONObject) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) URLConnection(java.net.URLConnection) URL(java.net.URL) BufferedWriter(java.io.BufferedWriter)

Aggregations

OutputStreamWriter (java.io.OutputStreamWriter)3616 IOException (java.io.IOException)1453 FileOutputStream (java.io.FileOutputStream)1408 BufferedWriter (java.io.BufferedWriter)1320 Writer (java.io.Writer)939 File (java.io.File)912 PrintWriter (java.io.PrintWriter)589 InputStreamReader (java.io.InputStreamReader)510 OutputStream (java.io.OutputStream)507 BufferedReader (java.io.BufferedReader)426 ByteArrayOutputStream (java.io.ByteArrayOutputStream)373 InputStream (java.io.InputStream)255 URL (java.net.URL)242 HttpURLConnection (java.net.HttpURLConnection)208 FileNotFoundException (java.io.FileNotFoundException)207 Test (org.junit.Test)201 ArrayList (java.util.ArrayList)198 Path (org.apache.hadoop.fs.Path)194 UnsupportedEncodingException (java.io.UnsupportedEncodingException)169 FileInputStream (java.io.FileInputStream)167