< prev index next >

src/share/classes/java/util/Base64.java

Print this page
rev 11879 : [mq]: 8165243-Base64-Encoder-wrap-os-write-byte-int-int-with-incorrect-arguments-should-not-produce-output
   1 /*
   2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 771         }
 772 
 773         @Override
 774         public void write(int b) throws IOException {
 775             byte[] buf = new byte[1];
 776             buf[0] = (byte)(b & 0xff);
 777             write(buf, 0, 1);
 778         }
 779 
 780         private void checkNewline() throws IOException {
 781             if (linepos == linemax) {
 782                 out.write(newline);
 783                 linepos = 0;
 784             }
 785         }
 786 
 787         @Override
 788         public void write(byte[] b, int off, int len) throws IOException {
 789             if (closed)
 790                 throw new IOException("Stream is closed");
 791             if (off < 0 || len < 0 || off + len > b.length)
 792                 throw new ArrayIndexOutOfBoundsException();
 793             if (len == 0)
 794                 return;
 795             if (leftover != 0) {
 796                 if (leftover == 1) {
 797                     b1 = b[off++] & 0xff;
 798                     len--;
 799                     if (len == 0) {
 800                         leftover++;
 801                         return;
 802                     }
 803                 }
 804                 b2 = b[off++] & 0xff;
 805                 len--;
 806                 checkNewline();
 807                 out.write(base64[b0 >> 2]);
 808                 out.write(base64[(b0 << 4) & 0x3f | (b1 >> 4)]);
 809                 out.write(base64[(b1 << 2) & 0x3f | (b2 >> 6)]);
 810                 out.write(base64[b2 & 0x3f]);
 811                 linepos += 4;


   1 /*
   2  * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 771         }
 772 
 773         @Override
 774         public void write(int b) throws IOException {
 775             byte[] buf = new byte[1];
 776             buf[0] = (byte)(b & 0xff);
 777             write(buf, 0, 1);
 778         }
 779 
 780         private void checkNewline() throws IOException {
 781             if (linepos == linemax) {
 782                 out.write(newline);
 783                 linepos = 0;
 784             }
 785         }
 786 
 787         @Override
 788         public void write(byte[] b, int off, int len) throws IOException {
 789             if (closed)
 790                 throw new IOException("Stream is closed");
 791             if (off < 0 || len < 0 || len > b.length - off)
 792                 throw new ArrayIndexOutOfBoundsException();
 793             if (len == 0)
 794                 return;
 795             if (leftover != 0) {
 796                 if (leftover == 1) {
 797                     b1 = b[off++] & 0xff;
 798                     len--;
 799                     if (len == 0) {
 800                         leftover++;
 801                         return;
 802                     }
 803                 }
 804                 b2 = b[off++] & 0xff;
 805                 len--;
 806                 checkNewline();
 807                 out.write(base64[b0 >> 2]);
 808                 out.write(base64[(b0 << 4) & 0x3f | (b1 >> 4)]);
 809                 out.write(base64[(b1 << 2) & 0x3f | (b2 >> 6)]);
 810                 out.write(base64[b2 & 0x3f]);
 811                 linepos += 4;


< prev index next >