001/*
002 * JPPF.
003 * Copyright (C) 2005-2018 JPPF Team.
004 * http://www.jppf.org
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.jppf.serialization;
020
021import java.io.*;
022
023import org.jppf.utils.streams.JPPFByteArrayOutputStream;
024
025/**
026 * A collection of static utility methods to help with serialization in JPPF.
027 * @author Laurent Cohen
028 */
029public class JPPFSerializationHelper {
030  /**
031   * The configured serialization.
032   */
033  private static final JPPFSerialization serialization = JPPFSerialization.Factory.getSerialization();
034
035  /**
036   * Serialize the specified object to the specified stream, according to the configured serialization scheme.
037   * @param object the object to serialize.
038   * @param os the outout stream to serialize to.
039   * @throws Exception if any error occurs during the serialization.
040   */
041  public static void serialize(final Object object, final OutputStream os) throws Exception {
042    serialization.serialize(object, os);
043  }
044
045  /**
046   * Serialize an object to an array of bytes.
047   * @param object the object to serialize.
048   * @return an array of byte containing the serialized object, starting at position 0 and with no unused position.
049   * @throws Exception if any error occurs during the serialization.
050   */
051  public static byte[] serializeToBytes(final Object object) throws Exception {
052    try (JPPFByteArrayOutputStream os = new JPPFByteArrayOutputStream()) {
053      serialize(object, os);
054      return os.toByteArray();
055    }
056  }
057
058  /**
059   * Serialize the an object from the specified stream, according to the configured serialization scheme.
060   * @param is the input stream to deserialize from.
061   * @return a deserialized object.
062   * @throws Exception if any error occurs during the deserialization.
063   */
064  public static Object deserialize(final InputStream is) throws Exception {
065    return serialization.deserialize(is);
066  }
067
068  /**
069   * Desrialize an object from an array of bytes.
070   * @param bytes the bytes to deserialize from.
071   * @param offset the position at which to start reading in the array.
072   * @param len the number of bytes to read.
073   * @return a deserialized object.
074   * @throws Exception if any error occurs during the deserialization.
075   */
076  public static Object deserializeFromBytes(final byte[] bytes, final int offset, final int len) throws Exception {
077    try (ByteArrayInputStream is = new ByteArrayInputStream(bytes, offset, len)) {
078      return deserialize(is);
079    }
080  }
081}