001/*
002 * (C) Copyright 2006-2011 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 *     Florent Guillaume
018 */
019
020package org.nuxeo.ecm.core.blob.binary;
021
022import java.io.File;
023import java.io.FileInputStream;
024import java.io.IOException;
025import java.io.InputStream;
026import java.io.Serializable;
027
028import org.nuxeo.ecm.core.blob.BlobManager;
029import org.nuxeo.ecm.core.blob.BlobProvider;
030import org.nuxeo.runtime.api.Framework;
031
032/**
033 * A binary object that can be read, and has a length and a digest.
034 *
035 * @author Florent Guillaume
036 * @author Bogdan Stefanescu
037 */
038public class Binary implements Serializable {
039
040    private static final long serialVersionUID = 1L;
041
042    protected final String digest;
043
044    protected final String blobProviderId;
045
046    protected transient File file;
047
048    protected long length;
049
050    protected Binary(String digest, String blobProviderId) {
051        this(null, digest, blobProviderId);
052    }
053
054    public Binary(File file, String digest, String blobProviderId) {
055        this.file = file;
056        this.digest = digest;
057        this.blobProviderId = blobProviderId;
058        length = -1;
059    }
060
061    /**
062     * Compute length on demand, default implementation only works if the file referenced contains the binary original
063     * content. If you're contributing a binary type, you should adapt this in case you're encoding the content. This
064     * method is only used when users make a direct access to the binary. Persisted blobs don't use that API.
065     *
066     * @since 5.7.3
067     */
068    protected long computeLength() {
069        if (file == null) {
070            return -1;
071        }
072        return file.length();
073    }
074
075    /**
076     * Gets the length of the binary.
077     *
078     * @return the length of the binary
079     */
080    public long getLength() {
081        if (length == -1) {
082            length = computeLength();
083        }
084        return length;
085    }
086
087    /**
088     * Gets the digest algorithm from the digest length.
089     *
090     * @since 7.4
091     */
092    public String getDigestAlgorithm() {
093        // Cannot use current digest algorithm of the binary manager here since it might have changed after the binary
094        // storage
095        String digest = getDigest();
096        if (digest == null) {
097            return null;
098        }
099        return AbstractBinaryManager.DIGESTS_BY_LENGTH.get(digest.length());
100    }
101
102    /**
103     * Gets a string representation of the hex digest of the binary.
104     *
105     * @return the digest, characters are in the range {@code [0-9a-f]}
106     */
107    public String getDigest() {
108        return digest;
109    }
110
111    /**
112     * Gets the blob provider which created this blob.
113     * <p>
114     * This is usually the repository name.
115     *
116     * @return the blob provider id
117     * @since 7.3
118     */
119    public String getBlobProviderId() {
120        return blobProviderId;
121    }
122
123    /**
124     * Gets an input stream for the binary.
125     *
126     * @return the input stream
127     * @throws IOException
128     */
129    public InputStream getStream() throws IOException {
130        return new FileInputStream(file);
131    }
132
133    @Override
134    public String toString() {
135        return getClass().getSimpleName() + '(' + digest + ')';
136    }
137
138    public File getFile() {
139        return file;
140    }
141
142    private void writeObject(java.io.ObjectOutputStream oos) throws IOException, ClassNotFoundException {
143        oos.defaultWriteObject();
144    }
145
146    private void readObject(java.io.ObjectInputStream ois) throws IOException, ClassNotFoundException {
147        ois.defaultReadObject();
148        file = recomputeFile();
149    }
150
151    /**
152     * Recomputes the file attribute by getting it from a new Binary for the same digest.
153     */
154    protected File recomputeFile() {
155        BlobManager bm = Framework.getService(BlobManager.class);
156        BlobProvider bp = bm.getBlobProvider(blobProviderId);
157        return bp.getBinaryManager().getBinary(digest).file;
158    }
159
160}