01: /*
02: * $RCSfile: SeekableOutputStream.java,v $
03: *
04: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
05: *
06: * Use is subject to license terms.
07: *
08: * $Revision: 1.1 $
09: * $Date: 2005/02/11 04:55:32 $
10: * $State: Exp $
11: */
12: package com.sun.media.jai.codec;
13:
14: import java.io.FileDescriptor;
15: import java.io.IOException;
16: import java.io.OutputStream;
17: import java.io.RandomAccessFile;
18:
19: /**
20: * An <code>OutputStream</code> which can seek to an arbitrary offset.
21: */
22: public class SeekableOutputStream extends OutputStream {
23:
24: private RandomAccessFile file;
25:
26: /**
27: * Constructs a <code>SeekableOutputStream</code> from a
28: * <code>RandomAccessFile</code>. Unless otherwise indicated,
29: * all method invocations are fowarded to the underlying
30: * <code>RandomAccessFile</code>.
31: *
32: * @param file The <code>RandomAccessFile</code> to which calls
33: * will be forwarded.
34: * @exception IllegalArgumentException if <code>file</code> is
35: * <code>null</code>.
36: */
37: public SeekableOutputStream(RandomAccessFile file) {
38: if (file == null) {
39: throw new IllegalArgumentException(JaiI18N
40: .getString("SeekableOutputStream0"));
41: }
42: this .file = file;
43: }
44:
45: public void write(int b) throws IOException {
46: file.write(b);
47: }
48:
49: public void write(byte b[]) throws IOException {
50: file.write(b);
51: }
52:
53: public void write(byte b[], int off, int len) throws IOException {
54: file.write(b, off, len);
55: }
56:
57: /**
58: * Invokes <code>getFD().sync()</code> on the underlying
59: * <code>RandomAccessFile</code>.
60: */
61: public void flush() throws IOException {
62: // Fix: 4636212. When this FIleDescriptor is not valid, do nothing.
63: FileDescriptor fd = file.getFD();
64: if (fd.valid())
65: fd.sync();
66: }
67:
68: public void close() throws IOException {
69: file.close();
70: }
71:
72: public long getFilePointer() throws IOException {
73: return file.getFilePointer();
74: }
75:
76: public void seek(long pos) throws IOException {
77: file.seek(pos);
78: }
79: }
|