1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
/*
* MimeOutputStream.java - A Filter stream for MIME encoding
*
* Copyright (C) 2000,,2003 2002 Matt Albrecht
* groboclown@users.sourceforge.net
* http://groboutils.sourceforge.net
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package net.sourceforge.groboutils.util.io.v1;
import java.io.FilterOutputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.awt.TextComponent;
/**
* java.io.FilterOutputStream implementation for Mime base 64. Not incredibly
* efficient, but it works and is small.
* <P>
* All we need to implement are:
* <ul>
* <li>write(int)</li>
* <li>flush()</li>
* </ul>
*
* @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
* @since 0.9.0 Alpha (early 2000)
* @version $Date: 2003/02/10 22:52:45 $
*/
public class MimeOutputStream extends FilterOutputStream
{
private int bits = 0, spare = 0;
/**
* Mime character set translation
*/
private static final int[] charset = {
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R',
'S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j',
'k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1',
'2','3','4','5','6','7','8','9','+','/' };
private static final int pad = '=';
/** Constructor! */
public MimeOutputStream( OutputStream o )
{ super(o); }
/**
* Write the specified <code>byte</code>, performing mime encoding.
*
* <p>Override this method, since all other write methods call it.
*
* @exception IOException If an I/O error occurs
*/
public void write(int c) throws IOException
{
int s, t;
switch (bits)
{
case 0: bits++;
s = (c >> 2) & 0x3f;
spare = c & 0x03;
out.write( charset[s] );
break;
case 1: bits++;
s = (spare << 4) | ((c >> 4) & 0x0f);
spare = c & 0x0f;
out.write( charset[s] );
break;
case 2: bits = 0;
s = (spare << 2) | ((c >> 6) & 0x03);
t = c & 0x3f;
out.write( charset[s] );
out.write( charset[t] );
break;
}
}
/**
* Flush the stream. If the stream has saved any characters from the
* various write() methods in a buffer, write them immediately to their
* intended destination. Then, if that destination is another character or
* byte stream, flush it. Thus one flush() invocation will flush all the
* buffers in a chain of Writers and OutputStreams.
* <P>
* This version does not buffer, but Mime encoding may have some trailing
* stuff, which needs padding.
*
* @exception IOException If an I/O error occurs
*/
public void flush() throws IOException
{
switch (bits)
{
case 1:
out.write( charset[spare << 4] );
out.write( pad );
out.write( pad );
break;
case 2:
out.write( charset[spare << 2] );
out.write( pad );
break;
}
super.flush();
}
}
|