All Downloads are FREE. Search and download functionalities are using the official Maven repository.

at.spardat.xma.serializer.Util Maven / Gradle / Ivy

/*******************************************************************************
 * Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     s IT Solutions AT Spardat GmbH - initial API and implementation
 *******************************************************************************/

//@(#) $Id: Util.java 2089 2007-11-28 13:56:13Z s3460 $
package at.spardat.xma.serializer;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import at.spardat.enterprise.exc.SysException;

/**
 * Some serialization util methods
 * 
 * @author YSD, 12.02.2005  
 */
public class Util {

    /**
     * Uses standard java serialization to map obj to a byte[]
     * 
     * @param obj may be null
     */
    public static byte[] serialize (Object obj) {
        ObjectOutputStream      oos = null;
        ByteArrayOutputStream   bos = null;
        try {
            bos = new ByteArrayOutputStream ();
            oos = new ObjectOutputStream (bos);
            oos.writeObject(obj);
            oos.close();
            return bos.toByteArray();
        } catch (Exception x) {
            throw new SysException (x);
        }
    }
    
    /**
     * inverse operation to serialize
     */
    public static Object deserialize (byte[] buf) {
        ObjectInputStream      ois = null;
        ByteArrayInputStream   bis = null;
        try {
            bis = new ByteArrayInputStream (buf);
            ois = new ObjectInputStream (bis);
            return ois.readObject();
        } catch (Exception x) {
            throw new SysException (x);
        }
    }
    
    private static final byte[]     hexDigits = { 
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' 
    };
    
    /**
     * Encodes every input byte as two hex characters
     */
    public static byte[] hexEncode (byte[] bytes) {
        byte[] result = new byte[bytes.length*2];
        for (int i=0; ihexEncode.
     */
    public static byte[] hexDecode (byte[] bytes) {
        byte[] result = new byte[bytes.length/2];
        for (int i=0; i= '0' && b1 <= '9') val1 = b1-'0';
            else val1 = b1-'a'+10;
            if (b2 >= '0' && b2 <= '9') val2 = b2-'0';
            else val2 = b2-'a'+10;
            int         val = 16*val1 + val2;
            if (val > 127) val -= 256;
            result[i] = (byte)val;
        }
        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy