Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I wrote the following code for Base64 encoding and decoding:</p> <pre><code>public static String base64Encode(String filename, boolean padding) { char[] base64Chars = ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/").toCharArray(); FileInputStream dataToEncode; StringBuilder encodedData = new StringBuilder(); byte[] dataBuffer = new byte[3]; int bytesRead; try { dataToEncode = new FileInputStream(filename); } catch (FileNotFoundException fnfe) { System.err.println("File not found!!"); return ""; } try { bytesRead = dataToEncode.read(dataBuffer); // cast to int, to avoid sign issues on the byte int buffer0 = dataBuffer[0] &amp; 0xFF; int buffer1 = dataBuffer[1] &amp; 0xFF; int buffer2 = dataBuffer[2] &amp; 0xFF; if (bytesRead == -1) { System.err.println("Premature END OF FILE (nothing read)!!"); dataToEncode.close(); return ""; } while (bytesRead == 3) { // calculation of the base64 digits int b641 = buffer0 &gt;&gt;&gt; 2; int b642 = ((buffer0 &amp; 0x03) &lt;&lt; 4) | (buffer1 &gt;&gt;&gt; 4); int b643 = ((buffer1 &amp; 0x0F) &lt;&lt; 2) | (buffer2 &gt;&gt;&gt; 6); int b644 = buffer2 &amp; 0x3F; // generation of the 4 base64 chars, for the 3 bytes encodedData.append(base64Chars[b641]); encodedData.append(base64Chars[b642]); encodedData.append(base64Chars[b643]); encodedData.append(base64Chars[b644]); bytesRead = dataToEncode.read(dataBuffer); buffer0 = dataBuffer[0] &amp; 0xFF; buffer1 = dataBuffer[1] &amp; 0xFF; buffer2 = dataBuffer[2] &amp; 0xFF; } if (bytesRead == 2) { encodedData.append(base64Chars[buffer0 &gt;&gt;&gt; 2]); encodedData.append(base64Chars[((buffer0 &amp; 0x03) &lt;&lt; 4) | (buffer1 &gt;&gt;&gt; 4)]); encodedData.append(base64Chars[((buffer1 &amp; 0x0F) &lt;&lt; 2)]); // exclude the last byte, that's just 0 if (padding) { // add the '=' character for padding, if the user wants encodedData.append('='); } } else if (bytesRead == 1) { encodedData.append(base64Chars[buffer0 &gt;&gt;&gt; 2]); encodedData.append(base64Chars[((buffer0 &amp; 0x03) &lt;&lt; 4)]); if (padding) { encodedData.append("=="); } } dataToEncode.close(); return encodedData.toString(); } catch (IOException e) { System.out.println("Error reading file!!"); return ""; } } public static byte[] base64Decode(String encodedData) { String base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; // remove padding encodedData = encodedData.split("=")[0]; // create the byte buffer to unpack the bytes into byte[] result = new byte[(int) (encodedData.length() / 4) * 3 + encodedData.length() % 4 - 1]; int readingPosition = 0, writingPosition = 0, b641, b642, b643, b644; while (encodedData.length() - readingPosition &gt; 3) { b641 = base64Chars.indexOf(encodedData.charAt(readingPosition++)); b642 = base64Chars.indexOf(encodedData.charAt(readingPosition++)); b643 = base64Chars.indexOf(encodedData.charAt(readingPosition++)); b644 = base64Chars.indexOf(encodedData.charAt(readingPosition++)); result[writingPosition++] = (byte) ((b641 &lt;&lt; 2) | (b642 &gt;&gt;&gt; 4)); result[writingPosition++] = (byte) (((b642 &amp; 0x0F) &lt;&lt; 4) | (b643 &gt;&gt;&gt; 2)); result[writingPosition++] = (byte) (((b643 &amp; 0x03) &lt;&lt; 6) | b644); } b641 = base64Chars.indexOf(encodedData.charAt(readingPosition++)); b642 = base64Chars.indexOf(encodedData.charAt(readingPosition++)); result[writingPosition++] = (byte) ((b641 &lt;&lt; 2) | (b642 &gt;&gt;&gt; 4)); if (encodedData.length() % 4 == 3) { b643 = base64Chars.indexOf(encodedData.charAt(readingPosition++)); result[writingPosition++] = (byte) (((b642 &amp; 0x0F) &lt;&lt; 4) | (b643 &gt;&gt;&gt; 2)); } return result; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload