[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Gzz-commits] gzz/lava/gzz/storm/impl ZipPool.java
From: |
Benja Fallenstein |
Subject: |
[Gzz-commits] gzz/lava/gzz/storm/impl ZipPool.java |
Date: |
Tue, 10 Dec 2002 11:10:50 -0500 |
CVSROOT: /cvsroot/gzz
Module name: gzz
Changes by: Benja Fallenstein <address@hidden> 02/12/10 11:10:50
Modified files:
lava/gzz/storm/impl: ZipPool.java
Log message:
More work from Anton-- finally ZipPool starts to do *something* right ;)
CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/gzz/gzz/lava/gzz/storm/impl/ZipPool.java.diff?tr1=1.4&tr2=1.5&r1=text&r2=text
Patches:
Index: gzz/lava/gzz/storm/impl/ZipPool.java
diff -u gzz/lava/gzz/storm/impl/ZipPool.java:1.4
gzz/lava/gzz/storm/impl/ZipPool.java:1.5
--- gzz/lava/gzz/storm/impl/ZipPool.java:1.4 Sat Nov 30 08:16:54 2002
+++ gzz/lava/gzz/storm/impl/ZipPool.java Tue Dec 10 11:10:49 2002
@@ -9,79 +9,120 @@
public class ZipPool extends AbstractPool {
protected Map blocks = new HashMap();
+ protected ZipFile file;
- public ZipPool(File file){
-
+ protected Header822 getEntryHeader(BlockId id) throws IOException {
+ String name = "b_" + gzz.util.HexUtil.byteArrToHex(id.getBytes());
+ InputStream is = new
+BufferedInputStream(id.getCheckedInputStream(
+file.getInputStream(file.getEntry(name))));
+
+ Header822 header = Headers822.readHeader(is);
+ is.close();
+ return header;
+ }
+
+ public class ZipBlock extends AbstractBlock {
+ public ZipBlock(BlockId id) throws IOException {
+ super(id, getEntryHeader(id));
+ }
+
+ public InputStream getRawInputStream() throws IOException {
+ String name = "b_" +
gzz.util.HexUtil.byteArrToHex(id.getBytes());
+ return new BufferedInputStream(
+ id.getCheckedInputStream(
+ file.getInputStream(file.getEntry(name))));
+ }
+ }
+
+ public class ZipBlockOutputStream extends AbstractBlockOutputStream{
+ public ByteArrayOutputStream baos;
+
+ public ZipBlockOutputStream(Header822 header) throws IOException {
+ super(new ByteArrayOutputStream(), header);
+ baos = (ByteArrayOutputStream)out;
+ }
+
+ public void close() throws IOException {
+ File tempFile = gzz.util.TempFileUtil.tmpFile(new File("."));
+ FileOutputStream fos = new FileOutputStream(tempFile);
+ ZipOutputStream zop = new ZipOutputStream(fos);
+ byte[] b = new byte[4096];
+
+ for (Enumeration i = file.entries(); i.hasMoreElements() ;) {
+ ZipEntry e = (ZipEntry)i.nextElement();
+ zop.putNextEntry(e);
+ InputStream is = file.getInputStream(e);
+ while(true) {
+ int n = is.read(b);
+ if(n < 0)
+ break;
+ zop.write(b, 0, n);
+ }
+ zop.closeEntry();
+ }
+
+ BlockId id = makeIdFromDigest();
+ String name = "b_" + gzz.util.HexUtil.byteArrToHex(id.getBytes());
+ ZipEntry e = new ZipEntry(name);
+
+ zop.putNextEntry(e);
+ zop.write(baos.toByteArray());
+ zop.closeEntry();
+
+ zop.close();
+
+ file.close();
+ File f = new File(file.getName());
+ f.delete();
+ tempFile.renameTo(f);
+ file = new ZipFile(f);
+
+ block = get(id);
+ }
}
- public Block get(BlockId id) throws FileNotFoundException {
- if(!blocks.keySet().contains(id))
- throw new FileNotFoundException("No such block: "+id);
-
- return (Block)blocks.get(id);
+ public ZipPool(ZipFile file){
+ this.file = file;
}
- public void add(Block b) throws IOException{
- byte[] bytes =
gzz.util.CopyUtil.readBytes(b.getRawInputStream());
-
- BlockId id = b.getId();
- id.check(bytes);
-
- InputStream is = new ByteArrayInputStream(bytes);
- Header822 header = Headers822.readHeader(is);
- is.close();
-
- Block block = new ZipBlock(id, bytes, header);
- blocks.put(id, block);
+ public Block get(BlockId id) throws IOException {
+ return new ZipBlock(id);
}
- public void delete(Block b) {
- blocks.keySet().remove(b.getId());}
- public Set getIds() {
- return blocks.keySet();
+ public void add(Block b) throws IOException{
}
- protected class ZipBlockOutputStream extends AbstractBlockOutputStream{
- protected ByteArrayOutputStream baos;
-
- protected ZipBlockOutputStream(Header822 header)
- throws IOException {
- super(new ByteArrayOutputStream(), header);
- baos = (ByteArrayOutputStream)out;
- }
-
- public void close() throws IOException {
- block = new ZipBlock(makeIdFromDigest(),
- baos.toByteArray(), header);
- blocks.put(block.getId(), block);
+ public void delete(Block b) {
}
- }
-
- protected class ZipBlock extends AbstractBlock{
- protected byte[] bytes;
- protected int headerLength;
- protected ZipBlock(BlockId id, byte[] bytes,
- Header822 header) throws IOException {
- super(id, header);
- this.bytes = bytes;
- }
+ public Set getIds() {
+ Set result = new HashSet();
- public InputStream getRawInputStream() throws IOException {
- return new ByteArrayInputStream(bytes);
+ for (Enumeration i = file.entries(); i.hasMoreElements() ;) {
+ ZipEntry e = (ZipEntry)i.nextElement();
+ String name = e.getName();
+
+ if(!name.startsWith("b_"))
+ continue;
+
+ BlockId id = new BlockId(name.substring(2));
+ result.add(id);
+ }
+
+ return result;
}
- }
public BlockOutputStream getBlockOutputStream(String contentType)throws
IOException{
- Header822 hdr = new UniqueHeader822();
- hdr.add("Content-Type", contentType);
- return new ZipBlockOutputStream(hdr);
+ Header822 hdr = new UniqueHeader822();
+ hdr.add("Content-Type", contentType);
+ return new ZipBlockOutputStream(hdr);
}
public BlockOutputStream getBlockOutputStream(Header822 hdr) throws
IOException{
- return new ZipBlockOutputStream(new VerbatimHeader822(hdr));
- }
+ return new ZipBlockOutputStream(new VerbatimHeader822(hdr));
+ }
}
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Gzz-commits] gzz/lava/gzz/storm/impl ZipPool.java,
Benja Fallenstein <=