2 |
26 Feb 07 |
jari |
1 |
/* |
2 |
26 Feb 07 |
jari |
Copyright @ 1999-2003, The Institute for Genomic Research (TIGR). |
2 |
26 Feb 07 |
jari |
All rights reserved. |
2 |
26 Feb 07 |
jari |
4 |
*/ |
2 |
26 Feb 07 |
jari |
5 |
/* |
2 |
26 Feb 07 |
jari |
* $RCSfile: BreakFilterStream.java,v $ |
2 |
26 Feb 07 |
jari |
* $Revision: 1.4 $ |
2 |
26 Feb 07 |
jari |
* $Date: 2006/02/23 21:00:02 $ |
2 |
26 Feb 07 |
jari |
* $Author: caliente $ |
2 |
26 Feb 07 |
jari |
* $State: Exp $ |
2 |
26 Feb 07 |
jari |
11 |
*/ |
2 |
26 Feb 07 |
jari |
12 |
package org.tigr.remote.protocol.serializer; |
2 |
26 Feb 07 |
jari |
13 |
|
2 |
26 Feb 07 |
jari |
14 |
import java.io.IOException; |
2 |
26 Feb 07 |
jari |
15 |
import java.io.OutputStream; |
2 |
26 Feb 07 |
jari |
16 |
|
2 |
26 Feb 07 |
jari |
17 |
import de.mnl.java.io.base64.Base64OutputStream; |
2 |
26 Feb 07 |
jari |
18 |
|
2 |
26 Feb 07 |
jari |
19 |
public class BreakFilterStream extends OutputStream { |
2 |
26 Feb 07 |
jari |
20 |
|
2 |
26 Feb 07 |
jari |
21 |
private final static int BREAK_AFTER = 57 * 1024 * 10; // break after 540K of source data |
2 |
26 Feb 07 |
jari |
22 |
|
2 |
26 Feb 07 |
jari |
23 |
/** |
2 |
26 Feb 07 |
jari |
* Constructs a <code>BreakFilterStream</code> for specified output stream. |
2 |
26 Feb 07 |
jari |
25 |
*/ |
2 |
26 Feb 07 |
jari |
26 |
public BreakFilterStream( OutputStream out ) { |
2 |
26 Feb 07 |
jari |
27 |
m_out = out; |
2 |
26 Feb 07 |
jari |
28 |
m_buffer = new byte[BREAK_AFTER]; |
2 |
26 Feb 07 |
jari |
29 |
} |
2 |
26 Feb 07 |
jari |
30 |
|
2 |
26 Feb 07 |
jari |
31 |
/** |
2 |
26 Feb 07 |
jari |
* Writes an integer into the wrapped output stream. |
2 |
26 Feb 07 |
jari |
33 |
*/ |
2 |
26 Feb 07 |
jari |
34 |
public void write(int b) throws IOException { |
2 |
26 Feb 07 |
jari |
35 |
m_buffer[m_counter++] = (byte)(b & 0xff); |
2 |
26 Feb 07 |
jari |
36 |
if (m_counter == BREAK_AFTER) |
2 |
26 Feb 07 |
jari |
37 |
doBreak(); |
2 |
26 Feb 07 |
jari |
38 |
} |
2 |
26 Feb 07 |
jari |
39 |
|
2 |
26 Feb 07 |
jari |
40 |
/** |
2 |
26 Feb 07 |
jari |
* This method is to write all buffered data to its destination. |
2 |
26 Feb 07 |
jari |
42 |
*/ |
2 |
26 Feb 07 |
jari |
43 |
public void flush() throws IOException { |
2 |
26 Feb 07 |
jari |
44 |
internalFlush(); |
2 |
26 Feb 07 |
jari |
45 |
} |
2 |
26 Feb 07 |
jari |
46 |
|
2 |
26 Feb 07 |
jari |
47 |
/** |
2 |
26 Feb 07 |
jari |
* Writes the break xml command to wrapped output stream. |
2 |
26 Feb 07 |
jari |
49 |
*/ |
2 |
26 Feb 07 |
jari |
50 |
private void doBreak() throws IOException { |
2 |
26 Feb 07 |
jari |
51 |
internalFlush(); |
2 |
26 Feb 07 |
jari |
52 |
m_out.write( s_break ); |
2 |
26 Feb 07 |
jari |
53 |
} |
2 |
26 Feb 07 |
jari |
54 |
|
2 |
26 Feb 07 |
jari |
55 |
/** |
2 |
26 Feb 07 |
jari |
* Writes all base64-encoded buffered data into wrapped output stream. |
2 |
26 Feb 07 |
jari |
57 |
*/ |
2 |
26 Feb 07 |
jari |
58 |
private void internalFlush() throws IOException { |
2 |
26 Feb 07 |
jari |
59 |
Base64OutputStream o = new Base64OutputStream(m_out, false); |
2 |
26 Feb 07 |
jari |
60 |
o.write(m_buffer, 0, m_counter); |
2 |
26 Feb 07 |
jari |
61 |
m_counter = 0; |
2 |
26 Feb 07 |
jari |
62 |
o.flush(); |
2 |
26 Feb 07 |
jari |
63 |
o = null; |
2 |
26 Feb 07 |
jari |
64 |
} |
2 |
26 Feb 07 |
jari |
65 |
|
2 |
26 Feb 07 |
jari |
66 |
private OutputStream m_out; |
2 |
26 Feb 07 |
jari |
67 |
private int m_counter = 0; |
2 |
26 Feb 07 |
jari |
68 |
private byte[] m_buffer; |
2 |
26 Feb 07 |
jari |
69 |
private static final byte[] s_break = "<?break?>\r\n".getBytes(); |
2 |
26 Feb 07 |
jari |
70 |
} |