1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.compress.archivers.zip;
20
21 import java.io.IOException;
22 import java.nio.ByteBuffer;
23 import java.nio.channels.SeekableByteChannel;
24
25
26
27
28
29 final class SeekableChannelRandomAccessOutputStream extends RandomAccessOutputStream {
30
31 private final SeekableByteChannel channel;
32
33 SeekableChannelRandomAccessOutputStream(final SeekableByteChannel channel) {
34 this.channel = channel;
35 }
36
37 @Override
38 public synchronized void close() throws IOException {
39 channel.close();
40 }
41
42 @Override
43 public synchronized long position() throws IOException {
44 return channel.position();
45 }
46
47 @Override
48 public synchronized void write(final byte[] b, final int off, final int len) throws IOException {
49 ZipIoUtil.writeAll(this.channel, ByteBuffer.wrap(b, off, len));
50 }
51
52 @Override
53 public synchronized void writeAll(final byte[] b, final int off, final int len, final long position) throws IOException {
54 final long saved = channel.position();
55 try {
56 channel.position(position);
57 ZipIoUtil.writeAll(channel, ByteBuffer.wrap(b, off, len));
58 } finally {
59 channel.position(saved);
60 }
61 }
62 }