OSDN Git Service

Add new source files
[armadillo/armadillo1.git] / src / jp / sfjp / armadillo / archive / ArchiveFile.java
1 package jp.sfjp.armadillo.archive;
2
3 import java.io.*;
4 import java.util.*;
5
6 public abstract class ArchiveFile implements
7                                  Iterable<ArchiveEntry>,
8                                  ArchiveCreator,
9                                  ArchiveExtractor {
10
11     protected long currentPosition;
12     protected boolean opened;
13     private boolean closed;
14
15     protected ArchiveFile() {
16         this.currentPosition = 0L;
17         this.opened = false;
18         this.closed = false;
19     }
20
21     @Override
22     public Iterator<ArchiveEntry> iterator() {
23         return new Iterator<ArchiveEntry>() {
24             boolean first = true;
25             ArchiveEntry entry = null;
26
27             @Override
28             public boolean hasNext() {
29                 try {
30                     if (first) {
31                         reset();
32                         first = false;
33                     }
34                     entry = nextEntry();
35                 }
36                 catch (IOException ex) {
37                     handleException(ex);
38                 }
39                 assert entry != null;
40                 return entry != ArchiveEntry.NULL;
41             }
42
43             @Override
44             public ArchiveEntry next() {
45                 return entry;
46             }
47
48             @Override
49             public void remove() {
50                 try {
51                     removeEntry(entry);
52                 }
53                 catch (IOException ex) {
54                     handleException(ex);
55                 }
56             }
57
58             void handleException(Exception ex) {
59                 throw new RuntimeException(ex);
60             }
61         };
62     }
63
64     @Override
65     public void addEntry(ArchiveEntry entry, File file) throws IOException {
66         assert entry.isDirectory() == file.isDirectory();
67         if (entry.isDirectory())
68             addEntry(entry, null, 0L);
69         else {
70             FileInputStream fis = new FileInputStream(file);
71             try {
72                 addEntry(entry, fis, file.length());
73             }
74             finally {
75                 fis.close();
76             }
77         }
78     }
79
80     @Override
81     public void addEntry(ArchiveEntry entry, InputStream is, long length) throws IOException {
82         throw new UnsupportedOperationException("ArchiveFile#addEntry");
83     }
84
85     public void updateEntry(ArchiveEntry entry, File file) throws IOException {
86         assert entry.isDirectory() == file.isDirectory();
87         if (entry.isDirectory())
88             updateEntry(entry, null, 0);
89         else {
90             FileInputStream fis = new FileInputStream(file);
91             try {
92                 updateEntry(entry, fis, file.length());
93             }
94             finally {
95                 fis.close();
96             }
97         }
98     }
99
100     public void updateEntry(ArchiveEntry entry, InputStream is, long length) throws IOException {
101         removeEntry(entry);
102         addEntry(entry, is, length);
103     }
104
105     public void removeEntry(ArchiveEntry entry) throws IOException {
106         throw new UnsupportedOperationException("ArchiveFile#removeEntry");
107     }
108
109     public boolean seek(ArchiveEntry entry) throws IOException {
110         reset();
111         while (true) {
112             ArchiveEntry nextEntry = nextEntry();
113             if (nextEntry == null)
114                 break;
115             if (nextEntry.equalsName(entry))
116                 return true;
117         }
118         reset();
119         return false;
120     }
121
122     @Override
123     public ArchiveEntry nextEntry() throws IOException {
124         throw new UnsupportedOperationException("ArchiveFile#nextEntry");
125     }
126
127     @Override
128     public long extract(OutputStream os) throws IOException {
129         throw new UnsupportedOperationException("ArchiveFile#extract");
130     }
131
132     public abstract void open() throws IOException;
133
134     public abstract void reset() throws IOException;
135
136     protected final void ensureOpen() throws IOException {
137         if (!opened)
138             throw new IOException("file is not opened yet");
139         if (closed)
140             throw new IOException("file was closed");
141     }
142
143     @Override
144     public void close() throws IOException {
145         currentPosition = 0L;
146         closed = true;
147     }
148
149 }