001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.repository.model;
016    
017    import java.io.ByteArrayInputStream;
018    import java.io.File;
019    import java.io.InputStream;
020    
021    /**
022     * @author Adolfo P??rez
023     */
024    public class FileContentReference {
025    
026            public static final FileContentReference fromBytes(
027                    String sourceFileName, String extension, String mimeType,
028                    byte[] bytes) {
029    
030                    return fromInputStream(
031                            sourceFileName, extension, mimeType,
032                            new ByteArrayInputStream(bytes), bytes.length);
033            }
034    
035            public static final FileContentReference fromFile(
036                    String sourceFileName, String extension, String mimeType, File file) {
037    
038                    return new FileContentReference(
039                            sourceFileName, extension, mimeType, file, null, 0);
040            }
041    
042            public static final FileContentReference fromInputStream(
043                    String sourceFileName, String extension, String mimeType,
044                    InputStream inputStream, long size) {
045    
046                    return new FileContentReference(
047                            sourceFileName, extension, mimeType, null, inputStream, size);
048            }
049    
050            public String getExtension() {
051                    return _extension;
052            }
053    
054            public String getMimeType() {
055                    return _mimeType;
056            }
057    
058            public long getSize() {
059                    if (_inputStream != null) {
060                            return _size;
061                    }
062    
063                    if (_file != null) {
064                            return _file.length();
065                    }
066    
067                    return 0;
068            }
069    
070            public String getSourceFileName() {
071                    return _sourceFileName;
072            }
073    
074            protected FileContentReference(
075                    String sourceFileName, String extension, String mimeType, File file,
076                    InputStream inputStream, long size) {
077    
078                    _sourceFileName = sourceFileName;
079                    _extension = extension;
080                    _mimeType = mimeType;
081                    _file = file;
082                    _inputStream = inputStream;
083                    _size = size;
084            }
085    
086            private final String _extension;
087            private final File _file;
088            private final InputStream _inputStream;
089            private final String _mimeType;
090            private final long _size;
091            private final String _sourceFileName;
092    
093    }