| 1 19 20 package gnu.mail.util; 21 22 import java.io.*; 23 24 29 public class QPInputStream 30 extends FilterInputStream 31 { 32 33 protected byte[] buf; 34 35 38 protected int spaceCount; 39 40 private static final int LF = 10; 41 private static final int CR = 13; 42 private static final int SPACE = 32; 43 private static final int EQ = 61; 44 45 49 public QPInputStream(InputStream in) 50 { 51 super(new PushbackInputStream(in, 2)); 52 buf = new byte[2]; 53 } 54 55 58 public int read() 59 throws IOException 60 { 61 if (spaceCount>0) 62 { 63 spaceCount--; 64 return SPACE; 65 } 66 67 int c = in.read(); 68 if (c==SPACE) 69 { 70 while ((c = in.read())==SPACE) 71 spaceCount++; 72 if (c==LF || c==CR || c==-1) 73 spaceCount = 0; 74 else 75 { 76 ((PushbackInputStream)in).unread(c); 77 c = SPACE; 78 } 79 return c; 80 } 81 if (c==EQ) 82 { 83 int c2 = super.in.read(); 84 if (c2==LF) 85 return read(); 86 if (c2==CR) 87 { 88 int peek = in.read(); 89 if (peek!=LF) 90 ((PushbackInputStream)in).unread(peek); 91 return read(); 92 } 93 if (c2==-1) 94 return c2; 95 96 buf[0] = (byte)c2; 97 buf[1] = (byte)in.read(); 98 try 99 { 100 return Integer.parseInt(new String (buf, 0, 2), 16); 101 } 102 catch (NumberFormatException e) 103 { 104 ((PushbackInputStream)in).unread(buf); 105 } 106 return c; 107 } 108 else 109 return c; 110 } 111 112 115 public int read(byte[] bytes, int off, int len) 116 throws IOException 117 { 118 int pos = 0; 119 try 120 { 121 while (pos<len) 122 { 123 int c = read(); 124 if (c==-1) 125 { 126 if (pos==0) 127 pos = -1; 128 break; 129 } 130 bytes[off+pos] = (byte)c; 131 pos++; 132 } 133 134 } 135 catch (IOException e) 136 { 137 pos = -1; 138 } 139 return pos; 140 } 141 142 145 public boolean markSupported() 146 { 147 return false; 148 } 149 150 153 public int available() 154 throws IOException 155 { 156 return in.available(); 157 } 158 159 } 160 | Popular Tags |