Here you can find the source of encode(byte[] buffer, int offset, int len, Writer out)
public static void encode(byte[] buffer, int offset, int len, Writer out) throws IOException
//package com.java2s; /*//from ww w . ja v a2 s. com * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License (the "License"). * You may not use this file except in compliance with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at usr/src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ import java.io.*; public class Main { private static final char ENCODE[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' }; private static final char CHAR_PAD = '='; /** * Encodes the data on the given {@code InputStream} as base64 and writes it * to the given {@code Writer}. * * @exception IOException * if an I/O error ocurrs */ public static void encode(InputStream in, Writer out) throws IOException { OUTER: while (true) { int pad = 0; int atom = 0; // Read in 3 8-bit bytes for (int i = 0; i < 3; i++) { int b = in.read(); if (b == -1) { if (i == 0) { break OUTER; } pad++; } else { atom |= b; } atom <<= 8; } // Output 4 6-bit characters... for (int i = 0; i < 4 - pad; i++) { int index = (atom & 0xFC000000) >>> 26; out.write(ENCODE[index]); atom <<= 6; } // ...plus any padding if 3 bytes couldn't be read for (int i = 0; i < pad; i++) { out.write(CHAR_PAD); } } out.flush(); } public static void encode(byte[] buffer, int offset, int len, Writer out) throws IOException { ByteArrayInputStream in = new ByteArrayInputStream(buffer, offset, len); encode(in, out); } public static void encode(byte[] buffer, Writer out) throws IOException { encode(buffer, 0, buffer.length, out); } public static void encode(String s, Writer out) throws IOException { encode(s.getBytes(), out); } public static String encode(byte[] buffer, int offset, int len) { StringWriter out = new StringWriter(); try { encode(buffer, offset, len, out); return out.toString(); } catch (IOException e) { // Not likely -- output is from a byte array to a String } return null; } public static String encode(byte[] buffer) { return encode(buffer, 0, buffer.length); } public static String encode(String s) { return encode(s.getBytes()); } }