/[gzz]/gzz/lava/gzz/storm/impl/ZipPool.java
ViewVC logotype

Diff of /gzz/lava/gzz/storm/impl/ZipPool.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.5 by benja, Tue Dec 10 16:10:49 2002 UTC revision 1.6 by benja, Tue Dec 17 15:48:35 2002 UTC
# Line 2  package gzz.storm.impl; Line 2  package gzz.storm.impl;
2    
3  import gzz.storm.*;  import gzz.storm.*;
4  import gzz.storm.headers.*;  import gzz.storm.headers.*;
5    import gzz.util.*;
6  import java.io.*;  import java.io.*;
7  import java.util.*;  import java.util.*;
8  import java.util.zip.*;  import java.util.zip.*;
# Line 13  public class ZipPool extends AbstractPoo Line 14  public class ZipPool extends AbstractPoo
14    
15          protected Header822 getEntryHeader(BlockId id) throws IOException {          protected Header822 getEntryHeader(BlockId id) throws IOException {
16              String name = "b_" + gzz.util.HexUtil.byteArrToHex(id.getBytes());              String name = "b_" + gzz.util.HexUtil.byteArrToHex(id.getBytes());
17                
18                if(file.getEntry(name) == null)
19                    throw new FileNotFoundException(name);
20    
21              InputStream is = new              InputStream is = new
22  BufferedInputStream(id.getCheckedInputStream(  BufferedInputStream(id.getCheckedInputStream(
23  file.getInputStream(file.getEntry(name))));  file.getInputStream(file.getEntry(name))));
# Line 44  file.getInputStream(file.getEntry(name)) Line 49  file.getInputStream(file.getEntry(name))
49             }             }
50    
51             public void close() throws IOException {             public void close() throws IOException {
52                File tempFile = gzz.util.TempFileUtil.tmpFile(new File("."));                baos.close();
53                FileOutputStream fos = new FileOutputStream(tempFile);                BlockId id = makeIdFromDigest();
54                ZipOutputStream zop = new ZipOutputStream(fos);                changeZipFile(null, baos.toByteArray(), id);
55                byte[] b = new byte[4096];                block = get(id);
   
               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);  
56             }             }
57          }          }
58    
# Line 90  file.getInputStream(file.getEntry(name)) Line 64  file.getInputStream(file.getEntry(name))
64                  return new ZipBlock(id);                  return new ZipBlock(id);
65          }          }
66    
67            protected void changeZipFile(BlockId delete, byte[] add, BlockId addId)
68                                                                    throws IOException {
69                       File tempFile = gzz.util.TempFileUtil.tmpFile(new File("."));
70                       FileOutputStream fos = new FileOutputStream(tempFile);
71                       ZipOutputStream zop = new ZipOutputStream(fos);
72                       byte[] b = new byte[4096];
73                       String deleteName = null;
74    
75                       if(delete != null)
76                               deleteName = "b_" +
77                                  gzz.util.HexUtil.
78                                     byteArrToHex(delete.getBytes());
79    
80                       for (Enumeration i = file.entries(); i.hasMoreElements() ;) {
81                          ZipEntry e = (ZipEntry)i.nextElement();
82    
83                          if(e.getName().equals(deleteName))
84                             continue;
85    
86                          zop.putNextEntry(e);
87                          InputStream is = file.getInputStream(e);
88                          while(true) {
89                          int n = is.read(b);
90                          if(n < 0)
91                              break;
92                          zop.write(b, 0, n);
93                          }
94                          zop.closeEntry();
95                       }
96    
97                       if(add != null) {
98                          addId.check(add);
99    
100                          String name = "b_" +
101                             gzz.util.HexUtil.
102                                byteArrToHex(addId.getBytes());
103    
104                          ZipEntry e = new ZipEntry(name);
105    
106                          zop.putNextEntry(e);
107                          zop.write(add);
108                          zop.closeEntry();
109                       }
110    
111                       zop.close();
112    
113                       file.close();
114                       File f = new File(file.getName());
115                       f.delete();
116                       tempFile.renameTo(f);
117                       file = new ZipFile(f);
118    
         public void add(Block b) throws IOException{  
119          }          }
120    
121          public void delete(Block b) {          public void add(Block b) throws IOException {
122                byte[] bytes = CopyUtil.readBytes(b.getRawInputStream());
123                changeZipFile(null, bytes, b.getId());
124            }
125    
126            public void delete(Block b) throws IOException {
127               BlockId bid = b.getId();
128               changeZipFile(bid, null, null);
129          }          }
130    
131          public Set getIds() {          public Set getIds() {
# Line 103  file.getInputStream(file.getEntry(name)) Line 134  file.getInputStream(file.getEntry(name))
134              for (Enumeration i = file.entries(); i.hasMoreElements() ;) {              for (Enumeration i = file.entries(); i.hasMoreElements() ;) {
135                    ZipEntry e = (ZipEntry)i.nextElement();                    ZipEntry e = (ZipEntry)i.nextElement();
136                    String name = e.getName();                    String name = e.getName();
137                      
138                    if(!name.startsWith("b_"))                    if(!name.startsWith("b_"))
139                        continue;                        continue;
140    
141                    BlockId id = new BlockId(name.substring(2));                    BlockId id = new BlockId("storm:block:" + name.substring(2));
142                    result.add(id);                    result.add(id);
143              }                          }
144                
145              return result;              return result;
146          }          }
147    
# Line 123  IOException{ Line 154  IOException{
154    
155          public BlockOutputStream getBlockOutputStream(Header822 hdr) throws          public BlockOutputStream getBlockOutputStream(Header822 hdr) throws
156  IOException{  IOException{
157              return new ZipBlockOutputStream(new VerbatimHeader822(hdr));                      return new ZipBlockOutputStream(new VerbatimHeader822(hdr));
158          }          }
159  }  }

Legend:
Removed from v.1.5  
changed lines
  Added in v.1.6

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26