1:
30:
31: package ;
32:
33: import ;
34: import ;
35: import ;
36: import ;
37: import ;
38: import ;
39: import ;
40:
41:
46: public class ZipEntryOutputStream extends OutputStream
47: {
48: private ByteArrayOutputStream outputStream;
49: private DeflaterOutputStream deflaterOutputStream;
50: private boolean closed;
51: private ZipContentItem item;
52:
53: public ZipEntryOutputStream(final ZipContentItem item)
54: {
55: this.item = item;
56: this.outputStream = new ByteArrayOutputStream();
57: this.deflaterOutputStream = new DeflaterOutputStream(outputStream);
58: }
59:
60: public void write(final int b)
61: throws IOException
62: {
63: if (closed)
64: {
65: throw new IOException("Already closed");
66: }
67: deflaterOutputStream.write(b);
68: }
69:
70: public void write(final byte[] b, final int off, final int len)
71: throws IOException
72: {
73: if (closed)
74: {
75: throw new IOException("Already closed");
76: }
77: deflaterOutputStream.write(b, off, len);
78: }
79:
80: public void close()
81: throws IOException
82: {
83: if (closed)
84: {
85: throw new IOException("Already closed");
86: }
87:
88: deflaterOutputStream.close();
89: final byte[] data = outputStream.toByteArray();
90: final ByteArrayInputStream bin = new ByteArrayInputStream(data);
91: final InflaterInputStream infi = new InflaterInputStream(bin);
92:
93: final ZipRepository repository = (ZipRepository) item.getRepository();
94:
95: final String contentId = (String) item.getContentId();
96: repository.writeContent(new ZipEntry(contentId), infi);
97: infi.close();
98:
99: outputStream = null;
100: deflaterOutputStream = null;
101: }
102:
103: public void write(final byte[] b)
104: throws IOException
105: {
106: if (closed)
107: {
108: throw new IOException("Already closed");
109: }
110: deflaterOutputStream.write(b);
111: }
112:
113: public void flush()
114: throws IOException
115: {
116: if (closed)
117: {
118: throw new IOException("Already closed");
119: }
120: deflaterOutputStream.flush();
121: }
122: }