1/*
2 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26#include "jni.h"
27#include "jni_util.h"
28#include "jlong.h"
29#include "jvm.h"
30
31#include "io_util.h"
32#include "io_util_md.h"
33#include "java_io_RandomAccessFile.h"
34
35#include <fcntl.h>
36
37/*
38 * static method to store field ID's in initializers
39 */
40
41jfieldID raf_fd; /* id for jobject 'fd' in java.io.RandomAccessFile */
42
43JNIEXPORT void JNICALL
44Java_java_io_RandomAccessFile_initIDs(JNIEnv *env, jclass fdClass) {
45 raf_fd = (*env)->GetFieldID(env, fdClass, "fd", "Ljava/io/FileDescriptor;");
46}
47
48
49JNIEXPORT void JNICALL
50Java_java_io_RandomAccessFile_open0(JNIEnv *env,
51 jobject this, jstring path, jint mode)
52{
53 int flags = 0;
54 if (mode & java_io_RandomAccessFile_O_RDONLY)
55 flags = O_RDONLY;
56 else if (mode & java_io_RandomAccessFile_O_RDWR) {
57 flags = O_RDWR | O_CREAT;
58 if (mode & java_io_RandomAccessFile_O_SYNC)
59 flags |= O_SYNC;
60 else if (mode & java_io_RandomAccessFile_O_DSYNC)
61 flags |= O_DSYNC;
62 }
63#ifdef WIN32
64 if (mode & java_io_RandomAccessFile_O_TEMPORARY)
65 flags |= O_TEMPORARY;
66#endif
67 fileOpen(env, this, path, raf_fd, flags);
68}
69
70JNIEXPORT jint JNICALL
71Java_java_io_RandomAccessFile_read0(JNIEnv *env, jobject this) {
72 return readSingle(env, this, raf_fd);
73}
74
75JNIEXPORT jint JNICALL
76Java_java_io_RandomAccessFile_readBytes(JNIEnv *env,
77 jobject this, jbyteArray bytes, jint off, jint len) {
78 return readBytes(env, this, bytes, off, len, raf_fd);
79}
80
81JNIEXPORT void JNICALL
82Java_java_io_RandomAccessFile_write0(JNIEnv *env, jobject this, jint byte) {
83 writeSingle(env, this, byte, JNI_FALSE, raf_fd);
84}
85
86JNIEXPORT void JNICALL
87Java_java_io_RandomAccessFile_writeBytes(JNIEnv *env,
88 jobject this, jbyteArray bytes, jint off, jint len) {
89 writeBytes(env, this, bytes, off, len, JNI_FALSE, raf_fd);
90}
91
92JNIEXPORT jlong JNICALL
93Java_java_io_RandomAccessFile_getFilePointer(JNIEnv *env, jobject this) {
94 FD fd;
95 jlong ret;
96
97 fd = GET_FD(this, raf_fd);
98 if (fd == -1) {
99 JNU_ThrowIOException(env, "Stream Closed");
100 return -1;
101 }
102 if ((ret = IO_Lseek(fd, 0L, SEEK_CUR)) == -1) {
103 JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
104 }
105 return ret;
106}
107
108JNIEXPORT jlong JNICALL
109Java_java_io_RandomAccessFile_length(JNIEnv *env, jobject this) {
110
111 FD fd;
112 jlong length = jlong_zero;
113
114 fd = GET_FD(this, raf_fd);
115 if (fd == -1) {
116 JNU_ThrowIOException(env, "Stream Closed");
117 return -1;
118 }
119 if ((length = IO_GetLength(fd)) == -1) {
120 JNU_ThrowIOExceptionWithLastError(env, "GetLength failed");
121 }
122 return length;
123}
124
125JNIEXPORT void JNICALL
126Java_java_io_RandomAccessFile_seek0(JNIEnv *env,
127 jobject this, jlong pos) {
128
129 FD fd;
130
131 fd = GET_FD(this, raf_fd);
132 if (fd == -1) {
133 JNU_ThrowIOException(env, "Stream Closed");
134 return;
135 }
136 if (pos < jlong_zero) {
137 JNU_ThrowIOException(env, "Negative seek offset");
138 } else if (IO_Lseek(fd, pos, SEEK_SET) == -1) {
139 JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
140 }
141}
142
143JNIEXPORT void JNICALL
144Java_java_io_RandomAccessFile_setLength(JNIEnv *env, jobject this,
145 jlong newLength)
146{
147 FD fd;
148 jlong cur;
149
150 fd = GET_FD(this, raf_fd);
151 if (fd == -1) {
152 JNU_ThrowIOException(env, "Stream Closed");
153 return;
154 }
155 if ((cur = IO_Lseek(fd, 0L, SEEK_CUR)) == -1) goto fail;
156 if (IO_SetLength(fd, newLength) == -1) goto fail;
157 if (cur > newLength) {
158 if (IO_Lseek(fd, 0L, SEEK_END) == -1) goto fail;
159 } else {
160 if (IO_Lseek(fd, cur, SEEK_SET) == -1) goto fail;
161 }
162 return;
163
164 fail:
165 JNU_ThrowIOExceptionWithLastError(env, "setLength failed");
166}
167