! :folding=indent:collapseFolds=1: ! $Id$ ! ! Copyright (C) 2003, 2004 Slava Pestov. ! ! Redistribution and use in source and binary forms, with or without ! modification, are permitted provided that the following conditions are met: ! ! 1. Redistributions of source code must retain the above copyright notice, ! this list of conditions and the following disclaimer. ! ! 2. Redistributions in binary form must reproduce the above copyright notice, ! this list of conditions and the following disclaimer in the documentation ! and/or other materials provided with the distribution. ! ! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, ! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND ! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, ! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; ! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, ! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR ! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. IN: streams USE: combinators USE: errors USE: kernel USE: lists USE: logic USE: namespaces USE: regexp USE: stack USE: strings : fcopy ( from to -- ) ! Copy the contents of the byte-stream 'from' to the byte-stream 'to'. [ [ "in" get ] bind ] dip [ "out" get ] bind [ "java.io.InputStream" "java.io.OutputStream" ] "factor.FactorLib" "copy" jinvoke-static ; ! These are in separate words so that they can be compiled. ! Do not call them directly. : /freadln ( -- string ) "in" get [ "java.io.InputStream" ] "factor.FactorLib" "readLine" jinvoke-static ; : ( -- ex ) [ ] "java.io.EOFException" jnew ; : >char/eof ( ch -- ch ) dup -1 = [ throw ] [ >char ] ifte ; : /fread1 ( -- string ) "in" get [ ] "java.io.InputStream" "read" jinvoke >char/eof ; : /fread# ( count -- string ) "in" get [ "int" "java.io.InputStream" ] "factor.FactorLib" "readCount" jinvoke-static ; : /fwrite ( string -- ) dup char? [ "out" get [ "int" ] "java.io.OutputStream" "write" jinvoke ] [ >bytes "out" get [ [ "byte" ] ] "java.io.OutputStream" "write" jinvoke ] ifte ; : /fflush ( -- ) "out" get [ ] "java.io.OutputStream" "flush" jinvoke ; : /fclose ( -- ) "in" get [ [ ] "java.io.InputStream" "close" jinvoke ] when* "out" get [ [ ] "java.io.OutputStream" "close" jinvoke ] when* ; : ( in -- in ) [ "java.io.InputStream" ] "java.io.BufferedInputStream" jnew ; : ( out -- out ) [ "java.io.OutputStream" ] "java.io.BufferedOutputStream" jnew ; : ( in out -- stream ) #! Creates a new stream for reading from the #! java.io.InputStream in, and writing to the #! java.io.OutputStream out. The streams are wrapped in #! buffered streams. [ "out" set "in" set ( -- string ) [ /freadln ] "freadln" set ( count -- string ) [ /fread1 ] "fread1" set ( count -- string ) [ /fread# ] "fread#" set ( string -- ) [ /fwrite ] "fwrite" set ( -- ) [ /fflush ] "fflush" set ( -- ) [ /fclose ] "fclose" set ] extend ; : /freadln ( -- string ) "in" get [ ] "java.io.BufferedReader" "readLine" jinvoke ; : /fread1 ( -- string ) "in" get [ ] "java.io.Reader" "read" jinvoke >char/eof ; : /fread# ( -- string ) "in" get [ "int" "java.io.Reader" ] "factor.FactorLib" "readCount" jinvoke-static ; : /fwrite ( string -- ) "out" get [ "java.lang.String" ] "java.io.Writer" "write" jinvoke ; : /fflush ( -- ) "out" get [ ] "java.io.Writer" "flush" jinvoke ; : /fclose ( -- ) "in" get [ [ ] "java.io.Reader" "close" jinvoke ] when* "out" get [ [ ] "java.io.Writer" "close" jinvoke ] when* ; : ( in out -- stream ) #! Creates a new stream for reading from the #! java.io.BufferedReader in, and writing to the #! java.io.Reader out. [ "out" set "in" set ( -- string ) [ /freadln ] "freadln" set ( -- string ) [ /fread1 ] "fread1" set ( count -- string ) [ /fread# ] "fread#" set ( string -- ) [ /fwrite ] "fwrite" set ( -- ) [ /fflush ] "fflush" set ( -- ) [ /fclose ] "fclose" set ] extend ; : ( writer -- bwriter ) [ "java.io.Writer" ] "java.io.BufferedWriter" jnew ; : ( outputstream -- owriter ) [ "java.io.OutputStream" ] "java.io.OutputStreamWriter" jnew ; : ( path -- stream ) [ "java.lang.String" ] "java.io.FileReader" jnew f ; : ( path -- stream ) [ "java.lang.String" ] "java.io.FileWriter" jnew f swap ; : ( path -- stream ) [ "java.lang.String" ] "java.io.FileInputStream" jnew f ; : ( path -- stream ) [ "java.lang.String" ] "java.io.FileOutputStream" jnew f swap ; : ( path -- file ) dup "java.io.File" is not [ [ "java.lang.String" ] "java.io.File" jnew ] when ; : fdelete ( file -- ? ) #! Delete a file. [ ] "java.io.File" "delete" jinvoke ; : ( file -- freader ) [ "java.lang.String" ] "java.io.FileReader" jnew ; : exists? ( file -- boolean ) [ ] "java.io.File" "exists" jinvoke ; : directory? ( file -- boolean ) [ ] "java.io.File" "isDirectory" jinvoke ; : directory ( file -- listing ) [ ] "java.io.File" "list" jinvoke array>list str-sort ; : frename ( from to -- ? ) ! Rename file 'from' to 'to'. These can be paths or ! java.io.File instances. swap [ "java.io.File" ] "java.io.File" "renameTo" jinvoke ; : file-extension ( filename -- extension ) ".*\\.(.*)" group1 ; : ( string -- reader ) [ "java.lang.String" ] "java.io.StringReader" jnew ; : close ( stream -- ) dup "java.io.Reader" is [ [ ] "java.io.Reader" "close" jinvoke ] [ [ ] "java.io.Writer" "close" jinvoke ] ifte ; : ( port -- stream ) #! Starts listening on localhost:port. Returns a stream that #! you can close with fclose, and accept connections from #! with accept. No other stream operations are supported. [ "int" ] "java.net.ServerSocket" jnew [ "socket" set ( -- ) [ "socket" get [ ] "java.net.ServerSocket" "close" jinvoke ] "fclose" set ] extend ; : socket-closed? ( socket -- ? ) [ ] "java.net.Socket" "isClosed" jinvoke ; : close-socket ( socket -- ) [ ] "java.net.Socket" "close" jinvoke ; : ?close-socket ( socket -- ) dup socket-closed? [ drop ] [ close-socket ] ifte ; : ( socket -- stream ) #! Wraps a socket inside a byte-stream. dup [ [ ] "java.net.Socket" "getInputStream" jinvoke ] [ [ ] "java.net.Socket" "getOutputStream" jinvoke ] cleave [ dup >str "client" set "socket" set ! We "extend" byte-stream's fclose. ( -- ) "fclose" get [ "socket" get ?close-socket ] append "fclose" set ] extend ; : ( server port -- stream ) #! Open a TCP/IP socket to a port on the given server. [ "java.lang.String" "int" ] "java.net.Socket" jnew ; : accept ( server -- client ) #! Accept a connection from a server socket. [ "socket" get ] bind [ ] "java.net.ServerSocket" "accept" jinvoke ;