mev-4.0.01/source/org/tigr/microarray/mev/r/RFileOutputStream.java

Code
Comments
Other
Rev Date Author Line
2 26 Feb 07 jari 1 //
2 26 Feb 07 jari 2 //  RFileOutputStream.java
2 26 Feb 07 jari 3 //  Klimt
2 26 Feb 07 jari 4 //
2 26 Feb 07 jari 5 //  Created by Simon Urbanek on Wed Oct 22 2003.
2 26 Feb 07 jari 6 //  Copyright (c) 2003 __MyCompanyName__. All rights reserved.
2 26 Feb 07 jari 7 //
2 26 Feb 07 jari 8
2 26 Feb 07 jari 9 package org.tigr.microarray.mev.r;
2 26 Feb 07 jari 10
2 26 Feb 07 jari 11 import java.io.*;
2 26 Feb 07 jari 12
2 26 Feb 07 jari 13 /** <b>RFileOutputStream</b> is an {@link OutputStream} to transfer files
2 26 Feb 07 jari 14 from the client to <b>Rserve</b> server. It is used very much like
2 26 Feb 07 jari 15 a {@link FileOutputStream}. Currently mark and seek is not supported.
2 26 Feb 07 jari 16 The current implementation is also "one-shot" only, that means the file
2 26 Feb 07 jari 17 can be written only once.
2 26 Feb 07 jari 18 @version $Id$
2 26 Feb 07 jari 19 */
2 26 Feb 07 jari 20
2 26 Feb 07 jari 21 public class RFileOutputStream extends OutputStream {
2 26 Feb 07 jari 22     /** Rtalk class to use for communication with the Rserve */
2 26 Feb 07 jari 23     Rtalk rt;
2 26 Feb 07 jari 24     /** set to <code>true</code> when {@link #close} was called.
2 26 Feb 07 jari 25     Any subsequent read requests on closed stream  result in an
2 26 Feb 07 jari 26 {@link IOException} or error result */
2 26 Feb 07 jari 27     boolean closed;
2 26 Feb 07 jari 28
2 26 Feb 07 jari 29     /** tries to create a file on the R server, using specified {@link Rtalk} object
2 26 Feb 07 jari 30         and filename. Be aware that the filename has to be specified in host
2 26 Feb 07 jari 31         format (which is usually unix). In general you should not use directories
2 26 Feb 07 jari 32         since Rserve provides an own directory for every connection. Future Rserve
2 26 Feb 07 jari 33         servers may even strip all directory navigation characters for security
2 26 Feb 07 jari 34         purposes. Therefore only filenames without path specification are considered
2 26 Feb 07 jari 35         valid, the behavior in respect to absolute paths in filenames is undefined.
2 26 Feb 07 jari 36         @param rti RTalk object for communication with Rserve
2 26 Feb 07 jari 37         @param fb filename of the file to create (existing file will be overwritten)
2 26 Feb 07 jari 38         */
2 26 Feb 07 jari 39     RFileOutputStream(Rtalk rti, String fn) throws IOException {
2 26 Feb 07 jari 40         rt=rti;
2 26 Feb 07 jari 41         Rpacket rp=rt.request(Rtalk.CMD_createFile,fn);
2 26 Feb 07 jari 42         if (rp==null || !rp.isOk())
2 26 Feb 07 jari 43             throw new IOException((rp==null)?"Connection to Rserve failed":("Request return code: "+rp.getStat()));
2 26 Feb 07 jari 44         closed=false;
2 26 Feb 07 jari 45     }
2 26 Feb 07 jari 46
2 26 Feb 07 jari 47     /** writes one byte to the file. This function should be avoided, since
2 26 Feb 07 jari 48     {@link RFileOutputStream} provides no buffering. This means that each
2 26 Feb 07 jari 49         call to this function leads to a complete packet exchange between
2 26 Feb 07 jari 50         the server and the client. Use {@link #write(byte[])} instead
2 26 Feb 07 jari 51         whenever possible. In fact this function calls <code>write(b,0,1)</code>.
2 26 Feb 07 jari 52         @param b byte to write
2 26 Feb 07 jari 53         */
2 26 Feb 07 jari 54     public void write(int b) throws IOException {
2 26 Feb 07 jari 55         byte[] ba=new byte[1];
2 26 Feb 07 jari 56         write(ba,0,1);
2 26 Feb 07 jari 57     }
2 26 Feb 07 jari 58
2 26 Feb 07 jari 59     /** writes the content of b into the file. This methods is equivalent to calling <code>write(b,0,b.length)</code>.
2 26 Feb 07 jari 60         @param b content to write
2 26 Feb 07 jari 61         */
2 26 Feb 07 jari 62     public void write(byte b[]) throws IOException {
2 26 Feb 07 jari 63         write(b,0,b.length);
2 26 Feb 07 jari 64     }
2 26 Feb 07 jari 65
2 26 Feb 07 jari 66     /** Writes specified number of bytes to the remote file.
2 26 Feb 07 jari 67         @param b buffer containing the bytes to write
2 26 Feb 07 jari 68         @param off offset where to start
2 26 Feb 07 jari 69         @param len number of bytes to write
2 26 Feb 07 jari 70         */
2 26 Feb 07 jari 71     public void write(byte[] b, int off, int len) throws IOException {
2 26 Feb 07 jari 72         if (closed) throw new IOException("File is not open");
2 26 Feb 07 jari 73         if (len<0) len=0;
2 26 Feb 07 jari 74         boolean isLarge=(len>0xfffff0);
2 26 Feb 07 jari 75         byte[] hdr=Rtalk.newHdr(Rtalk.DT_BYTESTREAM,len);
2 26 Feb 07 jari 76         Rpacket rp=rt.request(Rtalk.CMD_writeFile,hdr,b,off,len);
2 26 Feb 07 jari 77         if (rp==null || !rp.isOk())
2 26 Feb 07 jari 78             throw new IOException((rp==null)?"Connection to Rserve failed":("Request return code: "+rp.getStat()));
2 26 Feb 07 jari 79     }
2 26 Feb 07 jari 80
2 26 Feb 07 jari 81     /** close stream - is not related to the actual Rconnection, calling
2 26 Feb 07 jari 82         close does not close the Rconnection.
2 26 Feb 07 jari 83         */
2 26 Feb 07 jari 84     public void close() throws IOException {
2 26 Feb 07 jari 85         Rpacket rp=rt.request(Rtalk.CMD_closeFile,(byte[])null);
2 26 Feb 07 jari 86         if (rp==null || !rp.isOk())
2 26 Feb 07 jari 87             throw new IOException((rp==null)?"Connection to Rserve failed":("Request return code: "+rp.getStat()));
2 26 Feb 07 jari 88         closed=true;
2 26 Feb 07 jari 89     }
2 26 Feb 07 jari 90
2 26 Feb 07 jari 91     /** currently (Rserve 0.3) there is no way to force flush on the remote side, hence this function is noop. Future versions of Rserve may support this feature though. At any rate, it is safe to call it. */
2 26 Feb 07 jari 92     public void flush() {
2 26 Feb 07 jari 93     }
2 26 Feb 07 jari 94 }