Example usage for java.util Formatter Formatter

List of usage examples for java.util Formatter Formatter

Introduction

In this page you can find the example usage for java.util Formatter Formatter.

Prototype

public Formatter(OutputStream os) 

Source Link

Document

Constructs a new formatter with the specified output stream.

Usage

From source file:com.google.gwt.jolokia.server.servlet.ProxyServlet.java

/**
 * Encodes characters in the query or fragment part of the URI.
 *
 * <p>/*  w w  w.  jav a2  s  .co  m*/
 * Unfortunately, an incoming URI sometimes has characters disallowed by the
 * spec. HttpClient insists that the outgoing proxied request has a valid
 * URI because it uses Java's {@link URI}. To be more forgiving, we must
 * escape the problematic characters. See the URI class for the spec.
 *
 * @param in
 *            example: name=value&foo=bar#fragment
 */
protected static CharSequence encodeUriQuery(CharSequence in) {
    // Note that I can't simply use URI.java to encode because it will
    // escape pre-existing escaped things.
    StringBuilder outBuf = null;
    Formatter formatter = null;
    for (int i = 0; i < in.length(); i++) {
        char c = in.charAt(i);
        boolean escape = true;
        if (c < 128) {
            if (asciiQueryChars.get((int) c)) {
                escape = false;
            }
        } else if (!Character.isISOControl(c) && !Character.isSpaceChar(c)) {// not-ascii
            escape = false;
        }
        if (!escape) {
            if (outBuf != null)
                outBuf.append(c);
        } else {
            // escape
            if (outBuf == null) {
                outBuf = new StringBuilder(in.length() + 5 * 3);
                outBuf.append(in, 0, i);
                formatter = new Formatter(outBuf);
            }
            // leading %, 0 padded, width 2, capital hex
            formatter.format("%%%02X", (int) c);// TODO
        }
    }
    return outBuf != null ? outBuf : in;
}

From source file:net.sourceforge.msscodefactory.cflib.v1_11.CFLib.CFLibXmlUtil.java

public static String formatTimestamp(Calendar cal) {
    final String S_ProcName = "formatTimestamp";
    if (cal == null) {
        throw CFLib.getDefaultExceptionFactory().newNullArgumentException(CFLibXmlUtil.class, S_ProcName, 1,
                "cal");
    }//from  w w  w  . j a  va  2 s .  c  o  m
    StringBuffer buff = new StringBuffer();
    Formatter fmt = new Formatter(buff);
    fmt.format("%1$04d", cal.get(Calendar.YEAR));
    buff.append('-');
    fmt.format("%1$02d", cal.get(Calendar.MONTH) + 1);
    buff.append('-');
    fmt.format("%1$02d", cal.get(Calendar.DAY_OF_MONTH));
    buff.append('T');
    fmt.format("%1$02d", cal.get(Calendar.HOUR_OF_DAY));
    buff.append(':');
    fmt.format("%1$02d", cal.get(Calendar.MINUTE));
    buff.append(':');
    fmt.format("%1$02d", cal.get(Calendar.SECOND));
    String retval = buff.toString();
    fmt.close();
    return (retval);
}

From source file:net.sourceforge.msscodefactory.cflib.v1_11.CFLib.CFLibXmlUtil.java

public static String formatTZDate(Calendar cal) {
    final String S_ProcName = "formatTZDate";
    if (cal == null) {
        throw CFLib.getDefaultExceptionFactory().newNullArgumentException(CFLibXmlUtil.class, S_ProcName, 1,
                "cal");
    }//from w  w w .  j  av  a2 s  .  c o  m
    StringBuffer buff = new StringBuffer();
    Formatter fmt = new Formatter(buff);
    fmt.format("%1$04d", cal.get(Calendar.YEAR));
    buff.append('-');
    fmt.format("%1$02d", cal.get(Calendar.MONTH) + 1);
    buff.append('-');
    fmt.format("%1$02d", cal.get(Calendar.DAY_OF_MONTH));
    int tzoff = cal.getTimeZone().getRawOffset() / 60000;
    if (tzoff < 0) {
        tzoff = 0 - tzoff;
        buff.append('-');
    } else {
        buff.append('+');
    }
    int tzhour = tzoff / 60;
    int tzmin = tzoff % 60;
    if (tzhour > 12) {
        fmt.close();
        throw CFLib.getDefaultExceptionFactory().newInvalidArgumentException(CFLibXmlUtil.class, S_ProcName, 0,
                "tzhour", tzhour);
    }
    fmt.format("%1$02d", tzhour);
    buff.append(':');
    fmt.format("%1$02d", tzmin);
    String retval = buff.toString();
    fmt.close();
    return (retval);
}

From source file:com.fuseim.webapp.ProxyServlet.java

/**
 * Encodes characters in the query or fragment part of the URI.
 *
 * <p>Unfortunately, an incoming URI sometimes has characters disallowed by the spec. HttpClient
 * insists that the outgoing proxied request has a valid URI because it uses Java's {@link URI}.
 * To be more forgiving, we must escape the problematic characters. See the URI class for the
 * spec./*ww w .  j a  v a2 s  .  co  m*/
 *
 * @param in example: name=value&amp;foo=bar#fragment
 */
protected static CharSequence encodeUriQuery(CharSequence in) {
    //Note that I can't simply use URI.java to encode because it will escape pre-existing escaped things.
    StringBuilder outBuf = null;
    Formatter formatter = null;
    for (int i = 0; i < in.length(); i++) {
        char c = in.charAt(i);
        boolean escape = true;
        if (c < 128) {
            if (asciiQueryChars.get((int) c)) {
                escape = false;
            }
        } else if (!Character.isISOControl(c) && !Character.isSpaceChar(c)) { //not-ascii
            escape = false;
        }
        if (!escape) {
            if (outBuf != null)
                outBuf.append(c);
        } else {
            //escape
            if (outBuf == null) {
                outBuf = new StringBuilder(in.length() + 5 * 3);
                outBuf.append(in, 0, i);
                formatter = new Formatter(outBuf);
            }
            //leading %, 0 padded, width 2, capital hex
            formatter.format("%%%02X", (int) c); //TODO
        }
    }
    return outBuf != null ? outBuf : in;
}

From source file:net.sourceforge.msscodefactory.cflib.v1_11.CFLib.CFLibXmlUtil.java

public static String formatTZTime(Calendar cal) {
    final String S_ProcName = "formatTZTime";
    if (cal == null) {
        throw CFLib.getDefaultExceptionFactory().newNullArgumentException(CFLibXmlUtil.class, S_ProcName, 1,
                "cal");
    }//from   ww w  . j a  v a  2s .  c  om
    StringBuffer buff = new StringBuffer();
    Formatter fmt = new Formatter(buff);
    fmt.format("%1$02d", cal.get(Calendar.HOUR_OF_DAY));
    buff.append(':');
    fmt.format("%1$02d", cal.get(Calendar.MINUTE));
    buff.append(':');
    fmt.format("%1$02d", cal.get(Calendar.SECOND));
    int tzoff = cal.getTimeZone().getRawOffset() / 60000;
    if (tzoff < 0) {
        tzoff = 0 - tzoff;
        buff.append('-');
    } else {
        buff.append('+');
    }
    int tzhour = tzoff / 60;
    int tzmin = tzoff % 60;
    if (tzhour > 12) {
        fmt.close();
        throw CFLib.getDefaultExceptionFactory().newInvalidArgumentException(CFLibXmlUtil.class, S_ProcName, 0,
                "tzhour", tzhour);
    }
    fmt.format("%1$02d", tzhour);
    buff.append(':');
    fmt.format("%1$02d", tzmin);
    String retval = buff.toString();
    fmt.close();
    return (retval);
}

From source file:org.apache.poi.ss.format.CellNumberFormatter.java

private void writeSingleInteger(String fmt, int num, StringBuffer output, List<Special> numSpecials,
        Set<StringMod> mods) {

    StringBuffer sb = new StringBuffer();
    Formatter formatter = new Formatter(sb);
    formatter.format(LOCALE, fmt, num);//from   w  w w .j  a  v a 2 s .c o m
    writeInteger(sb, output, numSpecials, mods, false);
}

From source file:net.sourceforge.msscodefactory.cflib.v1_11.CFLib.CFLibXmlUtil.java

public static String formatTZTimestamp(Calendar cal) {
    final String S_ProcName = "formatTZTimestamp";
    if (cal == null) {
        throw CFLib.getDefaultExceptionFactory().newNullArgumentException(CFLibXmlUtil.class, S_ProcName, 1,
                "cal");
    }/*w  ww. java2s.co  m*/
    StringBuffer buff = new StringBuffer();
    Formatter fmt = new Formatter(buff);
    fmt.format("%1$04d", cal.get(Calendar.YEAR));
    buff.append('-');
    fmt.format("%1$02d", cal.get(Calendar.MONTH) + 1);
    buff.append('-');
    fmt.format("%1$02d", cal.get(Calendar.DAY_OF_MONTH));
    buff.append('T');
    fmt.format("%1$02d", cal.get(Calendar.HOUR_OF_DAY));
    buff.append(':');
    fmt.format("%1$02d", cal.get(Calendar.MINUTE));
    buff.append(':');
    fmt.format("%1$02d", cal.get(Calendar.SECOND));
    int tzoff = cal.getTimeZone().getRawOffset() / 60000;
    if (tzoff < 0) {
        tzoff = 0 - tzoff;
        buff.append('-');
    } else {
        buff.append('+');
    }
    int tzhour = tzoff / 60;
    int tzmin = tzoff % 60;
    if (tzhour > 12) {
        fmt.close();
        throw CFLib.getDefaultExceptionFactory().newInvalidArgumentException(CFLibXmlUtil.class, S_ProcName, 0,
                "tzhour", tzhour);
    }
    fmt.format("%1$02d", tzhour);
    buff.append(':');
    fmt.format("%1$02d", tzmin);
    String retval = buff.toString();
    fmt.close();
    return (retval);
}

From source file:net.java.sip.communicator.impl.certificate.CertificateServiceImpl.java

/**
 * Calculates the hash of the certificate known as the "thumbprint"
 * and returns it as a string representation.
 *
 * @param cert The certificate to hash./* ww  w  . ja v  a  2  s.co m*/
 * @param algorithm The hash algorithm to use.
 * @return The SHA-1 hash of the certificate.
 * @throws CertificateException
 */
private static String getThumbprint(Certificate cert, String algorithm) throws CertificateException {
    MessageDigest digest;
    try {
        digest = MessageDigest.getInstance(algorithm);
    } catch (NoSuchAlgorithmException e) {
        throw new CertificateException(e);
    }
    byte[] encodedCert = cert.getEncoded();
    StringBuilder sb = new StringBuilder(encodedCert.length * 2);
    Formatter f = new Formatter(sb);
    try {
        for (byte b : digest.digest(encodedCert))
            f.format("%02x", b);
    } finally {
        f.close();
    }
    return sb.toString();
}

From source file:com.itemanalysis.psychometrics.irt.model.Irm3PL.java

/**
 * A string representation of the item parameters. Mainly used for printing and debugging.
 *
 * @return a string of item parameters.//  ww  w.  jav a  2  s  . com
 */
@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    Formatter f = new Formatter(sb);

    String name = getName().toString();
    if (getName() != null) {
        name = getName().toString().substring(0, Math.min(18, name.length()));
    } else {
        name = "";
    }
    f.format("%-18s", name);
    f.format("%2s", "");

    String m = "";
    if (numberOfParameters == 3) {
        m = "L3";
    } else if (numberOfParameters == 2) {
        m = "L2";
    } else {
        m = "L1";
    }
    f.format("%-3s", m);
    f.format("%4s", "");

    f.format("% 4.2f", getDiscrimination());
    f.format("%1s", "");
    f.format("(%4.2f)", getDiscriminationStdError());
    f.format("%4s", "");

    f.format("% 4.2f", getDifficulty());
    f.format("%1s", "");
    f.format("(%4.2f)", getDifficultyStdError());
    f.format("%4s", "");

    if ((numberOfParameters < 3 && getGuessing() > 0) || numberOfParameters == 3) {
        f.format("% 4.2f", getGuessing());
        f.format("%1s", "");
        f.format("(%4.2f)", getGuessingStdError());
        f.format("%4s", "");
    } else {
        f.format("%13s", "");
    }

    if (getSlipping() < 1) {
        f.format("% 4.2f", getSlipping());
        f.format("%1s", "");
        f.format("(%4.2f)", getSlippingStdError());
        f.format("%4s", "");
    }
    return f.toString();

    //        //OLD==================================================================
    //        String name = "";
    //        if(getName()!=null){
    //            name = getName().toString();
    //        }
    //
    //        f.format("%10s", name);f.format("%2s", ": ");
    //        f.format("%1s", "[");
    //        f.format("% .6f", getDiscrimination()); f.format("%2s", ", ");
    //        f.format("% .6f", getDifficulty()); f.format("%2s", ", ");
    //        f.format("% .6f", getGuessing());
    //
    //        if(getSlipping()<1) {
    //            f.format("%2s", ", ");
    //            f.format("% .6f", getSlipping());
    //        }
    //        f.format("%1s", "]");
    //        f.format("%n");
    //        f.format("%10s", "");f.format("%2s", "");
    //        f.format("%1s", "(");
    //        f.format("% .6f", getDiscriminationStdError()); f.format("%2s", ", ");
    //        f.format("% .6f", getDifficultyStdError()); f.format("%2s", ", ");
    //        f.format("% .6f", getGuessingStdError());
    //        if(getSlipping()<1){
    //            f.format("%2s", ", ");
    //            f.format("% .6f", getSlippingStdError());
    //        }
    //        f.format("%1s", ")");
    //
    //        return f.toString();
}

From source file:edu.uga.cs.fluxbuster.db.PostgresDBInterface.java

/**
 * Store cluster similarities in the database.
 *
 * @param sims the similarities to store
 * @param type the type of similarity/*ww w .ja v  a 2  s. c  om*/
 */
private void storeClusterSimilarities(List<ClusterSimilarity> sims, ClusterSimilarityCalculator.SIM_TYPE type) {
    String format = "%d\t%d\t%f\t\'%s\'\t\'%s\'\n";
    StringBuffer databuf = new StringBuffer();
    Formatter formatter = new Formatter(databuf);
    if (sims.size() > 0) {
        String tabletype = "";

        switch (type) {
        case IP:
            tabletype = "ip";
            break;
        case DOMAINNAME:
            tabletype = "domainname";
            break;
        }

        String tabDateStr = dateFormatTable.format(sims.get(0).getADate());
        String copyQuery = "COPY cluster_" + tabletype + "_similarity_" + tabDateStr
                + " (cluster_id, candidate_cluster_id, "
                + "similarity, log_date, candidate_log_date ) FROM stdin;";

        for (ClusterSimilarity s : sims) {
            formatter.format(format, s.getAClusterId(), s.getBClusterId(), s.getSim(),
                    dateFormatStr.format(s.getADate()), dateFormatStr.format(s.getBDate()));
        }
        this.executeCopyIn(copyQuery, new StringReader(databuf.toString()));
    }
    formatter.close();
}