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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
|
/*
* QPOutputStream.java
* Copyright (C) 2000 Andrew Selkirk
*
* This file is part of GNU JavaMail, a library.
*
* GNU JavaMail is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* GNU JavaMail 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* As a special exception, if you link this library with other files to
* produce an executable, this library does not by itself cause the
* resulting executable to be covered by the GNU General Public License.
* This exception does not however invalidate any other reasons why the
* executable file might be covered by the GNU General Public License.
*/
package gnu.mail.util;
// Imports
import java.io.OutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
/**
* Quoted Printable Encoding stream.
*
* @author Andrew Selkirk
* @author <a href="mailto:dog@gnu.org">Chris Burdess</a>
* @version 1.0
* @see java.io.FilterOutputStream
**/
public class QPOutputStream
extends FilterOutputStream
{
/**
* Char array used in decimal to hexidecimal conversion.
*/
private static final char[] hex = {'0','1','2','3','4','5','6',
'7','8','9','A','B','C','D',
'E','F'};
/**
* Current byte position in output.
*/
private int count;
/**
* Number of bytes per line.
*/
private int bytesPerLine;
/**
* Flag when a space is seen.
*/
private boolean gotSpace;
/**
* Flag when a CR is seen.
*/
private boolean gotCR;
//-------------------------------------------------------------
// Initialization ---------------------------------------------
//-------------------------------------------------------------
/**
* Create a new Quoted Printable Encoding stream.
* @param stream Output stream
* @param length Number of bytes per line
*/
public QPOutputStream(OutputStream stream, int length)
{
super(stream);
this.bytesPerLine = length;
this.count = 0;
this.gotSpace = false;
this.gotCR = false;
} // QPEncoderStream()
/**
* Create a new Quoted Printable Encoding stream with
* the default 76 bytes per line.
* @param stream Output stream
*/
public QPOutputStream(OutputStream stream)
{
this(stream, 76);
} // QPEWncoderStream()
//-------------------------------------------------------------
// Methods ----------------------------------------------------
//-------------------------------------------------------------
/**
* Flush encoding buffer.
* @exception IOException IO Exception occurred
*/
public void flush() throws IOException
{
if (gotSpace)
{
output(0x20, false);
gotSpace = false;
}
out.flush();
} // flush()
/**
* Write bytes to encoding stream.
* @param bytes Byte array to read values from
* @param offset Offset to start reading bytes from
* @param length Number of bytes to read
* @exception IOException IO Exception occurred
*/
public void write(byte[] bytes, int offset, int length)
throws IOException
{
// Variables
int index;
// Process Each Byte
for (index = offset; index < length; index++)
{
write(bytes[index]);
} // for
} // write()
/**
* Write bytes to stream.
* @param bytes Byte array to write to stream
* @exception IOException IO Exception occurred
*/
public void write(byte[] bytes) throws IOException
{
write(bytes, 0, bytes.length);
} // write()
/**
* Write a byte to the stream.
* @param b Byte to write to the stream
* @exception IOException IO Exception occurred
*/
public void write(int b) throws IOException
{
b &= 0xff;
if (gotSpace)
{
if (b=='\n' || b=='\r')
output(' ', true);
else
output(' ', false);
gotSpace = false;
}
if (b==' ')
gotSpace = true;
else if (b=='\r')
{
gotCR = true;
outputCRLF();
}
else if (b=='\n')
{
if (gotCR)
gotCR = false;
else
outputCRLF();
}
else
{
if (b<' ' || b>=127 || b=='=')
output(b, true);
else
output(b, false);
}
} // write()
/**
* Close stream.
* @exception IOException IO Exception occurred
*/
public void close() throws IOException
{
out.close();
} // close()
/**
* ????
* @param b ??
* @param value ??
* @exception IOException IO Exception occurred
*/
protected void output(int b, boolean value)
throws IOException
{
if (value)
{
if ((count += 3) > bytesPerLine)
{
out.write('=');
out.write('\r');
out.write('\n');
count = 3;
}
out.write('=');
out.write(hex[b >> 4]);
out.write(hex[b & 0xf]);
}
else
{
if (++count > bytesPerLine)
{
out.write('=');
out.write('\r');
out.write('\n');
count = 1;
}
out.write(b);
}
} // output()
/**
* Write CRLF byte series to stream.
* @exception IOException IO Exception occurred
*/
private void outputCRLF() throws IOException
{
out.write('\r');
out.write('\n');
count = 0;
} // outputCRLF()
} // QPEncoderStream
|