1 22 23 24 package net.sourceforge.groboutils.util.io.v1; 25 26 import java.io.FilterInputStream ; 27 import java.io.InputStream ; 28 import java.io.IOException ; 29 30 44 public class MimeInputStream extends FilterInputStream 45 { 46 private int bits = 0, spare = 0; 47 48 51 private static final int[] charset = { 52 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R', 53 'S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j', 54 'k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1', 55 '2','3','4','5','6','7','8','9','+','/' }; 56 private static final int UPPER_START = 0; 57 private static final int LOWER_START = 26; 58 private static final int NUM_START = LOWER_START+26; 59 private static final int PLUS = NUM_START+10; 60 private static final int SLASH = PLUS+1; 61 private static final int pad = '='; 62 63 64 public MimeInputStream( InputStream i ) 65 { super(i); } 66 67 74 public int read() throws IOException 75 { 76 int s; 77 int c, val; 78 79 for (;;) 81 { 82 c = super.read(); 83 if (c < 0) return c; 85 if (c >= 'A' && c <= 'Z') { val = c - 'A' + UPPER_START; } 86 else if (c >= 'a' && c <= 'z') { val = c - 'a' + LOWER_START; } 87 else if (c >= '0' && c <= '9') { val = c - '0' + NUM_START; } 88 else if (c == '+') { val = PLUS; } 89 else if (c == '/') { val = SLASH; } 90 else if (c == pad) 91 { throw new IOException ("end-of-mime character encountered"); } 92 else continue; 94 switch (bits) 95 { 96 case 0: bits++; 97 spare = val << 2; 98 break; 100 case 1: bits++; 101 s = spare | ((val >> 4) & 0x03); 102 spare = (val << 4) & 0xF0; 103 return s; 104 case 2: bits++; 105 s = spare | ((val >> 2) & 0x0F); 106 spare = (val << 6) & 0xC0; 107 return s; 108 case 3: bits = 0; 109 return spare | val; 111 } 112 } 113 } 114 115 194 195 196 210 public long skip(long n) throws IOException 211 { 212 long p = n * 4; 213 if ((p % 3) != 0) { p /= 3; p++; } 214 else { p /= 3; } 215 p = in.skip(p); 216 p *= 3; 217 if ((p & 0x03) != 0) { p >>= 2; p++; } 218 else { p >>= 2; } 219 return p; 220 } 221 222 223 235 public int available() throws IOException 236 { 237 int p = in.available() * 3; 238 if ((p & 0x03) != 0) { p >>= 2; p++; } 239 else { p >>= 2; } 240 return p; 241 } 242 } 243 | Popular Tags |