source: xssim/trunk/src/simulator/workload/reader/xmlJob/TarReader.java @ 104

Revision 104, 2.4 KB checked in by wojtekp, 13 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package simulator.workload.reader.xmlJob;
2
3
4import java.io.File;
5import java.io.FileInputStream;
6import java.io.FileNotFoundException;
7import java.io.IOException;
8
9import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
10import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
11
12import simulator.workload.fileFilters.GrmsJobFileNameFilter;
13
14/**
15 *
16 * @author Marcin Krystek
17 *
18 */
19public class TarReader implements WLFileAccess{
20
21        protected TarArchiveInputStream tarStream;
22        protected TarArchiveEntry entry;
23       
24        protected TarReader(){
25               
26        }
27       
28        public TarReader(String fileName) throws FileNotFoundException{
29                FileInputStream fileStream;
30                fileStream = new FileInputStream(fileName);
31                this.tarStream = new TarArchiveInputStream(fileStream);
32        }
33       
34        public TarReader(File file) throws FileNotFoundException{
35                FileInputStream fileStream;
36                fileStream = new FileInputStream(file);
37                this.tarStream = new TarArchiveInputStream(fileStream);
38        }
39       
40        /**
41         *
42         * @return file name which contains content returned by read() method
43         */
44        public String getFileName(){
45                if(entry == null)
46                        return null;
47               
48                return entry.getName();
49        }
50       
51        /**
52         *
53         * @return content of the next file located in tar archive
54         * @throws IOException
55         *
56         */
57        public String read() throws IOException{
58
59                this.entry = getNextEntry();
60               
61                if(this.entry == null)
62                        return null;
63
64                return readEntry(this.entry);
65        }
66       
67        /**
68         *
69         */
70        public void close(){
71                if(this.tarStream != null)
72                        try {
73                                this.tarStream.close();
74                        } catch (IOException e) {
75                                e.printStackTrace();
76                        }
77        }
78       
79        /**
80         *
81         * @return
82         * @throws IOException
83         */
84        protected TarArchiveEntry getNextEntry() throws IOException{
85               
86                TarArchiveEntry entry = null;
87               
88                while((entry = this.tarStream.getNextTarEntry()) != null){
89                        if(entry.isDirectory())
90                                continue;
91                       
92                        String name = entry.getName();
93
94                        int pos = name.lastIndexOf(File.separatorChar);
95                        if(pos > 0)
96                                name = name.substring(pos + 1);
97
98                        if(!name.startsWith(GrmsJobFileNameFilter.FILE_NAME_PREFIX))
99                                continue;
100
101                        if(!name.endsWith(".xml"))
102                                continue;
103                       
104                        return entry;
105                }
106               
107                return entry;
108        }
109       
110        /**
111         *
112         * @param entry
113         * @return
114         * @throws IOException
115         */
116        protected String readEntry(TarArchiveEntry entry) throws IOException{
117               
118                int size = (int) entry.getSize();
119                byte buf[] = new byte[size];
120                this.tarStream.read(buf, 0, size);
121
122                String value = new String(buf);
123                return value;
124        }
125       
126}
Note: See TracBrowser for help on using the repository browser.