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 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.core.blob.binary;
020
021import java.io.File;
022import java.io.IOException;
023import java.io.InputStream;
024import java.io.Serializable;
025import java.util.Objects;
026
027import org.nuxeo.ecm.core.api.Blob;
028import org.nuxeo.ecm.core.api.impl.blob.AbstractBlob;
029import org.nuxeo.ecm.core.blob.ManagedBlob;
030
031/**
032 * A {@link Blob} wrapping a {@link Binary} value.
033 */
034public class BinaryBlob extends AbstractBlob implements ManagedBlob, Serializable {
035
036    private static final long serialVersionUID = 1L;
037
038    protected final Binary binary;
039
040    /**
041     * The key, which is the binary's digest but may in addition be prefixed by a blob provider id.
042     */
043    protected final String key;
044
045    protected final long length;
046
047    public BinaryBlob(Binary binary, String key, String filename, String mimeType, String encoding, String digest,
048            long length) {
049        this.binary = binary;
050        this.key = key;
051        this.length = length;
052        setFilename(filename);
053        setMimeType(mimeType);
054        setEncoding(encoding);
055        setDigest(digest);
056    }
057
058    @Override
059    public long getLength() {
060        return length;
061    }
062
063    @Override
064    public InputStream getStream() throws IOException {
065        return binary.getStream();
066    }
067
068    /**
069     * Gets the {@link Binary} attached to this blob.
070     *
071     * @since 5.9.4
072     * @return the binary
073     */
074    public Binary getBinary() {
075        return binary;
076    }
077
078    @Override
079    public String getDigestAlgorithm() {
080        return Objects.equals(binary.getDigest(), getDigest()) ? binary.getDigestAlgorithm() : null;
081    }
082
083    @Override
084    public String getDigest() {
085        String digest = super.getDigest();
086        if (digest == null) {
087            return binary.getDigest();
088        } else {
089            return digest;
090        }
091    }
092
093    @Override
094    public String getKey() {
095        return key;
096    }
097
098    @Override
099    public String getProviderId() {
100        return binary.getBlobProviderId();
101    }
102
103    @Override
104    public File getFile() {
105        return binary.getFile();
106    }
107
108    /*
109     * Optimized stream comparison method.
110     */
111    @Override
112    protected boolean equalsStream(Blob blob) {
113        if (!(blob instanceof BinaryBlob)) {
114            return super.equalsStream(blob);
115        }
116        BinaryBlob other = (BinaryBlob) blob;
117        if (binary == null) {
118            return other.binary == null;
119        } else if (other.binary == null) {
120            return false;
121        } else {
122            return binary.getDigest().equals(other.binary.getDigest());
123        }
124    }
125
126}