blob: ab8c3ab834aa60f0d952ff3b629c8d16acbb0122 [file] [log] [blame]
/*
* Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @(#)QDecoderStream.java 1.5 02/03/27
*/
package com.sun.xml.internal.messaging.saaj.packaging.mime.util;
import java.io.IOException;
import java.io.InputStream;
/**
* This class implements a Q Decoder as defined in RFC 2047
* for decoding MIME headers. It subclasses the QPDecoderStream class.
*
* @author John Mani
*/
public class QDecoderStream extends QPDecoderStream {
/**
* Create a Q-decoder that decodes the specified input stream.
* @param in the input stream
*/
public QDecoderStream(InputStream in) {
super(in);
}
/**
* Read the next decoded byte from this input stream. The byte
* is returned as an <code>int</code> in the range <code>0</code>
* to <code>255</code>. If no byte is available because the end of
* the stream has been reached, the value <code>-1</code> is returned.
* This method blocks until input data is available, the end of the
* stream is detected, or an exception is thrown.
*
* @return the next byte of data, or <code>-1</code> if the end of the
* stream is reached.
* @exception IOException if an I/O error occurs.
*/
@Override
public int read() throws IOException {
int c = in.read();
if (c == '_') // Return '_' as ' '
return ' ';
else if (c == '=') {
// QP Encoded atom. Get the next two bytes ..
ba[0] = (byte)in.read();
ba[1] = (byte)in.read();
// .. and decode them
try {
return ASCIIUtility.parseInt(ba, 0, 2, 16);
} catch (NumberFormatException nex) {
throw new IOException("Error in QP stream " + nex.getMessage());
}
} else
return c;
}
}