package simulator.workload.reader.xmlJob; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import org.apache.commons.compress.archivers.tar.TarArchiveEntry; import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; import simulator.workload.fileFilters.GrmsJobFileNameFilter; /** * * @author Marcin Krystek * */ public class TarReader implements WLFileAccess{ protected TarArchiveInputStream tarStream; protected TarArchiveEntry entry; protected TarReader(){ } public TarReader(String fileName) throws FileNotFoundException{ FileInputStream fileStream; fileStream = new FileInputStream(fileName); this.tarStream = new TarArchiveInputStream(fileStream); } public TarReader(File file) throws FileNotFoundException{ FileInputStream fileStream; fileStream = new FileInputStream(file); this.tarStream = new TarArchiveInputStream(fileStream); } /** * * @return file name which contains content returned by read() method */ public String getFileName(){ if(entry == null) return null; return entry.getName(); } /** * * @return content of the next file located in tar archive * @throws IOException * */ public String read() throws IOException{ this.entry = getNextEntry(); if(this.entry == null) return null; return readEntry(this.entry); } /** * */ public void close(){ if(this.tarStream != null) try { this.tarStream.close(); } catch (IOException e) { e.printStackTrace(); } } /** * * @return * @throws IOException */ protected TarArchiveEntry getNextEntry() throws IOException{ TarArchiveEntry entry = null; while((entry = this.tarStream.getNextTarEntry()) != null){ if(entry.isDirectory()) continue; String name = entry.getName(); int pos = name.lastIndexOf(File.separatorChar); if(pos > 0) name = name.substring(pos + 1); if(!name.startsWith(GrmsJobFileNameFilter.FILE_NAME_PREFIX)) continue; if(!name.endsWith(".xml")) continue; return entry; } return entry; } /** * * @param entry * @return * @throws IOException */ protected String readEntry(TarArchiveEntry entry) throws IOException{ int size = (int) entry.getSize(); byte buf[] = new byte[size]; this.tarStream.read(buf, 0, size); String value = new String(buf); return value; } }