001/* ---------------------------------------------------------------------------- 002 The Kiwi Toolkit - A Java Class Library 003 Copyright (C) 1998-2004 Mark A. Lindner 004 005 This library is free software; you can redistribute it and/or 006 modify it under the terms of the GNU General Public License as 007 published by the Free Software Foundation; either version 2 of the 008 License, or (at your option) any later version. 009 010 This library is distributed in the hope that it will be useful, 011 but WITHOUT ANY WARRANTY; without even the implied warranty of 012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 General Public License for more details. 014 015 You should have received a copy of the GNU General Public License 016 along with this library; if not, write to the Free Software 017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 018 02111-1307, USA. 019 020 The author may be contacted at: mark_a_lindner@yahoo.com 021 ---------------------------------------------------------------------------- 022 $Log: Radix64OutputStream.java,v $ 023 Revision 1.5 2004/05/05 21:36:35 markl 024 comment block updates 025 026 Revision 1.4 2003/01/19 09:37:12 markl 027 Javadoc & comment header updates. 028 029 Revision 1.3 2001/03/12 01:58:42 markl 030 Source code cleanup. 031 032 Revision 1.2 1999/01/10 03:34:00 markl 033 added GPL header & RCS tag 034 ---------------------------------------------------------------------------- 035*/ 036 037package kiwi.io; 038 039import java.io.*; 040 041import kiwi.text.Radix64Codec; 042 043/** This class implements a Radix-64 encoding filter. It accepts binary data 044 * written to it and writes the encoded form of the data to its output 045 * stream. To encode a file in Radix-64, one would do something like the 046 * following: 047 * <p> 048 * <pre> 049 * File f = new File("plain.txt"); 050 * Radix64OutputStream r64out = new Radix64OutputStream(System.out); 051 * FileInputStream fin = new FileInputStream(f); 052 * int b; 053 * while((b = f.read()) >= 0) 054 * r64out.write(b); 055 * </pre> 056 * <p> 057 * The encoded data in this case will be written to standard output. 058 * <p> 059 * This filter generates 64-column-wide output with a newline character after 060 * each line. 061 * 062 * @see java.io.FilterOutputStream 063 * 064 * @author Mark Lindner 065 */ 066 067public class Radix64OutputStream extends FilterOutputStream 068 { 069 private byte[] buf = new byte[3]; 070 private int c = 0, w = 16; 071 072 /** Construct a new <code>Radix64OutputStream</code> to filter the given 073 * output stream. 074 * 075 * @param out The <code>OutputStream</code> to filter. 076 */ 077 078 public Radix64OutputStream(OutputStream out) 079 { 080 super(out); 081 } 082 083 /** Write a byte to the output stream. 084 * 085 * @param b The byte to encode and write. 086 */ 087 088 public void write(int b) throws IOException 089 { 090 buf[c++] = (byte)b; 091 if(c == 3) 092 { 093 // encode this 3-byte sequence into a 4-char string & dump it out 094 095 out.write(Radix64Codec.encode(buf, c)); 096 c = 0; 097 if(--w == 0) 098 { 099 out.write('\n'); 100 w = 16; 101 } 102 } 103 } 104 105 /** Flush the output stream. Write any remaining data in the encode buffer 106 * to the output stream. 107 */ 108 109 public void flush() throws IOException 110 { 111 if(c > 0) 112 out.write(Radix64Codec.encode(buf, c)); 113 114 c = 0; 115 } 116 117 } 118 119/* end of source file */