Example usage for java.io OutputStreamWriter close

List of usage examples for java.io OutputStreamWriter close

Introduction

In this page you can find the example usage for java.io OutputStreamWriter close.

Prototype

public void close() throws IOException 

Source Link

Usage

From source file:javaapplication1.Prog.java

public void transferStuff(String obj, String user) throws MalformedURLException, IOException {

    URL object = new URL("http://localhost:8080/bankserver/users/" + user);
    HttpURLConnection con = (HttpURLConnection) object.openConnection();
    con.setDoOutput(true);/*from  w w w. ja  v  a2 s  .  co  m*/
    con.setRequestProperty("Content-Type", "application/json");
    con.setRequestProperty("Accept", "application/json");
    con.setRequestMethod("PUT");
    OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
    wr.write(obj);
    wr.flush();
    wr.close();
    con.getInputStream();
}

From source file:hudson.plugins.sonar.template.SimpleTemplate.java

public void write(FilePath path, String pomName) throws IOException, InterruptedException {
    FilePath pom = path.child(pomName);/*from  w w  w . j  a  va 2 s. c o  m*/
    OutputStreamWriter outputStream = new OutputStreamWriter(pom.write());
    try {
        outputStream.write(template);
    } finally {
        outputStream.close();
    }
}

From source file:com.example.appengine.appidentity.UrlShortener.java

/**
 * Returns a shortened URL by calling the Google URL Shortener API.
 *
 * <p>Note: Error handling elided for simplicity.
 *///from w w  w .j av a  2  s . c o  m
public String createShortUrl(String longUrl) throws Exception {
    ArrayList<String> scopes = new ArrayList<String>();
    scopes.add("https://www.googleapis.com/auth/urlshortener");
    final AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService();
    final AppIdentityService.GetAccessTokenResult accessToken = appIdentity.getAccessToken(scopes);
    // The token asserts the identity reported by appIdentity.getServiceAccountName()
    JSONObject request = new JSONObject();
    request.put("longUrl", longUrl);

    URL url = new URL("https://www.googleapis.com/urlshortener/v1/url?pp=1");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoOutput(true);
    connection.setRequestMethod("POST");
    connection.addRequestProperty("Content-Type", "application/json");
    connection.addRequestProperty("Authorization", "Bearer " + accessToken.getAccessToken());

    OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
    request.write(writer);
    writer.close();

    if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
        // Note: Should check the content-encoding.
        //       Any JSON parser can be used; this one is used for illustrative purposes.
        JSONTokener responseTokens = new JSONTokener(connection.getInputStream());
        JSONObject response = new JSONObject(responseTokens);
        return (String) response.get("id");
    } else {
        try (InputStream s = connection.getErrorStream();
                InputStreamReader r = new InputStreamReader(s, StandardCharsets.UTF_8)) {
            throw new RuntimeException(String.format("got error (%d) response %s from %s",
                    connection.getResponseCode(), CharStreams.toString(r), connection.toString()));
        }
    }
}

From source file:com.writewreckedsoftware.ullr.networking.UpdateMarker.java

@Override
protected String doInBackground(String... arg0) {
    String json = arg0[0];//ww  w. j  a v  a2 s  . c  o  m
    JSONObject obj = null;
    String id = "";
    try {
        obj = new JSONObject(json);
        id = obj.getString("_id");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    URL url = null;
    String resultText = "";
    try {
        url = new URL("http://ullr.herokuapp.com/markers/" + id);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestMethod("PUT");
        connection.setRequestProperty("Content-Type", "application/json");
        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
        out.write(json);
        out.close();
        InputStream response = connection.getInputStream();
        resultText = NetworkHelpers.getInstance(null).convertStreamToString(response);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    JSONObject result = null;
    try {
        result = new JSONObject(resultText);
        result.put("_id", id);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return result.toString();
}

From source file:at.tugraz.ist.akm.webservice.requestprocessor.HttpResponseDataAppender.java

public void appendHttpResponseData(HttpResponse httpResponse, final String contentType, final String data) {

    httpResponse.setEntity(new EntityTemplate(new ContentProducer() {
        @Override//from   w w  w  .ja v a 2s.c  o  m
        public void writeTo(OutputStream outstream) throws IOException {
            OutputStreamWriter writer = new OutputStreamWriter(outstream, mDefaultEncoding);
            writer.write(data);
            writer.flush();
            writer.close();
        }
    }));
    httpResponse.setHeader(WebServerConstants.HTTP.KEY_CONTENT_TYPE, contentType);
}

From source file:com.naver.wysohn2002.mythicmobcreator.util.Utf8YamlConfiguration.java

@Override
public void save(File file) throws IOException {
    Validate.notNull(file, "File cannot be null");

    Files.createParentDirs(file);

    String data = saveToString();

    FileOutputStream stream = new FileOutputStream(file);
    OutputStreamWriter writer = new OutputStreamWriter(stream, UTF8_CHARSET);

    try {//  ww  w  .j a  v  a2 s  .c o  m
        writer.write(data);
    } finally {
        writer.close();
    }
}

From source file:magixcel.FileMan.java

static void writeToFile() {
    FileOutputStream fos = null;/*from www .  j  a  v a 2 s .  co m*/
    OutputStreamWriter osw = null;
    BufferedWriter bw = null;
    String fileNameFull = OUTPUT_FILE_DIRECTORY + "output.txt";

    System.out.println("writing to: " + fileNameFull);

    try {
        fos = new FileOutputStream(fileNameFull);
        osw = new OutputStreamWriter(fos, Charsets.ISO_8859_1);
        bw = new BufferedWriter(osw);
        for (String lineOut : OutputFormat.getOutputLines()) {
            if (Globals.DEVELOPER_MODE == 1) {
                System.out.println("Writing line: " + lineOut);
            }
            bw.write(lineOut + System.lineSeparator());

        }
        System.out.println("DONE");

    } catch (FileNotFoundException ex) {
        Logger.getLogger(FileMan.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(FileMan.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            bw.close();
            osw.close();
            fos.close();
        } catch (IOException ex) {
            Logger.getLogger(FileMan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:org.sansdemeure.zenindex.service.TemplateService.java

private void generate(Map<String, Object> model, File htmlDocument, String template) {
    try {//from   w w  w. j a v  a 2s  .c o m
        Template temp = freeMarkerConfiguration.getTemplate(template);
        FileOutputStream out = new FileOutputStream(htmlDocument);
        OutputStreamWriter writer = new OutputStreamWriter(out, Charset.forName("UTF-8").newEncoder());
        temp.process(model, writer);
        out.close();
        writer.close();
    } catch (IOException | TemplateException e) {
        throw new RuntimeException(e);
    }
}

From source file:de.undercouch.gradle.tasks.download.CompressionTest.java

@Override
protected Handler[] makeHandlers() throws IOException {
    ContextHandler compressionHandler = new ContextHandler("/" + COMPRESSED) {
        @Override//w ww  . j a v a  2s. c  om
        public void handle(String target, HttpServletRequest request, HttpServletResponse response,
                int dispatch) throws IOException, ServletException {
            String acceptEncoding = request.getHeader("Accept-Encoding");
            boolean acceptGzip = "gzip".equals(acceptEncoding);

            response.setStatus(200);
            OutputStream os = response.getOutputStream();
            if (acceptGzip) {
                response.setHeader("Content-Encoding", "gzip");
                GZIPOutputStream gos = new GZIPOutputStream(os);
                OutputStreamWriter osw = new OutputStreamWriter(gos);
                osw.write("Compressed");
                osw.close();
                gos.flush();
                gos.close();
            } else {
                OutputStreamWriter osw = new OutputStreamWriter(os);
                osw.write("Uncompressed");
                osw.close();
            }
            os.close();
        }
    };
    return new Handler[] { compressionHandler };
}

From source file:edu.isi.karma.webserver.ExtractSpatialInformationFromWikimapiaServiceHandler.java

protected void outputToOSM(String url) throws SQLException, ClientProtocolException, IOException {
    //String url = "http://api.wikimapia.org/?function=box&count=100&lon_min=-118.29244&lat_min=34.01794&lon_max=-118.28&lat_max=34.02587&key=156E90A4-57B03618-05BDBEA0-ED9C6E97-DD2116A6-A5229FEC-091E312A-2856C8BA";      
    this.url = "http://api.wikimapia.org/?function=box&count=100" + url
            + "&key=156E90A4-57B03618-05BDBEA0-ED9C6E97-DD2116A6-A5229FEC-091E312A-2856C8BA";
    DefaultHttpClient client = new DefaultHttpClient();
    HttpGet get = new HttpGet(this.url);
    HttpResponse response = client.execute(get);
    HttpEntity entity = response.getEntity();
    String result = EntityUtils.toString(entity);
    FileOutputStream fout = new FileOutputStream(osmFile_path);
    OutputStream bout = new BufferedOutputStream(fout);
    OutputStreamWriter out = new OutputStreamWriter(bout, "UTF8");
    out.write(result);//  www  . j  a v  a  2s .c  o  m
    out.close();
}