📄 serveroutputstream.java
字号:
/* ---------------------------------------------------------------------- The SINUS Firewall -- a TCP/IP packet filter for Linux Written within the SINUS project at the University of Zurich, SWITCH, Telekurs Payserv AG, ETH Zurich. originally based on the sf Firewall Software (C) 1996 by Robert Muchsel and Roland Schmid. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. SINUS Firewall resources: SINUS Homepage: http://www.ifi.unizh.ch/ikm/SINUS/ Firewall Homepage: http://www.ifi.unizh.ch/ikm/SINUS/firewall.html Frequently asked questions: http://www.ifi.unizh.ch/ikm/SINUS/sf_faq.html Mailing list for comments, questions, bug reports: firewall@ifi.unizh.ch ---------------------------------------------------------------------- */package sfclasses;import java.io.*;import sfclasses.*;/** * This class implements an output stream to write data to a firewall system * via the relay. It uses the I/O functions implemented in the Communicator * class. Filter streams can be used on top of this stream to enhance * functionality. * @version 1.0 11 Dec 1996 * @author Roland E. Schmid */public class ServerOutputStream extends OutputStream { /** * Initialize the stream and open the file for writing * @param filename Name of the file including the full path */ public ServerOutputStream(byte[] h, String filename) { host = h; conn = Communicator.openServer(h); if (conn == null) { closed = true; return; } closed = !Communicator.writeFile(host, filename); } public void suspendPingThread() { conn.suspend(); } public void resumePingThread() { conn.resume(); } /** * Write one byte to the file. The output is buffered and will * be written when flush() or close() is called. * @param b Byte to write */ public void write(int b) throws IOException { if (closed) throw new IOException(); buffer[bufptr] = (byte)b; bufptr++; if (bufptr == bufsize) flush(); } /** * Write the buffer to the file. */ public void flush() throws IOException { if (closed) throw new IOException(); if (bufptr == 0) return; try { conn.writeHeader(Communicator.SERV_FILE_DATA, bufptr); conn.out.write(buffer, 0, bufptr); bufptr = 0; conn.readHeader(); if ((conn.readLength != 0) || (conn.readCommand != Communicator.SERV_OK)) throw new IOException(); } catch (Exception e) { throw new IOException(); } } /** * Close the file and the output stream. */ public void close() throws IOException { if (closed) return; if (bufptr > 0) flush(); conn.writeHeader(Communicator.SERV_FILE_DATA, 0); bufptr = 0; closed = true; try { // read OK for FILE_DATA conn.readHeader(); if ((conn.readLength != 0) || (conn.readCommand != Communicator.SERV_WRITE_OK)) throw new IOException(); // read OK for whole operation } catch (Exception e) { throw new IOException(); } } /** * Check if the output stream is closed. * @return true if the stream is closed and false if data can be written. */ public boolean isClosed() { return closed; } private static final int bufsize = 1000; private Connection conn; private byte[] host; private boolean closed; private byte[] buffer = new byte[bufsize]; private int bufptr = 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -