Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smaller static #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions src/main/java/com/migcomponents/migbase64/Base64.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.Arrays;

import static com.migcomponents.migbase64.Dictionary.CA;

/** A very fast and memory efficient class to encode and decode to and from BASE64 in full accordance
* with RFC 2045.<br><br>
* On Windows XP sp1 with 1.4.2_04 and later ;), this encoder and decoder is about 10 times faster
Expand Down Expand Up @@ -72,11 +74,10 @@

public class Base64
{
private static final char[] CA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();
private static final int[] IA = new int[256];
private static final byte[] IA = new byte[256];
static {
Arrays.fill(IA, -1);
for (int i = 0, iS = CA.length; i < iS; i++)
Arrays.fill(IA, (byte) -1);
for (byte i = 0, iS = (byte) CA.length; i < iS; i++)
IA[CA[i]] = i;
IA['='] = 0;
}
Expand Down Expand Up @@ -110,10 +111,10 @@ public final static char[] encodeToChar(byte[] sArr, boolean lineSep)
int i = (sArr[s++] & 0xff) << 16 | (sArr[s++] & 0xff) << 8 | (sArr[s++] & 0xff);

// Encode the int into four chars
dArr[d++] = CA[(i >>> 18) & 0x3f];
dArr[d++] = CA[(i >>> 12) & 0x3f];
dArr[d++] = CA[(i >>> 6) & 0x3f];
dArr[d++] = CA[i & 0x3f];
dArr[d++] = (char) CA[(i >>> 18) & 0x3f];
dArr[d++] = (char) CA[(i >>> 12) & 0x3f];
dArr[d++] = (char) CA[(i >>> 6) & 0x3f];
dArr[d++] = (char) CA[i & 0x3f];

// Add optional line separator
if (lineSep && ++cc == 19 && d < dLen - 2) {
Expand All @@ -130,9 +131,9 @@ public final static char[] encodeToChar(byte[] sArr, boolean lineSep)
int i = ((sArr[eLen] & 0xff) << 10) | (left == 2 ? ((sArr[sLen - 1] & 0xff) << 2) : 0);

// Set last four chars
dArr[dLen - 4] = CA[i >> 12];
dArr[dLen - 3] = CA[(i >>> 6) & 0x3f];
dArr[dLen - 2] = left == 2 ? CA[i & 0x3f] : '=';
dArr[dLen - 4] = (char) CA[i >> 12];
dArr[dLen - 3] = (char) CA[(i >>> 6) & 0x3f];
dArr[dLen - 2] = (char) (left == 2 ? CA[i & 0x3f] : '=');
dArr[dLen - 1] = '=';
}
return dArr;
Expand Down Expand Up @@ -299,10 +300,10 @@ public final static byte[] encodeToByte(byte[] sArr, int sOff, int sLen, boolean
int i = (sArr[s++] & 0xff) << 16 | (sArr[s++] & 0xff) << 8 | (sArr[s++] & 0xff);

// Encode the int into four chars
dArr[d++] = (byte) CA[(i >>> 18) & 0x3f];
dArr[d++] = (byte) CA[(i >>> 12) & 0x3f];
dArr[d++] = (byte) CA[(i >>> 6) & 0x3f];
dArr[d++] = (byte) CA[i & 0x3f];
dArr[d++] = CA[(i >>> 18) & 0x3f];
dArr[d++] = CA[(i >>> 12) & 0x3f];
dArr[d++] = CA[(i >>> 6) & 0x3f];
dArr[d++] = CA[i & 0x3f];

// Add optional line separator
if (lineSep && ++cc == 19 && d < dLen - 2) {
Expand All @@ -319,9 +320,9 @@ public final static byte[] encodeToByte(byte[] sArr, int sOff, int sLen, boolean
int i = ((sArr[sOff + eLen] & 0xff) << 10) | (left == 2 ? ((sArr[sOff + sLen - 1] & 0xff) << 2) : 0);

// Set last four chars
dArr[dLen - 4] = (byte) CA[i >> 12];
dArr[dLen - 3] = (byte) CA[(i >>> 6) & 0x3f];
dArr[dLen - 2] = left == 2 ? (byte) CA[i & 0x3f] : (byte) '=';
dArr[dLen - 4] = CA[i >> 12];
dArr[dLen - 3] = CA[(i >>> 6) & 0x3f];
dArr[dLen - 2] = left == 2 ? CA[i & 0x3f] : (byte) '=';
dArr[dLen - 1] = '=';
}
return dArr;
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/com/migcomponents/migbase64/Base64IO.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.io.InputStream;
import java.io.OutputStream;

import com.migcomponents.migbase64.Base64;
import static com.migcomponents.migbase64.Dictionary.CA;

/**
* Base64 for InputStream<br/> Licence = BSD
Expand All @@ -16,9 +16,6 @@
public class Base64IO
{

private static final byte[] CA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
.getBytes();

private static final int _8_BIT = 0xff;

private static final int _6_BIT = 0x3f;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/migcomponents/migbase64/Dictionary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.migcomponents.migbase64;

/*package*/ interface Dictionary {
byte[] CA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".getBytes();
}