1"======================================================================
2|
3|   Java run-time support.  java.io.FileDescriptor native methods.
4|
5|
6 ======================================================================"
7
8
9"======================================================================
10|
11| Copyright 2003 Free Software Foundation, Inc.
12| Written by Paolo Bonzini.
13|
14| This file is part of GNU Smalltalk.
15|
16| The GNU Smalltalk class library is free software; you can redistribute it
17| and/or modify it under the terms of the GNU General Public License
18| as published by the Free Software Foundation; either version 2, or (at
19| your option) any later version.
20|
21| The GNU Smalltalk class library is distributed in the hope that it will be
22| useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
23| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
24| Public License for more details.
25|
26| You should have received a copy of the GNU Lesser General Public License
27| along with the GNU Smalltalk class library; see the file COPYING.  If not,
28| write to the Free Software Foundation, 51 Franklin Street, Fifth Floor,
29| Boston, MA 02110-1301, USA.
30|
31 ======================================================================"
32
33
34!JavaVM methodsFor: 'java.io.FileDescriptor'!
35
36convertToSmalltalkFD
37    <javaNativeMethod: #asFileDescriptor
38        for: #{Java.java.io.FileDescriptor} static: false>
39    ^JavaVM fileDescriptorFor: self fd!
40
41java_io_FileDescriptor_init
42    <javaNativeMethod: #'init()V'
43        for: #{Java.java.io.FileDescriptor} static: true>
44    self in: self new.
45    self in perform: #'<init>(I)V' with: 0.
46    self out: self new.
47    self out perform: #'<init>(I)V' with: 1.
48    self err: self new.
49    self err perform: #'<init>(I)V' with: 2.
50!
51
52java_io_FileDescriptor_sync
53    <javaNativeMethod: #'sync()V'
54        for: #{Java.java.io.FileDescriptor} static: false>
55    "TODO: use fsync ..."
56!
57
58java_io_FileDescriptor_valid
59    <javaNativeMethod: #'valid()Z'
60        for: #{Java.java.io.FileDescriptor} static: false>
61    ^self fd >= 0 "TODO: check with fstat ..."
62!
63
64java_io_FileDescriptor_open_java_lang_String: arg1 int: arg2
65    | mode |
66    <javaNativeMethod: #'open(Ljava/lang/String;I)I'
67        for: #{Java.java.io.FileDescriptor} static: false>
68    mode := #('w' 'r' 'w' 'w+' 'a' 'a+' 'a' 'a+')
69        at: arg2 \\ 8 + 1.
70
71    ^JavaFileDescriptor fopen: arg1 mode: mode ifFail: [
72        | exception msg errno |
73        errno := File errno.
74        errno < 1 ifTrue: [ ^0 ].
75        msg := (self stringError: errno) asJavaString.
76        exception := Java.java.io.FileNotFoundException new.
77        exception perform: #'<init>(Ljava/lang/String;)V' with: msg.
78        exception throw ]
79!
80
81java_io_FileDescriptor_write_int: arg1
82    <javaNativeMethod: #'write(I)V'
83        for: #{Java.java.io.FileDescriptor} static: false>
84    ^self asFileDescriptor
85	write: (ByteArray with: arg1) from: 1 to: 1
86!
87
88java_io_FileDescriptor_write_byteArray: arg1 int: arg2 int: arg3
89    | array |
90    <javaNativeMethod: #'write([BII)V'
91        for: #{Java.java.io.FileDescriptor} static: false>
92    array := ByteArray new: arg3.
93    array replaceFrom: 1 to: arg3 with: arg1 startingAt: arg2 + 1.
94    ^self asFileDescriptor write: array from: 1 to: arg3
95!
96
97java_io_FileDescriptor_close
98    <javaNativeMethod: #'close()V'
99        for: #{Java.java.io.FileDescriptor} static: false>
100    self asFileDescriptor close
101!
102
103java_io_FileDescriptor_setLength_long: arg1
104    | delta fd position |
105    <javaNativeMethod: #'setLength(J)V'
106        for: #{Java.java.io.FileDescriptor} static: false>
107    fd := self asFileDescriptor.
108    delta := fd size - arg1.
109    delta = 0 ifTrue: [ ^self ].
110    delta < 0 ifTrue: [ fd position: arg1; truncate. ^self ].
111
112    "If the file is too short, we extend it.  We can't rely on
113     ftruncate() extending the file.  So we lseek() to 1 byte less
114     than we want, and then we write a single byte at the end."
115    position := fd position.
116    fd position: arg1 - 1.
117    fd write: #[0].
118    fd position: position
119!
120
121java_io_FileDescriptor_seek_long: arg1 int: arg2 boolean: arg3
122    | pos fd |
123    <javaNativeMethod: #'seek(JIZ)I'
124        for: #{Java.java.io.FileDescriptor} static: false>
125    fd := self asFileDescriptor.
126    pos := arg1.
127    arg2 = 0 ifFalse: [ pos := pos + fd position ].
128    arg3 = 1 ifTrue: [ pos := pos min: fd size  ].
129    fd position: pos.
130    ^pos
131!
132
133java_io_FileDescriptor_getLength
134    <javaNativeMethod: #'getLength()J'
135        for: #{Java.java.io.FileDescriptor} static: false>
136    ^self asFileDescriptor size
137!
138
139java_io_FileDescriptor_getFilePointer
140    <javaNativeMethod: #'getFilePointer()J'
141        for: #{Java.java.io.FileDescriptor} static: false>
142    ^self asFileDescriptor position
143!
144
145java_io_FileDescriptor_read
146    <javaNativeMethod: #'read()I'
147        for: #{Java.java.io.FileDescriptor} static: false>
148    ^self asFileDescriptor next value
149!
150
151java_io_FileDescriptor_read_byteArray: arg1 int: arg2 int: arg3
152    | array count |
153    <javaNativeMethod: #'read([BII)I'
154        for: #{Java.java.io.FileDescriptor} static: false>
155    array := ByteArray new: arg3.
156    count := self asFileDescriptor read: array from: 1 to: arg3.
157    arg1 replaceFrom: arg1 + 1 to: arg1 + count with: array startingAt: 1.
158    ^count
159!
160
161java_io_FileDescriptor_available
162    <javaNativeMethod: #'available()I'
163        for: #{Java.java.io.FileDescriptor} static: false>
164    ^self asFileDescriptor canRead
165	ifTrue: [ 1 ]
166	ifFalse: [ 0 ]
167! !
168
169