001    package railo.runtime.coder;
002    
003    
004    public final class Base64Util {
005            
006            private static byte base64Alphabet[];
007            private static byte lookUpBase64Alphabet[];
008            
009            
010            /**
011             * @param arrayOctect byte array to check
012             * @return true if base64
013             */
014            public static boolean isBase64(byte arrayOctect[]) {
015                    int length = arrayOctect.length;
016                    if(length == 0)
017                            return true;
018                    for(int i = 0; i < length; i++) {
019                            if(!isBase64(arrayOctect[i]))
020                                    return false;
021                    }
022                    return true;
023            }
024            /**
025             * @param octect byte to check
026             * @return true if base64
027             */
028            public static boolean isBase64(byte octect) {
029                    return octect == 61 || base64Alphabet[octect] != -1;
030            }
031            /**
032             * @param isValidString string to check
033             * @return true if base64
034             */
035            public static boolean isBase64(String isValidString) {
036                    return isBase64(isValidString.getBytes());
037            }
038            /** Initializations */
039            static {
040                    base64Alphabet = new byte[255];
041                    lookUpBase64Alphabet = new byte[64];
042                    for(int i = 0; i < 255; i++)
043                            base64Alphabet[i] = -1;
044                    for(int i = 90; i >= 65; i--)
045                            base64Alphabet[i] = (byte)(i - 65);
046                    for(int i = 122; i >= 97; i--)
047                            base64Alphabet[i] = (byte)((i - 97) + 26);
048                    for(int i = 57; i >= 48; i--)
049                            base64Alphabet[i] = (byte)((i - 48) + 52);
050                    base64Alphabet[43] = 62;
051                    base64Alphabet[47] = 63;
052                    for(int i = 0; i <= 25; i++)
053                            lookUpBase64Alphabet[i] = (byte)(65 + i);
054                    int i = 26;
055                    for(int j = 0; i <= 51; j++) {
056                            lookUpBase64Alphabet[i] = (byte)(97 + j);
057                            i++;
058                    }
059                    i = 52;
060                    for(int j = 0; i <= 61; j++) {
061                            lookUpBase64Alphabet[i] = (byte)(48 + j);
062                            i++;
063                    }
064                    lookUpBase64Alphabet[62] = 43;
065                    lookUpBase64Alphabet[63] = 47;
066            }
067    }
068