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.FileChannel;
24 import java.nio.file.OpenOption;
25 import java.nio.file.Path;
26 import java.nio.file.StandardOpenOption;
27 import java.util.Objects;
28
29
30
31
32
33 final class FileRandomAccessOutputStream extends RandomAccessOutputStream {
34
35 private final FileChannel channel;
36
37 private long position;
38
39 FileRandomAccessOutputStream(final FileChannel channel) {
40 this.channel = Objects.requireNonNull(channel, "channel");
41 }
42
43 FileRandomAccessOutputStream(final Path file) throws IOException {
44 this(file, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE);
45 }
46
47 FileRandomAccessOutputStream(final Path file, final OpenOption... options) throws IOException {
48 this(FileChannel.open(file, options));
49 }
50
51 FileChannel channel() {
52 return channel;
53 }
54
55 @Override
56 public void close() throws IOException {
57 if (channel.isOpen()) {
58 channel.close();
59 }
60 }
61
62 @Override
63 public synchronized long position() {
64 return position;
65 }
66
67 @Override
68 public synchronized void write(final byte[] b, final int off, final int len) throws IOException {
69 ZipIoUtil.writeAll(channel, ByteBuffer.wrap(b, off, len));
70 position += len;
71 }
72
73 @Override
74 public void writeAll(final byte[] b, final int off, final int len, final long pos) throws IOException {
75 ZipIoUtil.writeAll(channel, ByteBuffer.wrap(b, off, len), pos);
76 position += len;
77 }
78 }