All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.schlichtherle.truezip.fs.archive.tar.TarDriver Maven / Gradle / Ivy

/*
 * Copyright (C) 2006-2011 Schlichtherle IT Services
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.schlichtherle.truezip.fs.archive.tar;

import de.schlichtherle.truezip.entry.Entry;
import static de.schlichtherle.truezip.entry.Entry.Access.WRITE;
import static de.schlichtherle.truezip.entry.Entry.Size.DATA;
import de.schlichtherle.truezip.entry.Entry.Type;
import de.schlichtherle.truezip.fs.FsController;
import de.schlichtherle.truezip.fs.FsEntryName;
import de.schlichtherle.truezip.fs.FsInputOption;
import de.schlichtherle.truezip.fs.FsModel;
import de.schlichtherle.truezip.fs.FsOutputOption;
import de.schlichtherle.truezip.fs.archive.FsCharsetArchiveDriver;
import de.schlichtherle.truezip.fs.archive.FsMultiplexedArchiveOutputShop;
import de.schlichtherle.truezip.socket.IOPool;
import de.schlichtherle.truezip.socket.IOPoolProvider;
import de.schlichtherle.truezip.socket.InputShop;
import de.schlichtherle.truezip.socket.InputSocket;
import de.schlichtherle.truezip.socket.OutputShop;
import de.schlichtherle.truezip.socket.OutputSocket;
import de.schlichtherle.truezip.util.BitField;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.DefaultAnnotation;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.io.CharConversionException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import net.jcip.annotations.Immutable;

/**
 * An archive driver which builds Tape Archive files (TAR).
 * 
 * @author Christian Schlichtherle
 * @version $Id$
 */
@Immutable
@DefaultAnnotation(NonNull.class)
public class TarDriver extends FsCharsetArchiveDriver {

    /**
     * The default character set for entry names and comments, which is
     * {@code "US-ASCII"}.
     */
    public static final Charset TAR_CHARSET = Charset.forName("US-ASCII");

    private final IOPoolProvider provider;

    public TarDriver(final IOPoolProvider provider) {
        super(TAR_CHARSET);
        if (null == provider)
            throw new NullPointerException();
        this.provider = provider;
    }

    @Override
    protected final IOPool getPool() {
        return provider.get();
    }

    /**
     * Clears {@link FsInputOption#CACHE} in {@code options} before
     * forwarding the call to {@code controller}.
     */
    @Override
    public InputSocket getInputSocket(   FsController controller,
                                            FsEntryName name,
                                            BitField options) {
        return controller.getInputSocket(
                name,
                options.clear(FsInputOption.CACHE));
    }

    /**
     * Sets {@link FsOutputOption#COMPRESS} in {@code options} before
     * forwarding the call to {@code controller}.
     */
    @Override
    public OutputSocket getOutputSocket( FsController controller,
                                            FsEntryName name,
                                            BitField options,
                                            @CheckForNull Entry template) {
        return controller.getOutputSocket(
                name,
                options.set(FsOutputOption.COMPRESS),
                template);
    }

    @Override
    public TarArchiveEntry newEntry(
            String name,
            final Type type,
            final Entry template,
            final BitField mknod)
    throws CharConversionException {
        assertEncodable(name);
        name = toZipOrTarEntryName(name, type);
        final TarArchiveEntry entry;
        if (template instanceof TarArchiveEntry) {
            entry = newEntry(name, (TarArchiveEntry) template);
        } else {
            entry = newEntry(name);
            if (null != template) {
                entry.setModTime(template.getTime(WRITE));
                entry.setSize(template.getSize(DATA));
            }
        }
        return entry;
    }
    
    public TarArchiveEntry newEntry(String name) {
        return new TarArchiveEntry(name);
    }

    public TarArchiveEntry newEntry(String name, TarArchiveEntry template) {
        return new TarArchiveEntry(name, template);
    }

    /**
     * {@inheritDoc}
     * 

* The implementation in the class {@link TarDriver} acquires an input * stream from the given socket and forwards the call to * {@link #newTarInputShop}. */ @Override public TarInputShop newInputShop(FsModel model, InputSocket input) throws IOException { final InputStream in = newInputStream(model, input); try { return newTarInputShop(model, in); } finally { in.close(); } } protected TarInputShop newTarInputShop(FsModel model, InputStream in) throws IOException { return new TarInputShop(this, in); } /** * {@inheritDoc} *

* The implementation in the class {@link TarDriver} acquires an output * stream from the given socket, forwards the call to * {@link #newTarOutputShop} and wraps the result in a new * {@link FsMultiplexedArchiveOutputShop}. */ @Override public OutputShop newOutputShop( FsModel model, OutputSocket output, InputShop source) throws IOException { final OutputStream out = newOutputStream(model, output); try { return new FsMultiplexedArchiveOutputShop( newTarOutputShop(model, out, (TarInputShop) source), getPool()); } catch (IOException ex) { try { out.close(); } catch (IOException ex2) { throw (IOException) ex2.initCause(ex); } throw ex; } } protected TarOutputShop newTarOutputShop( FsModel model, OutputStream out, TarInputShop source) throws IOException { return new TarOutputShop(this, out); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy