001package ball.activation;
002/*-
003 * ##########################################################################
004 * Utilities
005 * $Id: TempFileDataSource.java 5856 2020-04-27 20:21:05Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-util/trunk/src/main/java/ball/activation/TempFileDataSource.java $
007 * %%
008 * Copyright (C) 2008 - 2020 Allen D. Ball
009 * %%
010 * Licensed under the Apache License, Version 2.0 (the "License");
011 * you may not use this file except in compliance with the License.
012 * You may obtain a copy of the License at
013 *
014 *      http://www.apache.org/licenses/LICENSE-2.0
015 *
016 * Unless required by applicable law or agreed to in writing, software
017 * distributed under the License is distributed on an "AS IS" BASIS,
018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019 * See the License for the specific language governing permissions and
020 * limitations under the License.
021 * ##########################################################################
022 */
023import java.beans.ConstructorProperties;
024import java.io.File;
025import java.io.FileInputStream;
026import java.io.FileOutputStream;
027import java.io.IOException;
028import lombok.ToString;
029
030import static org.apache.commons.lang3.StringUtils.EMPTY;
031
032/**
033 * {@link javax.activation.DataSource} backed by a temporary {@link File}
034 * and based on {@link FileInputStream} and {@link FileOutputStream}.
035 *
036 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
037 * @version $Revision: 5856 $
038 */
039@ToString
040public class TempFileDataSource extends AbstractDataSource {
041    private final String prefix;
042    private final String suffix;
043    private final File parent;
044    private volatile File file = null;
045
046    /**
047     * @param   prefix          The file name prefix.
048     * @param   suffix          The file name suffix.
049     * @param   parent          The parent {@link File}.
050     * @param   type            Initial {@code "ContentType"} attribute
051     *                          value.
052     *
053     * @see File#createTempFile(String,String,File)
054     */
055    @ConstructorProperties({ EMPTY, EMPTY, EMPTY, "contentType" })
056    public TempFileDataSource(String prefix, String suffix, File parent,
057                              String type) {
058        super();
059
060        if (prefix != null) {
061            this.prefix = prefix;
062        } else {
063            this.prefix = TempFileDataSource.class.getSimpleName();
064        }
065
066        this.suffix = suffix;
067        this.parent = parent;
068
069        if (type != null) {
070            setContentType(type);
071        }
072    }
073
074    /**
075     * @param   type            Initial {@code "ContentType"} attribute
076     *                          value.
077     */
078    @ConstructorProperties({ "contentType" })
079    public TempFileDataSource(String type) { this(null, null, null, type); }
080
081    {
082        try {
083            clear();
084        } catch (Exception exception) {
085            throw new ExceptionInInitializerError(exception);
086        }
087    }
088
089    @Override
090    public long length() { return file.length(); }
091
092    @Override
093    public FileInputStream getInputStream() throws IOException {
094        return new FileInputStream(file);
095    }
096
097    @Override
098    public FileOutputStream getOutputStream() throws IOException {
099        FileOutputStream out = null;
100        File old = file;
101
102        if (old != null) {
103            old.delete();
104        }
105
106        file = File.createTempFile(prefix, suffix, parent);
107        out = new FileOutputStream(file);
108
109        return out;
110    }
111
112    @Override
113    protected void finalize() throws Throwable {
114        if (file != null) {
115            file.delete();
116        }
117
118        super.finalize();
119    }
120}