4 * Copyright (C) 2016-2022 Hiroshi Miura
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * Linking this library statically or dynamically with other modules is
21 * making a combined work based on this library. Thus, the terms and
22 * conditions of the GNU General Public License cover the whole
25 * As a special exception, the copyright holders of this library give you
26 * permission to link this library with independent modules to produce an
27 * executable, regardless of the license terms of these independent
28 * modules, and to copy and distribute the resulting executable under
29 * terms of your choice, provided that you also meet, for each linked
30 * independent module, the terms and conditions of the license of that
31 * module. An independent module is a module which is not derived from
32 * or based on this library. If you modify this library, you may extend
33 * this exception to your version of the library, but you are not
34 * obligated to do so. If you do not wish to do so, delete this
35 * exception statement from your version.
40 import java.io.IOException;
41 import java.io.OutputStream;
42 import java.io.RandomAccessFile;
45 * OutputStream class supporting random access methods.
47 * @author Hiroshi Miura
49 public class RandomAccessOutputStream extends OutputStream {
51 private RandomAccessFile out;
54 * Construct RandomAccessOutputStream from file.
56 * @param outFile RamdomAccessFile
58 public RandomAccessOutputStream(final RandomAccessFile outFile) {
63 * Construct RandomAccessOutputStream from filename.
65 * @param file to write with random access.
66 * @param mode open mode.
67 * @exception IOException if an I/O error has occurred.
69 public RandomAccessOutputStream(final String file, final String mode) throws IOException {
70 this(new RandomAccessFile(file, mode));
74 public final synchronized void write(final byte[] buf, final int off, final int len)
76 out.write(buf, off, len);
80 public final synchronized void write(final int b) throws IOException {
87 * @param pos file position in byte.
88 * @exception IOException if an I/O error has occurred.
90 public final void seek(final long pos) throws IOException {
97 * @throws IOException if on I/O error occurred
99 public final long position() throws IOException {
100 return out.getFilePointer();
105 * @return length of file.
106 * @throws IOException when on I/O error occurred
108 public final long length() throws IOException {