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: Radix64InputStream.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 decoding filter. It reads Radix-64 encoded 044 * data from its input stream and outputs the original (decoded) form of the 045 * data. To decode a file encoded in Radix-64, one would do something like 046 * the following: 047 * <p> 048 * <pre> 049 * File f = new File("encoded.txt"); 050 * Radix64InputStream r64in = new Radix64InputStream(new FileInputStream(f)); 051 * </pre> 052 * <p> 053 * The decoded data may then be obtained by reading from <code>r64in</code>. 054 * <p> 055 * This filter disregards any non-Radix-64 characters in the input. Radix-64 056 * characters include '/', '+', '=', 'A' - 'Z', '0' - '9', and 'a' - 'z'. 057 * 058 * @see java.io.FilterInputStream 059 * 060 * @author Mark Lindner 061 */ 062 063public class Radix64InputStream extends FilterInputStream 064 { 065 private byte buf[] = new byte[3], bufx[] = new byte[4]; 066 private int c = -1; 067 068 /** Construct a new <code>Radix64InputStream</code> to filter the given 069 * input stream. 070 * 071 * @param in The <code>InputStream</code> to filter. 072 */ 073 074 public Radix64InputStream(InputStream in) 075 { 076 super(in); 077 } 078 079 /** Read a byte from the input stream. 080 * 081 * @return The next (decoded) byte. 082 */ 083 084 public int read() throws IOException 085 { 086 // if the decode buffer is empty, fill it 087 088 if(c < 0) 089 { 090 // read, but ignore non-radix-64 characters 091 092 int ct = 0; 093 for(;;) 094 { 095 byte br = (byte)in.read(); 096 if(br < 0) 097 return(br); 098 if(!Radix64Codec.isRadix64Character(br)) 099 continue; 100 101 bufx[ct] = br; 102 if(++ct == 4) 103 break; 104 } 105 106 // decode buffer 107 108 int p = Radix64Codec.decode(bufx, buf); 109 c = 0; 110 } 111 112 // return next byte from buffer 113 114 byte r = buf[c]; 115 if(++c == 3) 116 c = -1; 117 118 return((int)r); 119 } 120 121 } 122 123/* end of source file */