001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Thomas Roger
016 */
017
018package org.nuxeo.ecm.core.api.impl.blob;
019
020import java.io.File;
021import java.io.IOException;
022import java.io.InputStream;
023import java.io.OutputStream;
024import java.io.Serializable;
025
026import org.nuxeo.ecm.core.api.Blob;
027import org.nuxeo.ecm.core.api.CloseableFile;
028
029/**
030 * Wraps an existing {@link Blob} to allow setting a different filename.
031 *
032 * @since 5.9.2
033 */
034public class BlobWrapper implements Blob, Serializable {
035
036    private static final long serialVersionUID = 1L;
037
038    protected final Blob blob;
039
040    protected String filename;
041
042    public BlobWrapper(Blob blob) {
043        this.blob = blob;
044        filename = blob.getFilename();
045    }
046
047    @Override
048    public String getFilename() {
049        return filename;
050    }
051
052    @Override
053    public void setFilename(String filename) {
054        this.filename = filename;
055    }
056
057    // the rest of the implementation just delegates to the blob
058
059    @Override
060    public String getMimeType() {
061        return blob.getMimeType();
062    }
063
064    @Override
065    public String getEncoding() {
066        return blob.getEncoding();
067    }
068
069    @Override
070    public String getDigestAlgorithm() {
071        return blob.getDigestAlgorithm();
072    }
073
074    @Override
075    public String getDigest() {
076        return blob.getDigest();
077    }
078
079    @Override
080    public void setMimeType(String mimeType) {
081        blob.setMimeType(mimeType);
082    }
083
084    @Override
085    public void setEncoding(String encoding) {
086        blob.setEncoding(encoding);
087    }
088
089    @Override
090    public void setDigest(String digest) {
091        blob.setDigest(digest);
092    }
093
094    @Override
095    public InputStream getStream() throws IOException {
096        return blob.getStream();
097    }
098
099    @Override
100    public long getLength() {
101        return blob.getLength();
102    }
103
104    @Override
105    public byte[] getByteArray() throws IOException {
106        return blob.getByteArray();
107    }
108
109    @Override
110    public String getString() throws IOException {
111        return blob.getString();
112    }
113
114    @Override
115    public void transferTo(OutputStream out) throws IOException {
116        blob.transferTo(out);
117    }
118
119    @Override
120    public void transferTo(File file) throws IOException {
121        blob.transferTo(file);
122    }
123
124    @Override
125    public File getFile() {
126        return blob.getFile();
127    }
128
129    @Override
130    public CloseableFile getCloseableFile() throws IOException {
131        return blob.getCloseableFile();
132    }
133
134    @Override
135    public CloseableFile getCloseableFile(String ext) throws IOException {
136        return blob.getCloseableFile(ext);
137    }
138
139}