    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one
    003 * or more contributor license agreements.  See the NOTICE file
    004 * distributed with this work for additional information
    005 * regarding copyright ownership.  The ASF licenses this file
    006 * to you under the Apache License, Version 2.0 (the
    007 * "License"); you may not use this file except in compliance
    008 * with the License.  You may obtain a copy of the License at
    009 *
    010 * http://www.apache.org/licenses/LICENSE-2.0
    011 *
    012 * Unless required by applicable law or agreed to in writing,
    013 * software distributed under the License is distributed on an
    014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    015 * KIND, either express or implied.  See the License for the
    016 * specific language governing permissions and limitations
    017 * under the License.
    018 */
    019package org.apache.commons.compress.archivers.tar;
    020
    021import java.io.IOException;
    022import java.util.ArrayList;
    023import java.util.List;
    024
    025/**
    026 * This class represents a sparse entry in a Tar archive.
    027 *
    028 * <p>
    029 * The C structure for a sparse entry is:
    030 * <pre>
    031 * struct posix_header {
    032 * struct sparse sp[21]; // TarConstants.SPARSELEN_GNU_SPARSE     - offset 0
    033 * char isextended;      // TarConstants.ISEXTENDEDLEN_GNU_SPARSE - offset 504
    034 * };
    035 * </pre>
    036 * Whereas, "struct sparse" is:
    037 * <pre>
    038 * struct sparse {
    039 * char offset[12];   // offset 0
    040 * char numbytes[12]; // offset 12
    041 * };
    042 * </pre>
    043 *
    044 * <p>Each such struct describes a block of data that has actually been written to the archive. The offset describes
    045 * where in the extracted file the data is supposed to start and the numbytes provides the length of the block. When
    046 * extracting the entry the gaps between the sparse structs are equivalent to areas filled with zero bytes.</p>
    047 */
    048
    049public class TarArchiveSparseEntry implements TarConstants {
    050    /** If an extension sparse header follows. */
    051    private final boolean isExtended;
    052
    053    private final List<TarArchiveStructSparse> sparseHeaders;
    054
    055    /**
    056     * Construct an entry from an archive's header bytes. File is set
    057     * to null.
    058     *
    059     * @param headerBuf The header bytes from a tar archive entry.
    060     * @throws IOException on unknown format
    061     */
    062    public TarArchiveSparseEntry(final byte[] headerBuf) throws IOException {
    063        int offset = 0;
    064        sparseHeaders = new ArrayList<>(TarUtils.readSparseStructs(headerBuf, 0, SPARSE_HEADERS_IN_EXTENSION_HEADER));
    065        offset += SPARSELEN_GNU_SPARSE;
    066        isExtended = TarUtils.parseBoolean(headerBuf, offset);
    067    }
    068
    069    public boolean isExtended() {
    070        return isExtended;
    071    }
    072
    073    /**
    074     * Obtains information about the configuration for the sparse entry.
    075     * @since 1.20
    076     * @return information about the configuration for the sparse entry.
    077     */
    078    public List<TarArchiveStructSparse> getSparseHeaders() {
    079        return sparseHeaders;
    080    }
    081}