001    /**
002     * Copyright (c) 2010, SIB. All rights reserved.
003     * 
004     * SIB (Swiss Institute of Bioinformatics) - http://www.isb-sib.ch Host -
005     * https://sourceforge.net/projects/javaprotlib/
006     * 
007     * Redistribution and use in source and binary forms, with or without
008     * modification, are permitted provided that the following conditions are met:
009     * Redistributions of source code must retain the above copyright notice, this
010     * list of conditions and the following disclaimer. Redistributions in binary
011     * form must reproduce the above copyright notice, this list of conditions and
012     * the following disclaimer in the documentation and/or other materials provided
013     * with the distribution. Neither the name of the SIB/GENEBIO nor the names of
014     * its contributors may be used to endorse or promote products derived from this
015     * software without specific prior written permission.
016     * 
017     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019     * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
020     * ARE DISCLAIMED. IN NO EVENT SHALL SIB/GENEBIO BE LIABLE FOR ANY DIRECT,
021     * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022     * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023     * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
024     * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025     * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026     * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027     */
028    package org.expasy.jpl.commons.base.io;
029    
030    
031    import java.io.ByteArrayInputStream;
032    import java.io.ByteArrayOutputStream;
033    import java.io.FileInputStream;
034    import java.io.FileOutputStream;
035    import java.io.IOException;
036    import java.io.ObjectInputStream;
037    import java.io.ObjectOutputStream;
038    import java.io.Serializable;
039    import java.nio.ByteBuffer;
040    
041    
042    /**
043     * The serializer just handles (de)serialization of objects {@code T}.
044     * 
045     * @author nikitin
046     * 
047     * @param <T> the type to (de)serialize
048     * 
049     * @version 1.0
050     * 
051     */
052    public final class Serializer<T extends Serializable> {
053            
054            public static <T extends Serializable> Serializer<T> newInstance() {
055                    return new Serializer<T>();
056            }
057            
058            public void serialize(final T obj, final String filename)
059                throws IOException {
060                    
061                    final FileOutputStream fs = new FileOutputStream(filename);
062                    final ObjectOutputStream os = new ObjectOutputStream(fs);
063                    os.writeObject(obj);
064                    os.close();
065            }
066            
067            @SuppressWarnings("unchecked")
068            public T deserialize(final String filename) throws IOException {
069                    final FileInputStream fis = new FileInputStream(filename);
070                    final ObjectInputStream ois = new ObjectInputStream(fis);
071                    T obj = null;
072                    try {
073                            obj = (T) ois.readObject();
074                    } catch (ClassNotFoundException e) {
075                            throw new IllegalStateException(e.getMessage()
076                                + ": deserialize error", e);
077                    }
078                    ois.close();
079                    
080                    return obj;
081            }
082            
083            /**
084             * Serializes a T-object.
085             * 
086             * @param obj the object to serialize.
087             * @return an array of bytes.
088             */
089            public byte[] serialize(T obj) throws IOException {
090                    ByteArrayOutputStream array = new ByteArrayOutputStream();
091                    ObjectOutputStream os = new ObjectOutputStream(array);
092                    os.writeObject(obj);
093                    os.close();
094                    return array.toByteArray();
095            }
096            
097            /**
098             * Deserializes a T-object from an array of bytes.
099             * 
100             * @param bytes the array of bytes.
101             */
102            @SuppressWarnings("unchecked")
103            public T deserialize(byte[] bytes) throws IOException {
104                    ObjectInputStream in =
105                        new ObjectInputStream(new ByteArrayInputStream(bytes));
106                    T spec = null;
107                    try {
108                            spec = (T) in.readObject();
109                    } catch (ClassNotFoundException e) {
110                            throw new IllegalStateException(e.getMessage()
111                                + ": deserialize bad class object", e);
112                    }
113                    in.close();
114                    return spec;
115            }
116            
117            /**
118             * Deserializes a T-object from a ByteBuffer.
119             * 
120             * @param <T> the object type.
121             * @param buf the byte buffer.
122             * @return an instance of T-class.
123             */
124            public T deserialize(ByteBuffer buf) throws IOException {
125                    byte[] bytes = new byte[buf.remaining()];
126                    buf.get(bytes);
127                    return deserialize(bytes);
128            }
129    }