TarArchiveStructSparse.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  * http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing,
  13.  * software distributed under the License is distributed on an
  14.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15.  * KIND, either express or implied.  See the License for the
  16.  * specific language governing permissions and limitations
  17.  * under the License.
  18.  */
  19. package org.apache.commons.compress.archivers.tar;

  20. import java.util.Objects;

  21. /**
  22.  * A {@code struct sparse} in a <a href="https://www.gnu.org/software/tar/manual/html_node/Standard.html">Tar archive</a>.
  23.  * <p>
  24.  * Whereas, "struct sparse" is:
  25.  * </p>
  26.  * <pre>
  27.  * struct sparse {
  28.  * char offset[12];   // offset 0
  29.  * char numbytes[12]; // offset 12
  30.  * };
  31.  * </pre>
  32.  *
  33.  * @since 1.20
  34.  */
  35. public final class TarArchiveStructSparse {
  36.     private final long offset;
  37.     private final long numbytes;

  38.     public TarArchiveStructSparse(final long offset, final long numbytes) {
  39.         if (offset < 0) {
  40.             throw new IllegalArgumentException("offset must not be negative");
  41.         }
  42.         if (numbytes < 0) {
  43.             throw new IllegalArgumentException("numbytes must not be negative");
  44.         }
  45.         this.offset = offset;
  46.         this.numbytes = numbytes;
  47.     }

  48.     @Override
  49.     public boolean equals(final Object o) {
  50.         if (this == o) {
  51.             return true;
  52.         }
  53.         if (o == null || getClass() != o.getClass()) {
  54.             return false;
  55.         }
  56.         final TarArchiveStructSparse that = (TarArchiveStructSparse) o;
  57.         return offset == that.offset && numbytes == that.numbytes;
  58.     }

  59.     public long getNumbytes() {
  60.         return numbytes;
  61.     }

  62.     public long getOffset() {
  63.         return offset;
  64.     }

  65.     @Override
  66.     public int hashCode() {
  67.         return Objects.hash(offset, numbytes);
  68.     }

  69.     @Override
  70.     public String toString() {
  71.         return "TarArchiveStructSparse{" + "offset=" + offset + ", numbytes=" + numbytes + '}';
  72.     }
  73. }