1 21 22 27 28 package com.sun.mail.util; 29 30 import java.io.*; 31 32 40 41 public class QPEncoderStream extends FilterOutputStream { 42 private int count = 0; private int bytesPerLine; private boolean gotSpace = false; 45 private boolean gotCR = false; 46 47 54 public QPEncoderStream(OutputStream out, int bytesPerLine) { 55 super(out); 56 this.bytesPerLine = bytesPerLine - 1; 59 } 60 61 66 public QPEncoderStream(OutputStream out) { 67 this(out, 76); 68 } 69 70 80 public void write(byte[] b, int off, int len) throws IOException { 81 for (int i = 0; i < len; i++) 82 write(b[off + i]); 83 } 84 85 90 public void write(byte[] b) throws IOException { 91 write(b, 0, b.length); 92 } 93 94 99 public void write(int c) throws IOException { 100 c = c & 0xff; if (gotSpace) { if (c == '\r' || c == '\n') 103 output(' ', true); 105 else output(' ', false); 107 gotSpace = false; 108 } 109 110 if (c == '\r') { 111 gotCR = true; 112 outputCRLF(); 113 } else { 114 if (c == '\n') { 115 if (gotCR) 116 ; 119 else 120 outputCRLF(); 121 } else if (c == ' ') { 122 gotSpace = true; 123 } else if (c < 040 || c >= 0177 || c == '=') 124 output(c, true); 126 else output(c, false); 128 gotCR = false; 130 } 131 } 132 133 138 public void flush() throws IOException { 139 out.flush(); 140 } 141 142 146 public void close() throws IOException { 147 out.close(); 148 } 149 150 private void outputCRLF() throws IOException { 151 out.write('\r'); 152 out.write('\n'); 153 count = 0; 154 } 155 156 private final static char hex[] = { 158 '0','1', '2', '3', '4', '5', '6', '7', 159 '8','9', 'A', 'B', 'C', 'D', 'E', 'F' 160 }; 161 162 protected void output(int c, boolean encode) throws IOException { 163 if (encode) { 164 if ((count += 3) > bytesPerLine) { 165 out.write('='); 166 out.write('\r'); 167 out.write('\n'); 168 count = 3; } 170 out.write('='); 171 out.write(hex[c >> 4]); 172 out.write(hex[c & 0xf]); 173 } else { 174 if (++count > bytesPerLine) { 175 out.write('='); 176 out.write('\r'); 177 out.write('\n'); 178 count = 1; } 180 out.write(c); 181 } 182 } 183 184 195 } 196 | Popular Tags |