blob: 78820634ae08d368016e4b59aadebb7dc39f84d9 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1997-2007 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any 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_open(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 fileOpen(env, this, path, raf_fd, flags);
64}
65
66JNIEXPORT jint JNICALL
67Java_java_io_RandomAccessFile_read(JNIEnv *env, jobject this) {
68 return readSingle(env, this, raf_fd);
69}
70
71JNIEXPORT jint JNICALL
72Java_java_io_RandomAccessFile_readBytes(JNIEnv *env,
73 jobject this, jbyteArray bytes, jint off, jint len) {
74 return readBytes(env, this, bytes, off, len, raf_fd);
75}
76
77JNIEXPORT void JNICALL
78Java_java_io_RandomAccessFile_write(JNIEnv *env, jobject this, jint byte) {
79 writeSingle(env, this, byte, raf_fd);
80}
81
82JNIEXPORT void JNICALL
83Java_java_io_RandomAccessFile_writeBytes(JNIEnv *env,
84 jobject this, jbyteArray bytes, jint off, jint len) {
85 writeBytes(env, this, bytes, off, len, raf_fd);
86}
87
88JNIEXPORT jlong JNICALL
89Java_java_io_RandomAccessFile_getFilePointer(JNIEnv *env, jobject this) {
90 FD fd;
91 jlong ret;
92
93 fd = GET_FD(this, raf_fd);
94 if (fd == -1) {
95 JNU_ThrowIOException(env, "Stream Closed");
96 return -1;
97 }
98 if ((ret = IO_Lseek(fd, 0L, SEEK_CUR)) == -1) {
99 JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
100 }
101 return ret;
102}
103
104JNIEXPORT jlong JNICALL
105Java_java_io_RandomAccessFile_length(JNIEnv *env, jobject this) {
106 FD fd;
107 jlong cur = jlong_zero;
108 jlong end = jlong_zero;
109
110 fd = GET_FD(this, raf_fd);
111 if (fd == -1) {
112 JNU_ThrowIOException(env, "Stream Closed");
113 return -1;
114 }
115 if ((cur = IO_Lseek(fd, 0L, SEEK_CUR)) == -1) {
116 JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
117 } else if ((end = IO_Lseek(fd, 0L, SEEK_END)) == -1) {
118 JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
119 } else if (IO_Lseek(fd, cur, SEEK_SET) == -1) {
120 JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
121 }
122 return end;
123}
124
125JNIEXPORT void JNICALL
126Java_java_io_RandomAccessFile_seek(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}