1 22 23 24 package net.sourceforge.groboutils.util.io.v1; 25 26 import java.io.FilterOutputStream ; 27 import java.io.OutputStream ; 28 import java.io.IOException ; 29 import java.awt.TextComponent ; 30 31 45 public class MimeOutputStream extends FilterOutputStream 46 { 47 private int bits = 0, spare = 0; 48 49 52 private static final int[] charset = { 53 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R', 54 'S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j', 55 'k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1', 56 '2','3','4','5','6','7','8','9','+','/' }; 57 private static final int pad = '='; 58 59 60 public MimeOutputStream( OutputStream o ) 61 { super(o); } 62 63 70 public void write(int c) throws IOException 71 { 72 int s, t; 73 switch (bits) 74 { 75 case 0: bits++; 76 s = (c >> 2) & 0x3f; 77 spare = c & 0x03; 78 out.write( charset[s] ); 79 break; 80 case 1: bits++; 81 s = (spare << 4) | ((c >> 4) & 0x0f); 82 spare = c & 0x0f; 83 out.write( charset[s] ); 84 break; 85 case 2: bits = 0; 86 s = (spare << 2) | ((c >> 6) & 0x03); 87 t = c & 0x3f; 88 out.write( charset[s] ); 89 out.write( charset[t] ); 90 break; 91 } 92 } 93 94 95 107 public void flush() throws IOException 108 { 109 switch (bits) 110 { 111 case 1: 112 out.write( charset[spare << 4] ); 113 out.write( pad ); 114 out.write( pad ); 115 break; 116 case 2: 117 out.write( charset[spare << 2] ); 118 out.write( pad ); 119 break; 120 } 121 super.flush(); 122 } 123 } 124 | Popular Tags |