001/* 002 * Copyright 2002-2015 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.core.serializer; 018 019import java.io.IOException; 020import java.io.ObjectOutputStream; 021import java.io.OutputStream; 022import java.io.Serializable; 023 024/** 025 * A {@link Serializer} implementation that writes an object to an output stream 026 * using Java serialization. 027 * 028 * @author Gary Russell 029 * @author Mark Fisher 030 * @since 3.0.5 031 */ 032public class DefaultSerializer implements Serializer<Object> { 033 034 /** 035 * Writes the source object to an output stream using Java serialization. 036 * The source object must implement {@link Serializable}. 037 * @see ObjectOutputStream#writeObject(Object) 038 */ 039 @Override 040 public void serialize(Object object, OutputStream outputStream) throws IOException { 041 if (!(object instanceof Serializable)) { 042 throw new IllegalArgumentException(getClass().getSimpleName() + " requires a Serializable payload " + 043 "but received an object of type [" + object.getClass().getName() + "]"); 044 } 045 ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream); 046 objectOutputStream.writeObject(object); 047 objectOutputStream.flush(); 048 } 049 050}