/** * Retrieve a single byte from the decoded characters buffer. * * @return The decoded character or -1 if there was an EOF condition. */ private int getByte() throws IOException { if (!dataAvailable() && !decodeStreamData()) { return -1; } decodedCount--; // we need to ensure this doesn't get sign extended return decodedChars[decodedIndex++] & 0xff; }
public int read() throws IOException { return getByte(); }
public int read(byte [] buffer, int offset, int length) throws IOException { return getBytes(buffer, offset, length); }
public static InputStream getInputStreamFromDataOrFile(String data, File file) throws FileNotFoundException { if (data != null) { return new Base64DecoderStream(new ByteArrayInputStream(data.getBytes())); } if (file != null) { return new FileInputStream(file); } return null; }
/** * Decode a requested number of bytes of data into a buffer. * * @return true if we were able to obtain more data, false otherwise. */ private boolean decodeStreamData() throws IOException { decodedIndex = 0; // fill up a data buffer with input data int readCharacters = fillEncodedBuffer(); if (readCharacters > 0) { try { decodedChars = Base64Utility.decodeChunk(encodedChars, 0, readCharacters); } catch (Base64Exception e) { throw new IOException(e); } decodedCount = decodedChars.length; return true; } return false; }
public static InputStream decode(InputStream in, String encoding) throws IOException { if (encoding == null) { return in; } encoding = encoding.toLowerCase(); // some encodings are just pass-throughs, with no real decoding. if ("binary".equals(encoding) || "7bit".equals(encoding) || "8bit".equals(encoding)) { return in; } else if ("base64".equals(encoding)) { return new Base64DecoderStream(in); } else if ("quoted-printable".equals(encoding)) { return new QuotedPrintableDecoderStream(in); } else { throw new IOException("Unknown encoding " + encoding); } } public static boolean isTypeSupported(String contentType, List<String> types) {
/** * Decode a requested number of bytes of data into a buffer. * * @return true if we were able to obtain more data, false otherwise. */ private boolean decodeStreamData() throws IOException { decodedIndex = 0; // fill up a data buffer with input data int readCharacters = fillEncodedBuffer(); if (readCharacters > 0) { try { decodedChars = Base64Utility.decodeChunk(encodedChars, 0, readCharacters); } catch (Base64Exception e) { throw new IOException(e); } decodedCount = decodedChars.length; return true; } return false; }
/** * Retrieve a single byte from the decoded characters buffer. * * @return The decoded character or -1 if there was an EOF condition. */ private int getByte() throws IOException { if (!dataAvailable() && !decodeStreamData()) { return -1; } decodedCount--; // we need to ensure this doesn't get sign extended return decodedChars[decodedIndex++] & 0xff; }
public static InputStream decode(InputStream in, String encoding) throws IOException { if (encoding == null) { return in; } encoding = encoding.toLowerCase(); // some encodings are just pass-throughs, with no real decoding. if ("binary".equals(encoding) || "7bit".equals(encoding) || "8bit".equals(encoding)) { return in; } else if ("base64".equals(encoding)) { return new Base64DecoderStream(in); } else if ("quoted-printable".equals(encoding)) { return new QuotedPrintableDecoderStream(in); } else { throw new IOException("Unknown encoding " + encoding); } } public static boolean isTypeSupported(String contentType, List<String> types) {
public int read() throws IOException { return getByte(); }
public int read(byte [] buffer, int offset, int length) throws IOException { return getBytes(buffer, offset, length); }
private int getBytes(byte[] data, int offset, int length) throws IOException { int readCharacters = 0; while (length > 0) { // need data? Try to get some if (!dataAvailable() && !decodeStreamData()) { // if we can't get this, return a count of how much we did get (which may be -1). return readCharacters > 0 ? readCharacters : -1; } // now copy some of the data from the decoded buffer to the target buffer int copyCount = Math.min(decodedCount, length); System.arraycopy(decodedChars, decodedIndex, data, offset, copyCount); decodedIndex += copyCount; decodedCount -= copyCount; offset += copyCount; length -= copyCount; readCharacters += copyCount; } return readCharacters; }
private int getBytes(byte[] data, int offset, int length) throws IOException { int readCharacters = 0; while (length > 0) { // need data? Try to get some if (!dataAvailable() && !decodeStreamData()) { // if we can't get this, return a count of how much we did get (which may be -1). return readCharacters > 0 ? readCharacters : -1; } // now copy some of the data from the decoded buffer to the target buffer int copyCount = Math.min(decodedCount, length); System.arraycopy(decodedChars, decodedIndex, data, offset, copyCount); decodedIndex += copyCount; decodedCount -= copyCount; offset += copyCount; length -= copyCount; readCharacters += copyCount; } return readCharacters; }