Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9311201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:18:18+00:00 2026-06-19T01:18:18+00:00

I have the following method: /** * Encodes the byte array into base64 string

  • 0

I have the following method:

 /**
  * Encodes the byte array into base64 string
  * @param imageByteArray - byte array
  * @return String a {@link java.lang.String}
  */
public static String encodeImage(byte[] imageByteArray) {
    return Base64.encodeBase64URLSafeString(imageByteArray);
}

I am importing “org.apache.commons.codec.binary.Base64;” However, I get the error:

Multiple markers at this line
– The method encodeBase64URLSafeString(byte[]) is undefined for the
type Base64
– Line breakpoint:MySQLConnection [line: 287] – encodeImage(byte[])

I have copied this code from “http://www.myjeeva.com/2012/07/how-to-convert-image-to-string-and-string-to-image-in-java/“. I am using Eclipse Juno (fully updated) and GWT.

Does anyone know what I am doing wrong here?

Regards,

Glyn

Thank you Markus A. I have now created this Class from your information:

package org.AwardTracker.server;

public class Base64Decode {
private final static String base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

public static byte[] decode(String s) {

    // remove/ignore any characters not in the base64 characters list
    // or the pad character -- particularly newlines
    s = s.replaceAll("[^" + base64chars + "=]", "");

    // replace any incoming padding with a zero pad (the 'A' character is
    // zero)
    String p = (s.charAt(s.length() - 1) == '=' ? (s.charAt(s.length() - 2) == '=' ? "AA"
            : "A")
            : "");

    s = s.substring(0, s.length() - p.length()) + p;
    int resLength = (int) Math.ceil(((float) (s.length()) / 4f) * 3f);
    byte[] bufIn = new byte[resLength];
    int bufIn_i = 0;

    // increment over the length of this encrypted string, four characters
    // at a time
    for (int c = 0; c < s.length(); c += 4) {

        // each of these four characters represents a 6-bit index in the
        // base64 characters list which, when concatenated, will give the
        // 24-bit number for the original 3 characters
        int n = (base64chars.indexOf(s.charAt(c)) << 18)
                + (base64chars.indexOf(s.charAt(c + 1)) << 12)
                + (base64chars.indexOf(s.charAt(c + 2)) << 6)
                + base64chars.indexOf(s.charAt(c + 3));

        // split the 24-bit number into the original three 8-bit (ASCII)
        // characters

        char c1 = (char) ((n >>> 16) & 0xFF);
        char c2 = (char) ((n >>>8) & 0xFF);
        char c3 = (char) (n & 0xFF);

        bufIn[bufIn_i++] = (byte) c1;
        bufIn[bufIn_i++] = (byte) c2;
        bufIn[bufIn_i++] = (byte) c3;

    }

    return bufIn;
}


}

And changed the call to:
import org.AwardTracker.server.Base64Decode;

public static String encodeImage(byte[] imageByteArray) {
    return Base64Decode(imageByteArray); [Error on this line]
}

And I now get the error:

Multiple markers at this line
– The method Base64Decode(byte[]) is undefined for the type
MySQLConnection

Help greatfuly appreciated.

Regards,

Glyn

Round Three

OK, so I had my encrypt and decrypt arround the wrong way. This has now been corrected and placed in the correct libraries. However I still have an error in the encodeImage method. This is the method I use to call the encodeImage method. I suspect that the lines:

java.sql.Blob imageBlob = result.getBlob(1);
byte[] imageData = imageBlob.getBytes(1, (int) imageBlob.length());

are not correct. However, I have defined ImageData as a byte[] so I do not see why encodeImage thinks it is a String?

public String getImageData(String id){
    ResultSet result = null;
    PreparedStatement ps = null;
    String imageDataString = null;
    try {
        // Read in the image from the database.
        ps = conn.prepareStatement(
              "SELECT at_cub_details.cd_photograph " +
                      "FROM at_cub_details " + 
                      "WHERE at_cub_details.cd_id = \"" + id + "\"");
        result = ps.executeQuery();
        while (result.next()) {
            java.sql.Blob imageBlob = result.getBlob(1);
            byte[] imageData = imageBlob.getBytes(1, (int) imageBlob.length());

            //Convert Image byte array into Base64 String
            imageDataString = encodeImage(imageData);
        }

    }

Resulting in an error in:

public static String encodeImage(byte[] imageByteArray) {
    return Base64Encode.encode(imageByteArray); [Error on this line]
}

of “The method encode(String) in the type Base64Encode is not applicable for the arguments (byte[])”

Thanks for all your help.

Regards,

Glyn

p.s., I think this will deserve a blog when I am finished as I can not find any definitve work in this area and I would have thought that this would be used very often. My next project.

The Encode class is:

package org.AwardTracker.server;

public class Base64Encode {
private final static String base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

public static String encode(String s) {

    // the result/encoded string, the padding string, and the pad count
    String r = "", p = "";
    int c = s.length() % 3;

    // add a right zero pad to make this string a multiple of 3 characters
    if (c > 0) {
        for (; c < 3; c++) {
            p += "=";
            s += "\0";
        }
    }

    // increment over the length of the string, three characters at a time
    for (c = 0; c < s.length(); c += 3) {

        // we add newlines after every 76 output characters, according to
        // the MIME specs
        if (c > 0 && (c / 3 * 4) % 76 == 0)
            r += "\r\n";

        // these three 8-bit (ASCII) characters become one 24-bit number
        int n = (s.charAt(c) << 16) + (s.charAt(c + 1) << 8)
                + (s.charAt(c + 2));

        // this 24-bit number gets separated into four 6-bit numbers
        int n1 = (n >> 18) & 63, n2 = (n >> 12) & 63, n3 = (n >> 6) & 63, n4 = n & 63;

        // those four 6-bit numbers are used as indices into the base64
        // character list
        r += "" + base64chars.charAt(n1) + base64chars.charAt(n2)
                + base64chars.charAt(n3) + base64chars.charAt(n4);
    }

    return r.substring(0, r.length() - p.length()) + p;
}
}

And the Decode class is:

package org.AwardTracker.server;

public class Base64Decode {
private final static String base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

public static byte[] decode(String s) {

    // remove/ignore any characters not in the base64 characters list
    // or the pad character -- particularly newlines
    s = s.replaceAll("[^" + base64chars + "=]", "");

    // replace any incoming padding with a zero pad (the 'A' character is
    // zero)
    String p = (s.charAt(s.length() - 1) == '=' ? (s.charAt(s.length() - 2) == '=' ? "AA"
            : "A")
            : "");

    s = s.substring(0, s.length() - p.length()) + p;
    int resLength = (int) Math.ceil(((float) (s.length()) / 4f) * 3f);
    byte[] bufIn = new byte[resLength];
    int bufIn_i = 0;

    // increment over the length of this encrypted string, four characters
    // at a time
    for (int c = 0; c < s.length(); c += 4) {

        // each of these four characters represents a 6-bit index in the
        // base64 characters list which, when concatenated, will give the
        // 24-bit number for the original 3 characters
        int n = (base64chars.indexOf(s.charAt(c)) << 18)
                + (base64chars.indexOf(s.charAt(c + 1)) << 12)
                + (base64chars.indexOf(s.charAt(c + 2)) << 6)
                + base64chars.indexOf(s.charAt(c + 3));

        // split the 24-bit number into the original three 8-bit (ASCII)
        // characters

        char c1 = (char) ((n >>> 16) & 0xFF);
        char c2 = (char) ((n >>>8) & 0xFF);
        char c3 = (char) (n & 0xFF);

        bufIn[bufIn_i++] = (byte) c1;
        bufIn[bufIn_i++] = (byte) c2;
        bufIn[bufIn_i++] = (byte) c3;

    }

    return bufIn;
}


}

I have finally found an encode class that works:

package org.AwardTracker.server;

public class Base64Encode2 {
 private final static char[] ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();

    private static int[]  toInt   = new int[128];

    static {
        for(int i=0; i< ALPHABET.length; i++){
            toInt[ALPHABET[i]]= i;
        }
    }

/**
 * Translates the specified byte array into Base64 string.
 *
 * @param buf the byte array (not null)
 * @return the translated Base64 string (not null)
 */
public static String encode(byte[] buf){
    int size = buf.length;
    char[] ar = new char[((size + 2) / 3) * 4];
    int a = 0;
    int i=0;
    while(i < size){
        byte b0 = buf[i++];
        byte b1 = (i < size) ? buf[i++] : 0;
        byte b2 = (i < size) ? buf[i++] : 0;

        int mask = 0x3F;
        ar[a++] = ALPHABET[(b0 >> 2) & mask];
        ar[a++] = ALPHABET[((b0 << 4) | ((b1 & 0xFF) >> 4)) & mask];
        ar[a++] = ALPHABET[((b1 << 2) | ((b2 & 0xFF) >> 6)) & mask];
        ar[a++] = ALPHABET[b2 & mask];
    }
    switch(size % 3){
        case 1: ar[--a]  = '=';
        case 2: ar[--a]  = '=';
    }
    return new String(ar);
}

}

Thanks for all your help Markus A. It is greatly appreciated.

Regards,

Glyn

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T01:18:19+00:00Added an answer on June 19, 2026 at 1:18 am

    GWT does not provide the apache commons package, so I don’t think you’ll be able to just use this function in your code.

    Here are the things you can use in GWT:

    https://developers.google.com/web-toolkit/doc/latest/RefJreEmulation

    If you do want to use it, you need to actually have the source code of the Base64 class (and all its dependencies) available and put that into your project somewhere where the GWT compiler can reach it to turn it into JavaScript.

    For other ways to do the encoding, see here:

    How do I encode/decode short strings as Base64 using GWT?

    Addition for new question added to original post:

    For starters, you need to call static functions on a class like this:

    return Base64Decode.decode(imageByteArray);
    

    But you also seem to have your types backwards: encodeImage takes a byte[] and returns a String, while Base64Decode.decode takes a String and returns a byte[]. You probably need to be using the equivalent Base64Encode.encode function instead of the decode function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following method: public string Phase(string phase) { return Phase 1; }
I have a method which encodes some key-value entries into an ASCII string with
Say I have the following line in a method: String encodedString = URLEncoder.encode(foo, utf-8);
I have the following method: public static T ExecuteScalar<T>( string query, SqlConnection connection, params
I have a base64 encoded string that was encoded in Java: string s =
I have a method with the following signiture: public <T> T encode(String[] data, Class<T>
I have the following method in Flash which writes two ByteArrays and then base64
I have following method in wcf webenabled service Public Person AddPerson(Person p); As of
I have following method which I am using to load ActiveX control dynamically, Dim
I have the following method, which takes in the name of a file as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.