1 /* java_nio.c - Native methods for gnu.java.nio.FileChannelImpl class
2 Copyright (C) 2002 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 /* do not move; needed here because of some macro definitions */
48 #include "target_native.h"
49 #ifndef WITHOUT_NETWORK
50 #include "target_native_network.h"
51 #endif /* WITHOUT_NETWORK */
53 #include "java_nio_channels_FileChannelImpl.h"
57 #define NIO_DEBUG(X) /* no debug */
58 //#define NIO_DEBUG(X) X
60 /***************************************
62 * File Channel implementation
68 compare (int i
, int lim
, char *buffer
)
70 sprintf (buffer
, "(%d >= %d)", i
, lim
);
77 unsigned char *a
= (unsigned char *) &X
;
79 (((int) a
[0]) << 24) +
80 (((int) a
[1]) << 16) + (((int) a
[2]) << 8) + (((int) a
[3]) << 0);
85 convert_Long (jlong X
)
87 unsigned char *a
= (unsigned char *) &X
;
89 (((int) a
[0]) << 24) +
90 (((int) a
[1]) << 16) + (((int) a
[2]) << 8) + (((int) a
[3]) << 0);
94 (((int) a
[0]) << 24) +
95 (((int) a
[1]) << 16) + (((int) a
[2]) << 8) + (((int) a
[3]) << 0);
96 return ((jlong
) res1
) | ((jlong
) res2
) << 32LL;
100 convert_Short (short X
)
102 unsigned char *a
= (unsigned char *) &X
;
103 int res
= (((int) a
[2]) << 8) + (((int) a
[3]) << 0);
107 convert_Char (short X
)
109 unsigned char *a
= (unsigned char *) &X
;
110 int res
= (((int) a
[2]) << 8) + (((int) a
[3]) << 0);
114 static inline unsigned char
115 convert_Byte (unsigned char X
)
121 convert_Float (float X
)
127 convert_Double (double X
)
133 // Note: do to many get()'s on a buffer and you should throw a BufferUnderflowException
134 // Alas, I only found this out during testing....
136 #define READ_WRITE_MMAPED_FILE(TYPE,ELT) \
138 ELT Java_gnu_java_nio_MappedByteFileBuffer_nio_1read_1 ## TYPE ## _1file_1channel(JNIEnv *env, \
139 jclass c, jobject b, \
140 int index, int limit, jlong jaddress) \
142 char *address = *(void **) &jaddress; char buffer[128]; \
143 if (index >= limit) JCL_ThrowException(env, "java/nio/BufferUnderflowException", compare(index,limit, buffer)); \
144 NIO_DEBUG( fprintf(stderr, "READ:index = %d [0]=%c [1]=%c\n", index, address[0],address[1]); ) \
146 return convert_ ## TYPE (*(ELT *) address); \
149 void Java_gnu_java_nio_MappedByteFileBuffer_nio_1write_1 ## TYPE ## _1file_1channel(JNIEnv *env, \
150 jclass c, jobject b, \
151 int index, int limit, ELT value, jlong jaddress) \
154 char *address = *(void **) &jaddress; char buffer[128]; \
155 if (index >= limit) JCL_ThrowException(env, "java/nio/BufferUnderflowException", compare(index,limit, buffer)); \
156 NIO_DEBUG( fprintf(stderr, "WRITE:index = %d [0]=%c [1]=%c\n", index, address[0],address[1]); ) \
158 *(ELT *) address = value; \
161 ELT Java_gnu_java_nio_MappedByteFileBuffer_nio_1get_1 ## TYPE(JNIEnv *env, jclass c, jobject b, \
162 int index, int limit, jlong jaddress) \
164 fprintf(stderr, "unimplemented\n"); return 0; \
167 void Java_gnu_java_nio_MappedByteFileBuffer_nio_1put_1 ## TYPE(JNIEnv *env, jclass c, jobject b, \
168 int index, int limit, \
169 ELT value, jlong jaddress) \
171 fprintf(stderr, "unimplemented\n"); \
174 READ_WRITE_MMAPED_FILE (Byte
, u_int8_t
);
175 READ_WRITE_MMAPED_FILE (Char
, u_int16_t
);
176 READ_WRITE_MMAPED_FILE (Short
, u_int16_t
);
177 READ_WRITE_MMAPED_FILE (Int
, u_int32_t
);
178 READ_WRITE_MMAPED_FILE (Long
, u_int64_t
);
179 READ_WRITE_MMAPED_FILE (Float
, float);
180 READ_WRITE_MMAPED_FILE (Double
, double);
183 nio_mmap_file (jint fd
, jlong pos
, jint size
, jint jflags
)
189 int flags
= (jflags
!= 2) ? MAP_SHARED
: MAP_PRIVATE
;
190 int prot
= PROT_READ
;
195 // fprintf(stderr, "mapping file: %d\n", fd);
197 address
= mmap (0, size
, prot
, flags
, fd
, pos
);
199 if (address
== (void *) -1)
201 perror ("mapping file failed");
205 // fprintf(stderr, "address = %p, fd = %d, pos=%lld, size=%d\n", address, fd, pos, size);
207 *(void **) &ret
= address
;
210 #else /* not HAVE_MMAP */
211 return (TARGET_NATIVE_MATH_INT_INT64_CONST_0
);
212 #endif /* not HAVE_MMAP */
217 nio_msync (int fd
, jlong jaddress
, int size
)
221 char *address
= *(void **) &jaddress
;
223 // fprintf(stderr, "synchronizing with file (%p -> %d bytes (%s))\n", address, size, address);
225 res
= msync (address
, size
, MS_SYNC
| MS_INVALIDATE
);
229 perror ("synchronize with file failed");
231 #else /* not HAVE_MMAP */
232 #endif /* not HAVE_MMAP */
236 nio_unmmap_file (int fd
, jlong jaddress
, int size
)
240 char *address
= *(void **) &jaddress
;
242 // nio_msync(fd, jaddress, size);
244 // fprintf(stderr, "unmapping (%p -> %d bytes)\n", address, size);
246 res
= munmap (address
, size
);
249 perror ("un-mapping file failed");
251 #else /* not HAVE_MMAP */
252 #endif /* not HAVE_MMAP */
256 /***************************************
258 * Socket Channel implementation
262 /*************************************************************************/
265 * Returns a 32 bit Internet address for the passed in InetAddress object
266 * Ronald: This is a verbatim copy from javanet.c.
267 * It's a copy to avoid a link error in orp.
271 socket_channel_get_net_addr (JNIEnv
* env
, jobject addr
)
273 #ifndef WITHOUT_NETWORK
281 ("socket_channel_get_net_addr(): Entered socket_channel_get_net_addr\n");
283 /* Call the getAddress method on the object to retrieve the IP address */
284 cls
= (*env
)->GetObjectClass (env
, addr
);
288 mid
= (*env
)->GetMethodID (env
, cls
, "getAddress", "()[B");
292 DBG ("socket_channel_get_net_addr(): Got getAddress method\n");
294 arr
= (*env
)->CallObjectMethod (env
, addr
, mid
);
298 DBG ("socket_channel_get_net_addr(): Got the address\n");
300 /* Turn the IP address into a 32 bit Internet address in network byte order */
301 len
= (*env
)->GetArrayLength (env
, arr
);
304 JCL_ThrowException (env
, "java/io/IOException",
305 "Internal Error: invalid byte array length");
308 DBG ("socket_channel_get_net_addr(): Length ok\n");
310 octets
= (*env
)->GetByteArrayElements (env
, arr
, 0);
314 DBG ("socket_channel_get_net_addr(): Grabbed bytes\n");
316 TARGET_NATIVE_NETWORK_IPADDRESS_BYTES_TO_INT (octets
[0],
321 (*env
)->ReleaseByteArrayElements (env
, arr
, octets
, 0);
322 DBG ("socket_channel_get_net_addr(): Done getting addr\n");
325 #else /* not WITHOUT_NETWORK */
327 #endif /* not WITHOUT_NETWORK */