Example usage for java.io Writer append

List of usage examples for java.io Writer append

Introduction

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

Prototype

public Writer append(char c) throws IOException 

Source Link

Document

Appends the specified character to this writer.

Usage

From source file:org.talend.repository.hdfs.ui.metadata.ExtractTextFileSchemaService.java

/**
 * DOC ycbai Comment method "createTmpFile".
 * //w w w .j  a v  a  2s.  co m
 * Create a temporary file which contents are readed from the inputStream.
 * 
 * @param inputStream the inputStream to read
 * @param fileName the name of temporary file
 * @param maxLineNum the max quantity lines to read. If is "-1" means not limit the quantity.
 * @return
 */
private File createTmpFile(InputStream inputStream, String fileName, int maxLineNum) {
    Project project = ProjectManager.getInstance().getCurrentProject();
    IProject fsProject = null;
    try {
        fsProject = ResourceUtils.getProject(project);
    } catch (PersistenceException e2) {
        ExceptionHandler.process(e2);
    }
    if (fsProject == null) {
        return null;
    }
    File tmpParentFile = new File(
            fsProject.getLocationURI().getPath() + File.separator + "temp" + File.separator + "hdfs"); //$NON-NLS-1$ //$NON-NLS-2$
    File tmpfile = new File(tmpParentFile, fileName);
    BufferedReader reader = null;
    Writer writer = null;
    try {
        if (!tmpParentFile.exists()) {
            tmpParentFile.mkdirs();
        }
        if (tmpfile.exists()) {
            tmpfile.delete();
        }
        reader = new BufferedReader(new InputStreamReader(inputStream));
        writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tmpfile)));
        int totalLines = 0;
        String strLine = null;
        do {
            totalLines++;
            strLine = reader.readLine();
            if (strLine != null) {
                writer.append(strLine);
                writer.append("\r\n"); //$NON-NLS-1$
            }
        } while (strLine != null && (maxLineNum == -1 || totalLines < maxLineNum));
    } catch (Exception e) {
        ExceptionHandler.process(e);
    } finally {
        try {
            inputStream.close();
            if (reader != null) {
                reader.close();
            }
            if (writer != null) {
                writer.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return tmpfile;
}

From source file:org.apache.nutch.crawl.InjectorJob.java

@Override
public Map<String, Object> run(Map<String, Object> args) throws Exception {
    getConf().setLong("injector.current.time", System.currentTimeMillis());
    Path input;//from  w w w.jav  a 2  s.co m
    Object path = args.get(Nutch.ARG_SEEDDIR);
    if (path instanceof Path) {
        input = (Path) path;
    } else {
        input = new Path(path.toString());
    }

    numJobs = 1;
    currentJobNum = 0;
    currentJob = new NutchJob(getConf(), "inject " + input);

    // TODO
    Map<String, Integer> mapUrl = new HashMap<String, Integer>();
    Map<Integer, String> invertMapUrl = new HashMap<Integer, String>();
    Map<String, String> mapStatus = new HashMap<String, String>();
    /*mapUrl.put(
    123, 
    "http://lottecinemavn.com/en-us/default.aspx"
    );
    mapUrl.put(
    456, 
    "http://lottecinemavn.com/vi-vn/default.aspx"
    );
    mapUrl.put(
    123, 
    "http://lottecinemavn.com/vi-vn/phim/biet-oi-chim-canh-cut-vung-madagascar-(penguins-i.aspx"
    );
    mapUrl.put(
    456, 
    "http://lottecinemavn.com/en-us/phim/ke-san-tin-en-(nightcrawler).aspx"
    );
    */
    /*mapUrl.put(
    456, 
    "http://www.entertainmentone.com/home"
    );
    mapUrl.put(
    456, 
    "http://www.warnerbros.com/"
    );
    */
    /*mapUrl.put(
    "https://www.galaxycine.vn/vi/thong-tin-phim/big-hero",
    456
    );
    mapUrl.put(
    "http://lottecinemavn.com/vi-vn/phim/biet-oi-big-hero-6.aspx",
    123
    );*/
    /*mapUrl.put(
    "https://www.thegioididong.com/dtdd/asus-zenfone-4-45",
    123
    );*/

    /*mapUrl.put(
    "http://disney.com",
    123
    );*/
    /*invertMapUrl.put(
    123,
    "http://www.entertainmentone.com/home"
    );
            
    invertMapUrl.put(
    456,
    "http://www.entertainmentone.com/home"
    );
            
    invertMapUrl.put(
    789,
    "http://www.entertainmentone.com/home"
    );*/

    /*invertMapUrl.put(
    789,
    "http://korean.alibaba.com"
    );*/

    /*      invertMapUrl.put(
    123,
    //"NE;https://www.galaxycine.vn/vi/thong-tin-phim/DORAEMON-CG"
    //"NE;http://www.funnyland.vn/admin"
    "www.ebay.com.tw"
    );
          */
    /*mapUrl.put(
    "http://ch-hotelfurniture.en.made-in-china.com/product/KXJEDaVcHtkw/China-Hotel-Bedroom-Furniture-Luxury-Double-Bedroom-Furniture-Standard-Hotel-Double-Bedroom-Suite-Double-Hospitality-Guest-Room-Furniture-CHN-011-.html",
    123
    );*/

    /*HibernateUtils hibernateUtils = HibernateUtils.getInstance();
    Session session = hibernateUtils.openSession();
    Transaction transaction = session.beginTransaction();
    try{
       // web search
       //mapUrl = hibernateUtils.getListingWebsite();
       //invertMapUrl = hibernateUtils.getListingWebsiteV2();
               
       // movies
       //mapUrl = hibernateUtils.getListingCinema();
       //invertMapUrl = hibernateUtils.getListingCinemaV2();
               
       transaction.commit();
    } catch (Exception e){
       session.close();
    }*/

    // web search
    URL url;
    String prefix, value, status = "NE";
    multiMap = new HashMap<Integer, List<Integer>>();
    Map<String, Integer> prefixMap = new HashMap<String, Integer>();
    for (Map.Entry<Integer, String> entry : invertMapUrl.entrySet()) {
        value = entry.getValue();
        if (value.indexOf("NE;") == 0 || value.indexOf("UP;") == 0 || value.indexOf("DE;") == 0) {
            status = value.substring(0, 2);
            value = value.substring(3);
        }

        if (org.apache.commons.lang.StringUtils.isBlank(value)) {
            continue;
        }
        if (value != null && value.indexOf("http") != 0) {
            value = "http://" + value;
        }
        try {
            url = new URL(value);
        } catch (MalformedURLException e) {
            LOG.error("", e);
            continue;
        }

        prefix = url.getHost() + url.getPath();
        if (!prefixMap.containsKey(prefix)) {
            prefixMap.put(prefix, entry.getKey());
        } else {
            if (!multiMap.containsKey(prefixMap.get(prefix))) {
                multiMap.put(prefixMap.get(prefix), new ArrayList<Integer>());
            }
            multiMap.get(prefixMap.get(prefix)).add(entry.getKey());
        }
        if (!mapUrl.containsKey(value)) {
            mapUrl.put(value, entry.getKey());
        }

        if (!mapStatus.containsKey(value)) {
            mapStatus.put(value, status);
        }

    }

    String dirStr = currentJob.getWorkingDirectory().toUri().getPath();
    LOG.info("Working directory: " + dirStr);

    dirStr = dirStr + "/" + input;
    //dirStr = dirStr.substring(1) + "/" + input;

    File dirFile = new File(dirStr);
    if (dirFile.exists()) {
        //FileUtils.cleanDirectory(dirFile);
    } else {
        dirFile.mkdir();
    }
    LOG.info("Current directory: " + dirStr);
    String fileStr = dirStr + "/" + "url.txt";
    LOG.info("File name: " + fileStr);
    FileOutputStream file = new FileOutputStream(fileStr);
    Writer bw = new BufferedWriter(new OutputStreamWriter(file, "UTF8"));
    String newLine = System.getProperty("line.separator");

    String key;
    for (Map.Entry<String, Integer> entry : mapUrl.entrySet()) {
        key = entry.getKey();
        if (key != null && key.indexOf("http") != 0) {
            key = "http://" + key;
        }
        bw.append(key + " ## " + entry.getValue() + " ## " + mapStatus.get(key) + newLine);
        LOG.info("Append -- Key : " + key + " Value : " + entry.getValue());
    }

    bw.flush();
    bw.close();

    FileInputFormat.addInputPath(currentJob, input);
    currentJob.setMapperClass(UrlMapper.class);
    currentJob.setMapOutputKeyClass(String.class);
    currentJob.setMapOutputValueClass(WebPage.class);
    currentJob.setOutputFormatClass(GoraOutputFormat.class);

    DataStore<String, WebPage> store = StorageUtils.createWebStore(currentJob.getConfiguration(), String.class,
            WebPage.class);
    GoraOutputFormat.setOutput(currentJob, store, true);

    //TODO LOG
    LOG.info("CONFIG:  " + currentJob.getConfiguration().toString());

    // NUTCH-1471 Make explicit which datastore class we use
    Class<? extends DataStore<Object, Persistent>> dataStoreClass = StorageUtils
            .getDataStoreClass(currentJob.getConfiguration());
    LOG.info("InjectorJob: Using " + dataStoreClass + " as the Gora storage class.");

    currentJob.setReducerClass(Reducer.class);
    currentJob.setNumReduceTasks(0);

    currentJob.waitForCompletion(true);
    ToolUtil.recordJobStatus(null, currentJob, results);

    // NUTCH-1370 Make explicit #URLs injected @runtime
    long urlsInjected = currentJob.getCounters().findCounter("injector", "urls_injected").getValue();
    long urlsFiltered = currentJob.getCounters().findCounter("injector", "urls_filtered").getValue();
    LOG.info("InjectorJob: total number of urls rejected by filters: " + urlsFiltered);
    LOG.info("InjectorJob: total number of urls injected after normalization and filtering: " + urlsInjected);

    return results;
}

From source file:com.day.cq.wcm.foundation.forms.FormsHelper.java

/**
 * Writes the given form load resource as JSON into the given writer. Can be
 * used in JSPs to inline JSON for javascript code, for example:
 *
 * <pre>// w ww.  j  ava  2 s  .co  m
 * var data = &lt;% FormsHelper.inlineValuesAsJson(slingRequest, out, &quot;.&quot;); %&gt;;
 * </pre>
 *
 * which might result in:
 *
 * <pre>
 * var data = { &quot;jcr:primaryType&quot;: &quot;nt:unstructured&quot;, &quot;property&quot; : &quot;value&quot; };
 * </pre>
 *
 * <p>
 * If the path cannot be found, an empty object "<code>{}</code>" will be
 * written. Any exception will be passed to the caller.
 *
 * <p>
 * The underlying form load resource must be based on a JCR Node. The path
 * is relative and allows to specify subnodes. It cannot point to JCR
 * properties, please use
 * {@link #getValue(SlingHttpServletRequest, String, String)} or
 * {@link #getValues(SlingHttpServletRequest, String, String[])} for them.
 *
 * @param request
 *            the current request
 * @param out
 *            a writer, such as a {@link JspWriter}, to write the JSON into.
 *            Will automatically be flushed before and after.
 * @param path
 *            an absolute node path or a node path relativ to the current
 *            form load resource. Use "." for the resource node itself.
 * @param nodeDepth
 *            until which depth the tree should be written; 0 means the
 *            current node and its properties only; -1 means the whole tree.
 * @throws RepositoryException
 *             if some jcr error happened
 * @throws JSONException
 *             if writing the json failed
 * @throws IOException
 *             if there was a problem with the writer
 */
public static void inlineValuesAsJson(final SlingHttpServletRequest request, Writer out, String path,
        int nodeDepth) throws IOException, RepositoryException, JSONException {
    out.flush();

    Node node;
    Session session = request.getResourceResolver().adaptTo(Session.class);
    if (path.startsWith("/")) {
        if (session.nodeExists(path)) {
            node = session.getNode(path);
        } else {
            // write empty object to not break javascript syntax
            out.append("{}");
            return;
        }
    } else {
        Resource loadResource = FormsHelper.getFormLoadResource(request);
        if (loadResource == null) {
            out.append("{}");
            return;
        }
        node = loadResource.adaptTo(Node.class);
        if (node == null) {
            out.append("{}");
            return;
        }
        if (!node.hasNode(path)) {
            out.append("{}");
            return;
        }
        node = node.getNode(path);
    }

    JsonItemWriter jsonWriter = new JsonItemWriter(null);
    jsonWriter.dump(node, new PrintWriter(out), nodeDepth);

    out.flush();
}

From source file:com.mirth.connect.donkey.test.util.TestUtils.java

public static String getPerformanceText(int numDestinations, long milliseconds, List<Long> times)
        throws IOException {
    double seconds = ((double) milliseconds) / 1000d;
    double speed = ((double) times.size()) / seconds;

    long sum = 0;
    Writer writer = null;

    if (PERFORMANCE_LOG_FILE != null) {
        writer = new BufferedWriter(new FileWriter(new File(PERFORMANCE_LOG_FILE)));
    }//from  w w  w .  ja v a2s  . co m

    for (Long time : times) {
        sum += time;

        if (writer != null) {
            writer.append(time + "\n");
        }
    }

    if (writer != null) {
        writer.flush();
    }

    Collections.sort(times);

    StringBuilder stringBuilder = new StringBuilder();
    final int padding = 17;

    stringBuilder.append(StringUtils.rightPad("Messages Sent:", padding) + times.size() + "\n");
    stringBuilder.append(
            StringUtils.rightPad("Inbound:", padding) + Precision.round(speed, 2) + " messages/second\n");
    stringBuilder.append(StringUtils.rightPad("Outbound:", padding)
            + Precision.round(speed * numDestinations, 2) + " messages/second\n");

    if (times.size() > 0) {
        stringBuilder.append(StringUtils.rightPad("Lowest Time:", padding) + times.get(0) + "ms\n");
        stringBuilder
                .append(StringUtils.rightPad("Highest Time:", padding) + times.get(times.size() - 1) + "ms\n");
        stringBuilder
                .append(StringUtils.rightPad("Median Time:", padding) + times.get(times.size() / 2) + "ms\n");
        stringBuilder.append(StringUtils.rightPad("Average Time:", padding)
                + Precision.round((double) sum / (double) times.size(), 2) + "ms\n");
        stringBuilder.append(StringUtils.rightPad("Total Send Time:", padding) + sum + "ms\n");
        stringBuilder.append(StringUtils.rightPad("Total Test Time:", padding) + milliseconds + "ms\n");
    }

    return stringBuilder.toString();
}

From source file:com.redhat.rhn.frontend.taglibs.ListDisplayTag.java

private void appendButtons(Writer out) throws IOException {
    out.append("<div class=\"spacewalk-list-pagination-btns btn-group\">\n");

    boolean canGoForward = getPageList().getEnd() < getPageList().getTotalSize();
    boolean canGoBack = getPageList().getStart() > 1;

    if (canGoForward || canGoBack) {
        out.append(renderPaginationButton(FIRST, "nav-page-first", " |&lt; ", canGoBack));
        out.append(renderPaginationButton(PREV, "nav-page-prev", " &lt; ", canGoBack));
        out.append(renderPaginationButton(NEXT, "nav-page-next", " &gt; ", canGoForward));
        out.append(renderPaginationButton(LAST, "nav-page-last", " &gt;| ", canGoForward));
    }/*from   ww w  .j  a  v a  2s  .  c  om*/
    out.append("</div>\n");
}

From source file:ee.ria.xroad.common.message.SaxSoapParserImpl.java

@SneakyThrows
protected void writeEndElementXml(String prefix, QName element, Attributes attributes, Writer writer) {
    writer.append("</");
    String localName = element.getLocalPart();
    String tag = StringUtils.isEmpty(prefix) ? localName : prefix + ":" + localName;
    writer.append(tag);//from w w  w. ja v a 2 s  .co m
    writer.append('>');
}

From source file:ee.ria.xroad.common.message.SaxSoapParserImpl.java

@SneakyThrows
protected void writeStartElementXml(String prefix, QName element, Attributes attributes, Writer writer) {
    writer.append('<');
    String localName = element.getLocalPart();
    String tag = StringUtils.isEmpty(prefix) ? localName : prefix + ":" + localName;
    writer.append(tag);/*from w w  w. j a  v a  2  s. co m*/
    for (int i = 0; i < attributes.getLength(); i++) {
        String escapedAttrValue = StringEscapeUtils.escapeXml11(attributes.getValue(i));
        writer.append(String.format(" %s=\"%s\"", attributes.getQName(i), escapedAttrValue));
    }
    writer.append('>');
}

From source file:com.fizzed.rocker.compiler.JavaGenerator.java

public void appendArgumentMembers(TemplateModel model, Writer w, String access, boolean finalModifier,
        int indent) throws IOException {
    // arguments (including possible RockerBody)
    if (model.getArguments().size() > 0) {
        w.append(CRLF);
        for (Argument arg : model.getArguments()) {
            tab(w, indent).append("// argument ").append(sourceRef(arg)).append(CRLF);
            tab(w, indent).append(access).append(" ").append((finalModifier ? "final " : ""))
                    .append(arg.getExternalType()).append(" " + arg.getName()).append(";").append(CRLF);
        }/*from   w ww .j  av  a2s . c  om*/
    }
}

From source file:com.redhat.rhn.frontend.taglibs.ListDisplayTag.java

private void renderPagination(Writer out, boolean top) throws IOException {

    out.append("<div class=\"spacewalk-list-pagination\">\n");

    out.append("<div class=\"list-infotext\">");
    int finalResult = getPageList().getEnd();
    if (finalResult > getPageList().getTotalSize()) {
        finalResult = getPageList().getTotalSize();
    }/*from w  ww. ja  v  a2 s. c  o m*/

    Object[] args = new Object[4];
    if (getPageList().size() == 0) {
        args[0] = new Integer(0);
        args[1] = args[0];
        args[2] = args[0];
    } else {
        args[0] = new Integer(getPageList().getStart());
        args[1] = new Integer(finalResult);
        args[2] = new Integer(getPageList().getTotalSize());
    }
    out.append(LocalizationService.getInstance().getMessage("message.range", args));

    if ((set != null) && (!RhnSetDecl.SYSTEMS.getLabel().equals(set.getLabel()))) {
        if (top) {
            out.append(" <strong><span id=\"pagination_selcount_top\">");
        } else {
            out.append(" <strong><span id=\"pagination_selcount_bottom\">");
        }
        out.append(LocalizationService.getInstance().getMessage("message.numselected",
                Integer.toString(set.size())));
        out.append("</span></strong>\n");
    }

    out.append("</div>");
    appendButtons(out);
    out.append("  </div>");
}