View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one or more
3    *  contributor license agreements.  See the NOTICE file distributed with
4    *  this work for additional information regarding copyright ownership.
5    *  The ASF licenses this file to You under the Apache License, Version 2.0
6    *  (the "License"); you may not use this file except in compliance with
7    *  the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   *
17   */
18  
19  package org.apache.commons.compress.archivers.tar;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertNotNull;
23  import static org.junit.Assert.assertNull;
24  
25  import java.util.Random;
26  
27  import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
28  import org.junit.Test;
29  
30  public class BigFilesIT {
31  
32      @Test
33      public void readFileBiggerThan8GByteStar() throws Exception {
34          readFileBiggerThan8GByte("/8.star.tar.gz");
35      }
36  
37      @Test
38      public void readFileBiggerThan8GBytePosix() throws Exception {
39          readFileBiggerThan8GByte("/8.posix.tar.gz");
40      }
41  
42      private void readFileBiggerThan8GByte(String name) throws Exception {
43          GzipCompressorInputStream in = null;
44          TarArchiveInputStream tin = null;
45          try {
46              in =
47                  new GzipCompressorInputStream(BigFilesIT.class
48                                                .getResourceAsStream(name));
49              tin = new TarArchiveInputStream(in);
50              TarArchiveEntry e = tin.getNextTarEntry();
51              assertNotNull(e);
52              assertEquals(8200l * 1024 * 1024, e.getSize());
53  
54              long read = 0;
55              Random r = new Random(System.currentTimeMillis());
56              int readNow;
57              byte[] buf = new byte[1024 * 1024];
58              while ((readNow = tin.read(buf, 0, buf.length)) > 0) {
59                  // testing all bytes for a value of 0 is going to take
60                  // too long, just pick a few ones randomly
61                  for (int i = 0; i < 100; i++) {
62                      int idx = r.nextInt(readNow);
63                      assertEquals("testing byte " + (read + idx), 0, buf[idx]);
64                  }
65                  read += readNow;
66              }
67              assertEquals(8200l * 1024 * 1024, read);
68              assertNull(tin.getNextTarEntry());
69          } finally {
70              if (tin != null) {
71                  tin.close();
72              }
73              if (in != null) {
74                  in.close();
75              }
76          }
77      }
78  
79  }