analysis: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / internal / tmf / ui / project / wizards / importtrace / GzipFile.java
1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Marc-Andre Laperle - Initial API and implementation.
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace;
14
15 import java.io.File;
16 import java.io.FileInputStream;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.util.Enumeration;
20 import java.util.zip.GZIPInputStream;
21
22 /**
23 * Wrapper for a Gzipped file
24 */
25 public class GzipFile implements AutoCloseable {
26
27 private static final String GZIP_EXTENSION = ".gz"; //$NON-NLS-1$
28
29 private final File fFile;
30 private final GzipEntry fEntry;
31 private GzipEntry fCurEntry;
32 private boolean fIsClosed = false;
33
34 private final InputStream fInternalEntryStream;
35
36 /**
37 * Create a new GzipFile for the given file.
38 *
39 * @param source the source file
40 * @throws IOException
41 * File not found and such
42 */
43 public GzipFile(File source) throws IOException {
44 fFile = source;
45
46 InputStream in = new FileInputStream(source);
47 try {
48 // Check if it's a GZIPInputStream.
49 fInternalEntryStream = new GZIPInputStream(in);
50 } catch (IOException e) {
51 in.close();
52 throw e;
53 }
54 String name = source.getName();
55 fEntry = new GzipEntry(name.substring(0, name.lastIndexOf(GZIP_EXTENSION)));
56 fCurEntry = fEntry;
57 }
58
59 /**
60 * Close the tar file input stream.
61 *
62 * @throws IOException if the file cannot be successfully closed
63 */
64 @Override
65 public void close() throws IOException {
66 if (fInternalEntryStream != null && !fIsClosed) {
67 fInternalEntryStream.close();
68 fIsClosed = true;
69
70 }
71 }
72
73 /**
74 * Create a new GzipFile for the given path name.
75 *
76 * @param filename
77 * the filename of the gzip file
78 * @throws IOException
79 * if the file cannot be opened
80 */
81 public GzipFile(String filename) throws IOException {
82 this(new File(filename));
83 }
84
85 /**
86 * Returns an enumeration cataloguing the tar archive.
87 *
88 * @return enumeration of all files in the archive
89 */
90 public Enumeration<GzipEntry> entries() {
91 return new Enumeration<GzipEntry>() {
92 @Override
93 public boolean hasMoreElements() {
94 return (fCurEntry != null);
95 }
96
97 @Override
98 public GzipEntry nextElement() {
99 GzipEntry oldEntry = fCurEntry;
100 fCurEntry = null;
101 return oldEntry;
102 }
103 };
104 }
105
106 /**
107 * Returns a new InputStream for the given file in the tar archive.
108 *
109 * @param entry
110 * the GzipEntry
111 * @return an input stream for the given file
112 */
113 public InputStream getInputStream(GzipEntry entry) {
114 if (entry != fEntry) {
115 throw new IllegalArgumentException();
116 }
117 return fInternalEntryStream;
118 }
119
120 /**
121 * Returns the path name of the file this archive represents.
122 *
123 * @return path
124 */
125 public String getName() {
126 return fFile.getPath();
127 }
128
129 @Override
130 protected void finalize() throws Throwable {
131 close();
132 super.finalize();
133 }
134
135 }
This page took 0.036336 seconds and 5 git commands to generate.