Example usage for javax.xml.bind.annotation.adapters HexBinaryAdapter marshal

List of usage examples for javax.xml.bind.annotation.adapters HexBinaryAdapter marshal

Introduction

In this page you can find the example usage for javax.xml.bind.annotation.adapters HexBinaryAdapter marshal.

Prototype

public String marshal(byte[] bytes) 

Source Link

Usage

From source file:edu.stanford.cfuller.colocalization3d.correction.Correction.java

protected String writeToXML() {

    StringWriter sw = new StringWriter();

    try {/* w ww . j  a  v a 2s .c  om*/

        XMLStreamWriter xsw = XMLOutputFactory.newFactory().createXMLStreamWriter(sw);

        xsw.writeStartDocument();
        xsw.writeCharacters("\n");
        xsw.writeStartElement("root");
        xsw.writeCharacters("\n");
        xsw.writeStartElement(CORRECTION_ELEMENT);
        xsw.writeAttribute(N_POINTS_ATTR, Integer.toString(this.distanceCutoffs.getDimension()));
        xsw.writeAttribute(REF_CHANNEL_ATTR, Integer.toString(this.referenceChannel));
        xsw.writeAttribute(CORR_CHANNEL_ATTR, Integer.toString(this.correctionChannel));

        xsw.writeCharacters("\n");

        for (int i = 0; i < this.distanceCutoffs.getDimension(); i++) {
            xsw.writeStartElement(CORRECTION_POINT_ELEMENT);

            xsw.writeAttribute(X_POS_ATTR, Double.toString(this.positionsForCorrection.getEntry(i, 0)));
            xsw.writeAttribute(Y_POS_ATTR, Double.toString(this.positionsForCorrection.getEntry(i, 1)));
            xsw.writeAttribute(Z_POS_ATTR, Double.toString(this.positionsForCorrection.getEntry(i, 2)));

            xsw.writeCharacters("\n");

            String x_param_string = "";
            String y_param_string = "";
            String z_param_string = "";

            for (int j = 0; j < this.correctionX.getColumnDimension(); j++) {
                String commaString = "";
                if (j != 0)
                    commaString = ", ";
                x_param_string += commaString + this.correctionX.getEntry(i, j);
                y_param_string += commaString + this.correctionY.getEntry(i, j);
                z_param_string += commaString + this.correctionZ.getEntry(i, j);
            }

            x_param_string = x_param_string.trim() + "\n";
            y_param_string = y_param_string.trim() + "\n";
            z_param_string = z_param_string.trim() + "\n";

            xsw.writeStartElement(X_PARAM_ELEMENT);

            xsw.writeCharacters(x_param_string);

            xsw.writeEndElement();

            xsw.writeCharacters("\n");

            xsw.writeStartElement(Y_PARAM_ELEMENT);

            xsw.writeCharacters(y_param_string);

            xsw.writeEndElement();

            xsw.writeCharacters("\n");

            xsw.writeStartElement(Z_PARAM_ELEMENT);

            xsw.writeCharacters(z_param_string);

            xsw.writeEndElement();

            xsw.writeCharacters("\n");

            xsw.writeEndElement();

            xsw.writeCharacters("\n");

        }

        xsw.writeStartElement(BINARY_DATA_ELEMENT);

        xsw.writeAttribute(ENCODING_ATTR, ENCODING_NAME);

        ByteArrayOutputStream bytesOutput = new ByteArrayOutputStream();

        try {

            ObjectOutputStream oos = new ObjectOutputStream(bytesOutput);

            oos.writeObject(this);

        } catch (java.io.IOException e) {
            java.util.logging.Logger.getLogger(LOG_NAME)
                    .severe("Exception encountered while serializing correction: " + e.getMessage());

        }

        HexBinaryAdapter adapter = new HexBinaryAdapter();
        xsw.writeCharacters(adapter.marshal(bytesOutput.toByteArray()));

        xsw.writeEndElement();

        xsw.writeCharacters("\n");

        xsw.writeEndElement();

        xsw.writeCharacters("\n");

        xsw.writeEndElement();

        xsw.writeCharacters("\n");

        xsw.writeEndDocument();

    } catch (XMLStreamException e) {

        java.util.logging.Logger.getLogger(LOG_NAME)
                .severe("Exception encountered while writing XML correction output: " + e.getMessage());

    }

    return sw.toString();

}

From source file:org.jts.eclipse.conversion.cjsidl.ConversionUtil.java

/**
 * Converts a byte array to a hex string 
 * @param hexArray - input byte array//from   w  ww . j a  va2 s. c o m
 * @return - resulting hex string
 */
public static String byteArrayToHexString(byte[] hexArray) {
    HexBinaryAdapter adapter = new HexBinaryAdapter();
    String result = adapter.marshal(hexArray);
    return result;
}

From source file:tech.sirwellington.alchemy.generator.StringGenerators.java

/**
 * Generates a random hexadecimal string.
 *
 * @param length The length of the String, must be at least 1.
 *
 * @return/*from w w  w  . j av a2 s  .c  o m*/
 */
public static AlchemyGenerator<String> hexadecimalString(int length) {
    checkThat(length > 0, "Length must be at least 1");
    HexBinaryAdapter hexBinaryAdapter = new HexBinaryAdapter();
    AlchemyGenerator<byte[]> binaryGenerator = binary(length);

    return () -> {
        byte[] binary = one(binaryGenerator);
        String hex = hexBinaryAdapter.marshal(binary);
        return StringUtils.left(hex, length);
    };
}