001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019 020 package org.apache.geronimo.mail.util; 021 022 import java.io.ByteArrayOutputStream; 023 import java.io.FilterInputStream; 024 import java.io.IOException; 025 import java.io.InputStream; 026 import java.io.UnsupportedEncodingException; 027 028 /** 029 * An implementation of a FilterOutputStream that decodes the 030 * stream data in Q-P encoding format. This version does the 031 * decoding "on the fly" rather than decoding a single block of 032 * data. Since this version is intended for use by the MimeUtilty class, 033 * it also handles line breaks in the encoded data. 034 * 035 * @version $Rev: 467553 $ $Date: 2006-10-25 06:01:51 +0200 (Mi, 25. Okt 2006) $ 036 */ 037 public class QuotedPrintableDecoderStream extends FilterInputStream { 038 // our decoder for processing the data 039 protected QuotedPrintableEncoder decoder; 040 041 042 /** 043 * Stream constructor. 044 * 045 * @param in The InputStream this stream is filtering. 046 */ 047 public QuotedPrintableDecoderStream(InputStream in) { 048 super(in); 049 decoder = new QuotedPrintableEncoder(); 050 } 051 052 // in order to function as a filter, these streams need to override the different 053 // read() signatures. 054 055 056 /** 057 * Read a single byte from the stream. 058 * 059 * @return The next byte of the stream. Returns -1 for an EOF condition. 060 * @exception IOException 061 */ 062 public int read() throws IOException 063 { 064 // just get a single byte from the decoder 065 return decoder.decode(in); 066 } 067 068 069 /** 070 * Read a buffer of data from the input stream. 071 * 072 * @param buffer The target byte array the data is placed into. 073 * @param offset The starting offset for the read data. 074 * @param length How much data is requested. 075 * 076 * @return The number of bytes of data read. 077 * @exception IOException 078 */ 079 public int read(byte [] buffer, int offset, int length) throws IOException { 080 081 for (int i = 0; i < length; i++) { 082 int ch = decoder.decode(in); 083 if (ch == -1) { 084 return i; 085 } 086 buffer[offset + i] = (byte)ch; 087 } 088 089 return length; 090 } 091 092 093 /** 094 * Indicate whether this stream supports the mark() operation. 095 * 096 * @return Always returns false. 097 */ 098 public boolean markSupported() { 099 return false; 100 } 101 102 103 /** 104 * Give an estimate of how much additional data is available 105 * from this stream. 106 * 107 * @return Always returns -1. 108 * @exception IOException 109 */ 110 public int available() throws IOException { 111 // this is almost impossible to determine at this point 112 return -1; 113 } 114 } 115 116