01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: /* $Id: SeekableOutputStream.java 496556 2007-01-16 00:59:48Z cam $ */
19:
20: package org.apache.xmlgraphics.image.codec.util;
21:
22: import java.io.IOException;
23: import java.io.OutputStream;
24: import java.io.RandomAccessFile;
25:
26: /**
27: * An <code>OutputStream</code> which can seek to an arbitrary offset.
28: *
29: * @version $Id: SeekableOutputStream.java 496556 2007-01-16 00:59:48Z cam $
30: */
31: public class SeekableOutputStream extends OutputStream {
32:
33: private RandomAccessFile file;
34:
35: /**
36: * Constructs a <code>SeekableOutputStream</code> from a
37: * <code>RandomAccessFile</code>. Unless otherwise indicated,
38: * all method invocations are fowarded to the underlying
39: * <code>RandomAccessFile</code>.
40: *
41: * @param file The <code>RandomAccessFile</code> to which calls
42: * will be forwarded.
43: * @exception IllegalArgumentException if <code>file</code> is
44: * <code>null</code>.
45: */
46: public SeekableOutputStream(RandomAccessFile file) {
47: if (file == null) {
48: throw new IllegalArgumentException("SeekableOutputStream0");
49: }
50: this .file = file;
51: }
52:
53: public void write(int b) throws IOException {
54: file.write(b);
55: }
56:
57: public void write(byte[] b) throws IOException {
58: file.write(b);
59: }
60:
61: public void write(byte[] b, int off, int len) throws IOException {
62: file.write(b, off, len);
63: }
64:
65: /**
66: * Invokes <code>getFD().sync()</code> on the underlying
67: * <code>RandomAccessFile</code>.
68: */
69: public void flush() throws IOException {
70: file.getFD().sync();
71: }
72:
73: public void close() throws IOException {
74: file.close();
75: }
76:
77: public long getFilePointer() throws IOException {
78: return file.getFilePointer();
79: }
80:
81: public void seek(long pos) throws IOException {
82: file.seek(pos);
83: }
84: }
|