001/*
002 * (C) Copyright 2006-2014 Nuxeo SA (http://nuxeo.com/) and others.
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 *     http://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 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 */
020
021package org.nuxeo.ecm.core.api;
022
023import java.io.BufferedInputStream;
024import java.io.ByteArrayInputStream;
025import java.io.File;
026import java.io.FileInputStream;
027import java.io.FileOutputStream;
028import java.io.IOException;
029import java.io.InputStream;
030import java.io.ObjectInputStream;
031import java.io.ObjectOutputStream;
032import java.io.OutputStream;
033import java.io.Serializable;
034
035import org.apache.commons.io.IOUtils;
036import org.nuxeo.runtime.api.Framework;
037
038/**
039 * A serializable input stream.
040 * <p>
041 * Note: The stream is closed after the object is serialized.
042 *
043 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
044 */
045public class SerializableInputStream extends InputStream implements Serializable {
046
047    public static final int IN_MEM_LIMIT = 1024 * 64;
048
049    private static final long serialVersionUID = -2816387281878881614L;
050
051    private transient File file;
052
053    private transient InputStream in;
054
055    public SerializableInputStream(InputStream in) {
056        this.in = in;
057    }
058
059    public SerializableInputStream(byte[] content) {
060        in = new ByteArrayInputStream(content);
061    }
062
063    public SerializableInputStream(String content) {
064        this(content.getBytes());
065    }
066
067    @Override
068    public int available() throws IOException {
069        return in.available();
070    }
071
072    @Override
073    public void close() throws IOException {
074        in.close();
075    }
076
077    @Override
078    public synchronized void mark(int readlimit) {
079        in.mark(readlimit);
080    }
081
082    @Override
083    public boolean markSupported() {
084        return in.markSupported();
085    }
086
087    @Override
088    public int read() throws IOException {
089        return in.read();
090    }
091
092    @Override
093    public int read(byte[] b, int off, int len) throws IOException {
094        return in.read(b, off, len);
095    }
096
097    @Override
098    public int read(byte[] b) throws IOException {
099        return in.read(b);
100    }
101
102    @Override
103    public synchronized void reset() throws IOException {
104        in.reset();
105    }
106
107    public File getTempFile() {
108        return file;
109    }
110
111    public InputStream reopen() throws IOException {
112        if (!canReopen()) {
113            throw new IOException("Cannot reopen non persistent stream");
114        }
115        return new BufferedInputStream(new FileInputStream(file));
116    }
117
118    public boolean canReopen() {
119        return file != null && file.isFile();
120    }
121
122    @Override
123    public long skip(long n) throws IOException {
124        return in.skip(n);
125    }
126
127    private void readObject(ObjectInputStream in) throws ClassNotFoundException, IOException {
128        // always perform the default de-serialization first
129        in.defaultReadObject();
130        // create a temp file where we will put the blob content
131        file = Framework.createTempFile("SerializableIS-", ".tmp");
132        Framework.trackFile(file, file);
133        OutputStream out = null;
134        try {
135            out = new FileOutputStream(file);
136            byte[] buffer = new byte[IN_MEM_LIMIT];
137            int read;
138            int bytes = in.readInt();
139            while (bytes > -1 && (read = in.read(buffer, 0, bytes)) != -1) {
140                out.write(buffer, 0, read);
141                bytes -= read;
142                if (bytes == 0) {
143                    bytes = in.readInt();
144                }
145            }
146        } finally {
147            if (out != null) {
148                out.close();
149            }
150            if (file.isFile()) {
151                this.in = new BufferedInputStream(new FileInputStream(file));
152            }
153        }
154    }
155
156    private void writeObject(ObjectOutputStream out) throws IOException {
157        out.defaultWriteObject();
158        // write content
159        if (in == null) {
160            return;
161        }
162        try {
163            int read;
164            byte[] buf = new byte[IN_MEM_LIMIT];
165            while ((read = in.read(buf)) != -1) {
166                // next follows a chunk of 'read' bytes
167                out.writeInt(read);
168                out.write(buf, 0, read);
169            }
170            out.writeInt(-1); // EOF
171        } finally {
172            if (in != null) {
173                in.close();
174            }
175        }
176    }
177
178    @Override
179    protected void finalize() throws Throwable {
180        IOUtils.closeQuietly(in);
181        if (file != null) {
182            file.delete();
183        }
184        super.finalize();
185    }
186
187}