2 |
26 Feb 07 |
jari |
1 |
// |
2 |
26 Feb 07 |
jari |
// RFileOutputStream.java |
2 |
26 Feb 07 |
jari |
// Klimt |
2 |
26 Feb 07 |
jari |
4 |
// |
2 |
26 Feb 07 |
jari |
// Created by Simon Urbanek on Wed Oct 22 2003. |
2 |
26 Feb 07 |
jari |
// 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 |
/** <b>RFileOutputStream</b> is an {@link OutputStream} to transfer files |
2 |
26 Feb 07 |
jari |
from the client to <b>Rserve</b> server. It is used very much like |
2 |
26 Feb 07 |
jari |
a {@link FileOutputStream}. Currently mark and seek is not supported. |
2 |
26 Feb 07 |
jari |
The current implementation is also "one-shot" only, that means the file |
2 |
26 Feb 07 |
jari |
can be written only once. |
2 |
26 Feb 07 |
jari |
@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 |
/** Rtalk class to use for communication with the Rserve */ |
2 |
26 Feb 07 |
jari |
23 |
Rtalk rt; |
2 |
26 Feb 07 |
jari |
/** set to <code>true</code> when {@link #close} was called. |
2 |
26 Feb 07 |
jari |
Any subsequent read requests on closed stream result in an |
2 |
26 Feb 07 |
jari |
{@link IOException} or error result */ |
2 |
26 Feb 07 |
jari |
27 |
boolean closed; |
2 |
26 Feb 07 |
jari |
28 |
|
2 |
26 Feb 07 |
jari |
/** tries to create a file on the R server, using specified {@link Rtalk} object |
2 |
26 Feb 07 |
jari |
and filename. Be aware that the filename has to be specified in host |
2 |
26 Feb 07 |
jari |
format (which is usually unix). In general you should not use directories |
2 |
26 Feb 07 |
jari |
since Rserve provides an own directory for every connection. Future Rserve |
2 |
26 Feb 07 |
jari |
servers may even strip all directory navigation characters for security |
2 |
26 Feb 07 |
jari |
purposes. Therefore only filenames without path specification are considered |
2 |
26 Feb 07 |
jari |
valid, the behavior in respect to absolute paths in filenames is undefined. |
2 |
26 Feb 07 |
jari |
@param rti RTalk object for communication with Rserve |
2 |
26 Feb 07 |
jari |
@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 |
/** writes one byte to the file. This function should be avoided, since |
2 |
26 Feb 07 |
jari |
{@link RFileOutputStream} provides no buffering. This means that each |
2 |
26 Feb 07 |
jari |
call to this function leads to a complete packet exchange between |
2 |
26 Feb 07 |
jari |
the server and the client. Use {@link #write(byte[])} instead |
2 |
26 Feb 07 |
jari |
whenever possible. In fact this function calls <code>write(b,0,1)</code>. |
2 |
26 Feb 07 |
jari |
@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 |
/** 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 |
@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 |
/** Writes specified number of bytes to the remote file. |
2 |
26 Feb 07 |
jari |
@param b buffer containing the bytes to write |
2 |
26 Feb 07 |
jari |
@param off offset where to start |
2 |
26 Feb 07 |
jari |
@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 |
/** close stream - is not related to the actual Rconnection, calling |
2 |
26 Feb 07 |
jari |
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 |
/** 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 |
} |