Search in sources :

Example 36 with OutputStreamWriter

use of java.io.OutputStreamWriter in project pinot by linkedin.

the class PerfBenchmarkDriver method postQuery.

public JSONObject postQuery(String query, String optimizationFlags) throws Exception {
    JSONObject requestJson = new JSONObject();
    requestJson.put("pql", query);
    if (optimizationFlags != null && !optimizationFlags.isEmpty()) {
        requestJson.put("debugOptions", "optimizationFlags=" + optimizationFlags);
    }
    long start = System.currentTimeMillis();
    URLConnection conn = new URL(_brokerBaseApiUrl + "/query").openConnection();
    conn.setDoOutput(true);
    try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8"))) {
        String requestString = requestJson.toString();
        writer.write(requestString);
        writer.flush();
        StringBuilder stringBuilder = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"))) {
            String line;
            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line);
            }
        }
        long totalTime = System.currentTimeMillis() - start;
        String responseString = stringBuilder.toString();
        JSONObject responseJson = new JSONObject(responseString);
        responseJson.put("totalTime", totalTime);
        if (_verbose && (responseJson.getLong("numDocsScanned") > 0)) {
            LOGGER.info("requestString: {}", requestString);
            LOGGER.info("responseString: {}", responseString);
        }
        return responseJson;
    }
}
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)

Example 37 with OutputStreamWriter

use of java.io.OutputStreamWriter in project pinot by linkedin.

the class PostQueryCommand method run.

public String run() throws Exception {
    if (_brokerHost == null) {
        _brokerHost = NetUtil.getHostAddress();
    }
    LOGGER.info("Executing command: " + toString());
    final JSONObject json = new JSONObject();
    json.put("pql", _query);
    String brokerUrl = "http://" + _brokerHost + ":" + _brokerPort;
    final URLConnection conn = new URL(brokerUrl + "/query").openConnection();
    conn.setDoOutput(true);
    final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8"));
    String request = json.toString();
    writer.write(request, 0, request.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);
    }
    return sb.toString();
}
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)

Example 38 with OutputStreamWriter

use of java.io.OutputStreamWriter in project jersey by jersey.

the class UnsafeCharsInUriTest method sendGetRequestOverSocket.

private String sendGetRequestOverSocket(final URI baseUri, final String requestLine) throws IOException {
    // Low level approach with sockets is used, because common Java HTTP clients are using java.net.URI,
    // which fails when unencoded curly bracket is part of the URI
    final Socket socket = new Socket(baseUri.getHost(), baseUri.getPort());
    final PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), Charset.forName("ISO-8859-1"))));
    pw.println(requestLine);
    // http request should end with a blank line
    pw.println();
    pw.flush();
    final BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), Charset.forName("UTF-8")));
    String lastLine = null;
    String line;
    while ((line = br.readLine()) != null) {
        // read the response and remember the last line
        lastLine = line;
    }
    pw.close();
    br.close();
    return lastLine;
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) Socket(java.net.Socket) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter)

Example 39 with OutputStreamWriter

use of java.io.OutputStreamWriter in project ormlite-android by j256.

the class OrmLiteConfigUtil method writeConfigFile.

/**
	 * Write a configuration file to an output stream with the configuration for classes.
	 */
public static void writeConfigFile(OutputStream outputStream, Class<?>[] classes) throws SQLException, IOException {
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream), 4096);
    try {
        writeHeader(writer);
        for (Class<?> clazz : classes) {
            writeConfigForTable(writer, clazz);
        }
        // NOTE: done is here because this is public
        System.out.println("Done.");
    } finally {
        writer.close();
    }
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter)

Example 40 with OutputStreamWriter

use of java.io.OutputStreamWriter in project eweb4j-framework by laiweiwei.

the class PetControlTest method testFreeMarker.

@Test
public void testFreeMarker() throws Exception {
    Configuration cfg = new Configuration();
    // 指定模板从何处加载的数据源,这里设置成一个文件目录。
    cfg.setDirectoryForTemplateLoading(new File("src/test/java/test/ftl"));
    // 指定模板如何检索数据模型
    cfg.setObjectWrapper(new DefaultObjectWrapper());
    Map root = new HashMap();
    root.put("user", "Big Joe");
    Map latest = new HashMap();
    root.put("latestProduct", latest);
    latest.put("url", "produces/greenmouse.html");
    latest.put("name", "green mouse");
    Template template = cfg.getTemplate("hello.html");
    Writer out = new OutputStreamWriter(System.out);
    template.process(root, out);
    out.flush();
}
Also used : Configuration(freemarker.template.Configuration) HashMap(java.util.HashMap) DefaultObjectWrapper(freemarker.template.DefaultObjectWrapper) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) Template(freemarker.template.Template) Test(org.junit.Test)

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