Working towards better make dist
[cacao.git] / nat / FileOutputStream.c
1 /* class: java/io/FileOutputStream */
2
3 /*
4  * Class:     java/io/FileOutputStream
5  * Method:    close
6  * Signature: ()V
7  */
8 JNIEXPORT void JNICALL Java_java_io_FileOutputStream_close ( JNIEnv *env ,  struct java_io_FileOutputStream* this)
9 {
10         if (this->fd->fd == fileno(stderr))  /* don't close stderr!!! -- phil. */
11                 return;
12
13         if (this->fd->fd == fileno(stdout))
14                 return;
15
16         if (this->fd->fd >= 0) {
17                 s4 r = close (this->fd->fd);
18                 this->fd->fd = -1;
19                 if (r<0) 
20                         exceptionptr = native_new_and_init (class_java_io_IOException);
21                 }
22 }
23
24 /*
25  * Class:     java/io/FileOutputStream
26  * Method:    initIDs
27  * Signature: ()V
28  */
29 JNIEXPORT void JNICALL Java_java_io_FileOutputStream_initIDs ( JNIEnv *env  )
30 {
31     /* no IDs required */
32 }
33
34 /*
35  * Class:     java/io/FileOutputStream
36  * Method:    open
37  * Signature: (Ljava/lang/String;)V
38  */
39 JNIEXPORT void JNICALL Java_java_io_FileOutputStream_open ( JNIEnv *env ,  struct java_io_FileOutputStream* this, struct java_lang_String* name)
40 {
41         s4 fd;
42         char *fname = javastring_tochar ((java_objectheader*)name);
43         if (!fname) goto fail;
44         
45         fd = creat (fname, 0666);
46         if (fd<0) goto fail;
47         
48         threadedFileDescriptor(fd);
49
50         this->fd->fd = fd;
51         return;
52
53         fail:
54                 exceptionptr = native_new_and_init (class_java_io_IOException);
55                 return;
56 }
57
58 /*
59  * Class:     java/io/FileOutputStream
60  * Method:    openAppend
61  * Signature: (Ljava/lang/String;)V
62  */
63 JNIEXPORT void JNICALL Java_java_io_FileOutputStream_openAppend ( JNIEnv *env ,  struct java_io_FileOutputStream* this, struct java_lang_String* name)
64 {
65         s4 fd;
66         char *fname = javastring_tochar ((java_objectheader*)name);
67         if (!fname) goto fail;
68         
69         fd = open (fname, O_APPEND, 0666);
70         if (fd<0) goto fail;
71         
72         threadedFileDescriptor(fd);
73
74         this->fd->fd = fd;
75         return;
76
77         fail:
78                 exceptionptr = native_new_and_init (class_java_io_IOException);
79                 return;
80 }
81
82 /*
83  * Class:     java/io/FileOutputStream
84  * Method:    write
85  * Signature: (I)V
86  */
87 JNIEXPORT void JNICALL Java_java_io_FileOutputStream_write ( JNIEnv *env ,  struct java_io_FileOutputStream* this, s4 byte)
88 {
89         u1 buffer[1];
90         s4 l;
91
92         buffer[0] = byte;
93         l = threadedWrite (this->fd->fd, (char *) buffer, 1);
94
95         if (l<1) {
96                 exceptionptr = native_new_and_init (class_java_io_IOException);
97                 }
98 }
99
100 /*
101  * Class:     java/io/FileOutputStream
102  * Method:    writeBytes
103  * Signature: ([BII)V
104  */
105 JNIEXPORT void JNICALL Java_java_io_FileOutputStream_writeBytes ( JNIEnv *env ,  struct java_io_FileOutputStream* this, java_bytearray* buffer, s4 start, s4 len)
106 {
107         s4 o;
108
109         if (len == 0)
110                 return;
111         o = threadedWrite (this->fd->fd, (char *) buffer->data+start, len);
112         if (o!=len) 
113                 exceptionptr = native_new_and_init (class_java_io_IOException);
114 }
115
116
117