OSDN Git Service

6f9399cda849668c063f5b402e9a0ee2ce5183d7
[dictzip-java/dictzip-java.git] / dictzip-lib / src / main / java / org / dict / zip / RandomAccessOutputStream.java
1 /*
2  * DictZip library.
3  *
4  * Copyright (C) 2016-2022 Hiroshi Miura
5  *
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.
10  *
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.
15  *
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.
19  *
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
23  * combination.
24  *
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.
36  */
37
38 package org.dict.zip;
39
40 import java.io.IOException;
41 import java.io.OutputStream;
42 import java.io.RandomAccessFile;
43
44 /**
45  * OutputStream class supporting random access methods.
46  *
47  * @author Hiroshi Miura
48  */
49 public class RandomAccessOutputStream extends OutputStream {
50
51     private RandomAccessFile out;
52
53     /**
54      * Construct RandomAccessOutputStream from file.
55      *
56      * @param outFile RamdomAccessFile
57      */
58     public RandomAccessOutputStream(final RandomAccessFile outFile) {
59         this.out = outFile;
60     }
61
62     /**
63      * Construct RandomAccessOutputStream from filename.
64      *
65      * @param file to write with random access.
66      * @param mode open mode.
67      * @exception IOException if an I/O error has occurred.
68      */
69     public RandomAccessOutputStream(final String file, final String mode) throws IOException {
70         this(new RandomAccessFile(file, mode));
71     }
72
73     @Override
74     public final synchronized void write(final byte[] buf, final int off, final int len)
75             throws IOException {
76         out.write(buf, off, len);
77     }
78
79     @Override
80     public final synchronized void write(final int b) throws IOException {
81         out.write(b);
82     }
83
84     /**
85      * Seek file position.
86      *
87      * @param pos file position in byte.
88      * @exception IOException if an I/O error has occurred.
89      */
90     public final void seek(final long pos) throws IOException {
91         out.seek(pos);
92     }
93
94     /**
95      * Get file position;
96      * @return position
97      * @throws IOException if on I/O error occurred
98      */
99     public final long position() throws IOException {
100         return out.getFilePointer();
101     }
102
103     /**
104      * Get file length.
105      * @return length of file.
106      * @throws IOException when on I/O error occurred
107      */
108     public final long length() throws IOException {
109         return out.length();
110     }
111 }