jaxws/src/java.xml.ws/share/classes/com/sun/xml/internal/messaging/saaj/packaging/mime/util/BASE64DecoderStream.java
changeset 43852 93a527059d8a
parent 28326 2b9860c0d68a
equal deleted inserted replaced
43752:3c68ef249093 43852:93a527059d8a
     1 /*
     1 /*
     2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    68      * @return     next byte of data, or <code>-1</code> if the end of the
    68      * @return     next byte of data, or <code>-1</code> if the end of the
    69      *             stream is reached.
    69      *             stream is reached.
    70      * @exception  IOException  if an I/O error occurs.
    70      * @exception  IOException  if an I/O error occurs.
    71      * @see        java.io.FilterInputStream#in
    71      * @see        java.io.FilterInputStream#in
    72      */
    72      */
       
    73     @Override
    73     public int read() throws IOException {
    74     public int read() throws IOException {
    74         if (index >= bufsize) {
    75         if (index >= bufsize) {
    75             decode(); // Fills up buffer
    76             decode(); // Fills up buffer
    76             if (bufsize == 0) // buffer is empty
    77             if (bufsize == 0) // buffer is empty
    77                 return -1;
    78                 return -1;
    92      * @return     the total number of bytes read into the buffer, or
    93      * @return     the total number of bytes read into the buffer, or
    93      *             <code>-1</code> if there is no more data because the end of
    94      *             <code>-1</code> if there is no more data because the end of
    94      *             the stream has been reached.
    95      *             the stream has been reached.
    95      * @exception  IOException  if an I/O error occurs.
    96      * @exception  IOException  if an I/O error occurs.
    96      */
    97      */
       
    98     @Override
    97     public int read(byte[] buf, int off, int len) throws IOException {
    99     public int read(byte[] buf, int off, int len) throws IOException {
    98         int i, c;
   100         int i, c;
    99         for (i = 0; i < len; i++) {
   101         for (i = 0; i < len; i++) {
   100             if ((c = read()) == -1) {
   102             if ((c = read()) == -1) {
   101                 if (i == 0) // At end of stream, so we should
   103                 if (i == 0) // At end of stream, so we should
   110 
   112 
   111     /**
   113     /**
   112      * Tests if this input stream supports marks. Currently this class
   114      * Tests if this input stream supports marks. Currently this class
   113      * does not support marks
   115      * does not support marks
   114      */
   116      */
       
   117     @Override
   115     public boolean markSupported() {
   118     public boolean markSupported() {
   116         return false; // Maybe later ..
   119         return false; // Maybe later ..
   117     }
   120     }
   118 
   121 
   119     /**
   122     /**
   120      * Returns the number of bytes that can be read from this input
   123      * Returns the number of bytes that can be read from this input
   121      * stream without blocking. However, this figure is only
   124      * stream without blocking. However, this figure is only
   122      * a close approximation in case the original encoded stream
   125      * a close approximation in case the original encoded stream
   123      * contains embedded CRLFs; since the CRLFs are discarded, not decoded
   126      * contains embedded CRLFs; since the CRLFs are discarded, not decoded
   124      */
   127      */
       
   128     @Override
   125     public int available() throws IOException {
   129     public int available() throws IOException {
   126          // This is only an estimate, since in.available()
   130          // This is only an estimate, since in.available()
   127          // might include CRLFs too ..
   131          // might include CRLFs too ..
   128          return ((in.available() * 3)/4 + (bufsize-index));
   132          return ((in.available() * 3)/4 + (bufsize-index));
   129     }
   133     }
   198      * Base64 decode a byte array.  No line breaks are allowed.
   202      * Base64 decode a byte array.  No line breaks are allowed.
   199      * This method is suitable for short strings, such as those
   203      * This method is suitable for short strings, such as those
   200      * in the IMAP AUTHENTICATE protocol, but not to decode the
   204      * in the IMAP AUTHENTICATE protocol, but not to decode the
   201      * entire content of a MIME part.
   205      * entire content of a MIME part.
   202      *
   206      *
       
   207      * @param inbuf byte array to decode
       
   208      *
       
   209      * @return decoded byte array
       
   210      *
   203      * NOTE: inbuf may only contain valid base64 characters.
   211      * NOTE: inbuf may only contain valid base64 characters.
   204      *       Whitespace is not ignored.
   212      *       Whitespace is not ignored.
   205      */
   213      */
   206     public static byte[] decode(byte[] inbuf) {
   214     public static byte[] decode(byte[] inbuf) {
   207         int size = (inbuf.length / 4) * 3;
   215         int size = (inbuf.length / 4) * 3;