Java XML Data Type Converter fromString(String s)

Here you can find the source of fromString(String s)

Description

Deserializes an object from the specified base64 encoded string.

License

Open Source License

Parameter

Parameter Description
s The base64 encoded string.

Return

The deserialized object.

Declaration

public static Object fromString(String s) 

Method Source Code


//package com.java2s;
/*//w w  w .ja va2s.c om
 * Copyright (C) 2012 Klaus Reimer <k@ailis.de>
 * See LICENSE.TXT for licensing information.
 */

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import javax.xml.bind.DatatypeConverter;

public class Main {
    /**
     * Deserializes an object from the specified base64 encoded string.
     * 
     * @param s
     *            The base64 encoded string.
     * @return The deserialized object.
     */
    public static Object fromString(String s) {
        try {

            return new ObjectInputStream(new ByteArrayInputStream(DatatypeConverter.parseBase64Binary(s)))
                    .readObject();
        } catch (Exception e) {
            // Can't happen
            throw new RuntimeException(e.toString(), e);
        }
    }

    /**
     * Converts the specified object into a base64 encoded string.
     * 
     * @param o
     *            The object to convert
     * @return The object as a base64 encoded string.
     */
    public static String toString(Serializable o) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        try {
            ObjectOutputStream objectStream = new ObjectOutputStream(stream);
            objectStream.writeObject(o);
            objectStream.close();
            return DatatypeConverter.printBase64Binary(stream.toByteArray());
        } catch (IOException e) {
            // Can't happen
            throw new RuntimeException(e.toString(), e);
        }
    }
}

Related

  1. encodeJavaOpts(String javaOpts)
  2. encrypt(String plainText, String password)
  3. encryptPassword(String unencrypted)
  4. fetchUrl(String url, String username, String password)
  5. formatFriendlyName(final byte[] addressBytes)
  6. fromStringSafe(final String s)
  7. generateSalt()
  8. generateSalt(int clientNonceByteCount)
  9. getByteList(String nomal, boolean isDecode)