Example usage for java.io Writer close

List of usage examples for java.io Writer close

Introduction

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

Prototype

public abstract void close() throws IOException;

Source Link

Document

Closes the stream, flushing it first.

Usage

From source file:com.comcast.drivethru.spring.AbstractCerealHttpMessageConverter.java

@Override
public void writeInternal(Object t, HttpOutputMessage outputMessage) throws IOException {
    try {/*  www  . ja v a 2s. c  om*/
        Writer writer = new OutputStreamWriter(outputMessage.getBody());
        engine.write(t, writer);
        writer.close();
    } catch (CerealException cex) {
        throw new IOException("Failed to cerealize the content", cex);
    } finally {
        IOUtils.closeQuietly(outputMessage.getBody());
    }
}

From source file:jease.cmf.service.Backup.java

/**
 * Dump contents of node (and all children) into a file.
 *//*  ww  w.j  a  va2s.co m*/
public File dump(Node node) {
    if (node == null) {
        return null;
    }
    try {
        Node nodeCopy = node.copy(true);
        nodeCopy.setParent(null);
        String filename = (StringUtils.isEmpty(nodeCopy.getId()) ? nodeCopy.getType() : nodeCopy.getId())
                + ".xml";
        File tmpDirectory = Files.createTempDirectory("jease-backup").toFile();
        tmpDirectory.deleteOnExit();
        File dumpFile = new File(tmpDirectory, filename);
        dumpFile.deleteOnExit();
        Writer writer = Files.newBufferedWriter(dumpFile.toPath());
        toXML(nodeCopy, writer);
        writer.close();
        File zipFile = Zipfiles.zip(dumpFile);
        zipFile.deleteOnExit();
        return zipFile;
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:net.mindengine.dashserver.compiler.WidgetCompiler.java

private StyleAsset compileWidgetSass(String widgetName, File widgetItem) throws Exception {
    SCSSErrorHandler errorHandler = new SCSSErrorHandler();

    ScssStylesheet e = ScssStylesheet.get(widgetItem.getAbsolutePath(), null, new SCSSDocumentHandlerImpl(),
            errorHandler);//from w w  w .  java 2 s.  c  o m
    e.compile();
    String targetFileName = widgetName + "." + widgetItem.getName() + ".style.css";
    File targetFile = new File(compiledWidgetsFolder.getAbsolutePath() + File.separator + targetFileName);
    Writer writer = createWriter(targetFile);
    e.write(writer);
    writer.close();

    return new StyleAsset(assetPrefix + targetFileName);
}

From source file:hu.petabyte.redflags.web.svc.NoticesSvc.java

private String sql(List<Filter> filters, boolean counting, boolean orderByFlags)
        throws TemplateException, IOException {
    Map<String, Object> root = new HashMap<String, Object>();
    root.put("filters", filters);
    if (counting) {
        root.put("counting", true);
    }//  w  w  w.  j  av a  2  s  .  c o m
    if (orderByFlags) {
        root.put("orderByFlags", true);
    }
    OutputStream os = new ByteArrayOutputStream();
    Writer out = new OutputStreamWriter(os);
    tpl.process(root, out);
    out.close();
    StringBuilder sb = new StringBuilder();
    for (String line : os.toString().split("\n")) {
        line = line.replaceAll("\\s+$", "");
        if (!line.isEmpty()) {
            sb.append(line);
            sb.append("\n");
        }
    }
    return sb.toString();
}

From source file:com.moss.posixfifosockets.impl.HandlerThread.java

public void run() {
    try {//  w w w . j  av  a2  s . c om
        File in = new File(socketsDirectory, id + ".fifo.in");
        File out = new File(socketsDirectory, id + ".fifo.out");
        File control = new File(socketsDirectory, id + ".fifo.control");

        PosixFifoSocket.createFifo(in);

        {// LET THE CLIENT KNOW THAT THE CONNECTION HAS BEEN ESTABLISHED
            if (!out.exists()) {
                throw new RuntimeException(
                        "ERROR: FILE SHOULD HAVE BEEEN CREATED BY SOCKET CLIENT: " + out.getAbsolutePath());
            }
            if (!control.exists()) {
                throw new RuntimeException(
                        "ERROR: FILE SHOULD HAVE BEEEN CREATED BY SOCKET CLIENT: " + out.getAbsolutePath());
            }
            Writer w = new FileWriter(control);
            w.write("OK\n");
            w.close();
        }

        PosixFifoSocket socket = new PosixFifoSocket(id, in, out);

        if (log.isDebugEnabled())
            log.debug("Opened socket " + socket);
        handler.handle(socket);
        socket.close();

    } catch (Throwable e) {
        this.e = e;
        log.error("Error creating socket: " + e.getMessage(), e);
    } finally {
        if (debug != null) {
            debug.wentInactive(this);
        }

        //            handlers.remove(this);
        //            recentHandlers.add(this);
    }
}

From source file:com.whatsthatlight.teamcity.hipchat.test.HipChatConfigurationControllerTest.java

private static String renderTemplate(Template template) throws TemplateException, IOException {
    HashMap<String, Object> templateMap = new HashMap<String, Object>();
    Writer writer = new StringWriter();
    template.process(templateMap, writer);
    writer.flush();/*from   w  w  w.  j  a  v a2s .  c  om*/
    String renderedTemplate = writer.toString();
    writer.close();
    return renderedTemplate;
}

From source file:org.esupportail.nfctagdroid.requestasync.CsnHttpRequestAsync.java

protected String doInBackground(String... params) {

    CsnMessageBean nfcMsg = new CsnMessageBean();
    nfcMsg.setNumeroId(LocalStorage.getValue("numeroId"));
    nfcMsg.setCsn(params[0]);//from   w ww. j  a  v a 2  s. c o  m
    ObjectMapper mapper = new ObjectMapper();
    String jsonInString = null;
    try {
        jsonInString = mapper.writeValueAsString(nfcMsg);
        URL url = new URL(NfcTacDroidActivity.ESUP_NFC_TAG_SERVER_URL + "/csn-ws");
        log.info("Will call csn-ws on : " + url);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
        conn.connect();
        Writer writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8"));
        writer.write(jsonInString);
        writer.close();
        InputStream inputStream = conn.getInputStream();
        String response = "";
        String line;
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
        while ((line = br.readLine()) != null) {
            response += line;
        }
        conn.disconnect();
        return response;
    } catch (Exception e) {
        throw new NfcTagDroidException(e);
    }
}

From source file:com.wavemaker.tools.project.upgrade.UpgradeTemplateFile.java

@Override
public void doUpgrade(Project project, UpgradeInfo upgradeInfo) {

    if (this.relativePath == null) {
        throw new WMRuntimeException("No file provided");
    }/*from   w w w .  jav a 2  s  .co m*/

    try {
        File localFile = project.getRootFolder().getFile(this.relativePath);

        InputStream resourceStream = this.getClass().getClassLoader()
                .getResourceAsStream(ProjectManager._TEMPLATE_APP_RESOURCE_NAME);
        ZipInputStream resourceZipStream = new ZipInputStream(resourceStream);

        ZipEntry zipEntry = null;

        while ((zipEntry = resourceZipStream.getNextEntry()) != null) {
            if (this.relativePath.equals(zipEntry.getName())) {
                Writer writer = localFile.getContent().asWriter();
                IOUtils.copy(resourceZipStream, writer);
                writer.close();
            }
        }

        resourceZipStream.close();
        resourceStream.close();
    } catch (IOException e) {
        throw new WMRuntimeException(e);
    }

    if (this.message != null) {
        upgradeInfo.addMessage(this.message);
    }
}

From source file:net.rim.ejde.internal.builders.ALXBuilder.java

/**
 * Write out the ALX file. ALX files are used by the desktop tools to load applications onto devices
 *
 * @param filename/*from  ww  w . j a  v a 2 s.com*/
 *            the name of alx file
 * @param AlxElement
 *            the object comprising the ALX file
 */
public static boolean write(String fileName, Alx alx) {
    if ((fileName == null) || (alx == null)) {
        return false;
    }
    try {
        ALXBuilder.debug("FileName: " + fileName);
        // FileWriter fw = new FileWriter(fileName + _props.getStringProperty(ALX_FILENAME_EXTENSION));
        FileOutputStream fos = new FileOutputStream(
                fileName + _props.getStringProperty(ALX_FILENAME_EXTENSION));
        Writer fw = new BufferedWriter(new OutputStreamWriter(fos, "ISO8859_1"));

        ALXBuilder.debug("adding application tag");
        Element loader = new Element(_props.getStringProperty(ALX_TAG_LOADER));
        loader.attribute(_props.getStringProperty(ALX_TAG_LOADER_ATTR_VERSION),
                _props.getStringProperty(ALX_VERSION));
        loader.add(alx.toElement(_props));
        loader.write(fw, "");
        fw.close();
    } catch (IOException ex) {
        System.err.println("AlxBuilder.write() failed:" + ex);
        return false;
    }
    return true;
}

From source file:com.wavemaker.tools.project.upgrade.five_dot_zero.WebXmlUpgradeTask.java

@Override
public void doUpgrade(Project project, UpgradeInfo upgradeInfo) {

    File webXml = project.getWebXmlFile();
    if (webXml.exists()) {
        try {/*  ww w  .j a  v a2 s.  com*/
            File webXmlBak = project.getWebInfFolder().getFile(WEB_XML_BACKUP);
            webXmlBak.getContent().write(webXml);
            webXml.delete();
            File userWebXml = project.getWebInfFolder().getFile(ProjectConstants.USER_WEB_XML);
            InputStream resourceStream = this.getClass().getClassLoader()
                    .getResourceAsStream(ProjectManager._TEMPLATE_APP_RESOURCE_NAME);
            ZipInputStream resourceZipStream = new ZipInputStream(resourceStream);

            ZipEntry zipEntry = null;

            while ((zipEntry = resourceZipStream.getNextEntry()) != null) {
                if ("webapproot/WEB-INF/user-web.xml".equals(zipEntry.getName())) {
                    Writer writer = userWebXml.getContent().asWriter();
                    IOUtils.copy(resourceZipStream, writer);
                    writer.close();
                }
            }

            resourceZipStream.close();
            resourceStream.close();
        } catch (IOException e) {
            throw new WMRuntimeException(e);
        }

        upgradeInfo.addMessage("The web.xml file has changed.  If you have custom"
                + "modifications, please copy them from " + WEB_XML_BACKUP + " to the new user-web.xml.");
    }
}