1 19 20 package gnu.mail.util; 21 22 import java.io.IOException ; 23 import java.io.OutputStream ; 24 25 54 public class QOutputStream 55 extends QPOutputStream 56 { 57 58 private static final int SPACE = 32; 59 private static final int UNDERSCORE = 95; 60 61 private static String TEXT_SPECIALS = "=_?"; 62 private static String WORD_SPECIALS = "=_?\"#$%&'(),.:;<>@[\\]^`{|}~"; 63 64 private String specials; 65 66 74 public QOutputStream(OutputStream out, boolean word) 75 { 76 super(out, 0x7fffffff); 77 specials = word ? WORD_SPECIALS : TEXT_SPECIALS; 78 } 79 80 83 public void write(int c) 84 throws IOException 85 { 86 c &= 0xff; 87 if (c==SPACE) 88 output(UNDERSCORE, false); 89 else 90 { 91 if (c<32 || c>=127 || specials.indexOf(c)>=0) 92 output(c, true); 93 else 94 output(c, false); 95 } 96 } 97 98 public static int encodedLength(byte[] bytes, boolean word) 99 { 100 int len = 0; 101 String specials = word ? WORD_SPECIALS : TEXT_SPECIALS; 102 for(int i = 0; i<bytes.length; i++) 103 { 104 int c = bytes[i]&0xff; 105 if (c<32 || c>=127 || specials.indexOf(c)>=0) 106 len += 3; 107 else 108 len++; 109 } 110 return len; 111 } 112 113 } 114 | Popular Tags |