aspose file tools*
The moose likes Java in General and the fly likes 175: warning: BASE64Encoder is internal proprietary API and m ay be removed in a future release Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "175: warning: BASE64Encoder is internal proprietary API and m ay be removed in a future release" Watch "175: warning: BASE64Encoder is internal proprietary API and m ay be removed in a future release" New topic
Author

175: warning: BASE64Encoder is internal proprietary API and m ay be removed in a future release

Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
Hello,

I am getting this warning:


I have downloaded base64coder.jar and placed into tomcat/lib, jdk/jre/lib/ext but still getting this warning. How can I fix it?

Best regards
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

Where did you download that JAR file from?

Ofcourse downloading the JAR and putting it in your classpath is not enough. You'll also need to change your program to make use of the Base64 codec that's in the JAR file, instead of using class sun.misc.BASE64Encoder.

You get the error because sun.misc.BASE64Encoder is an internal API of the JDK. It's not part of the official public Java API, so you are not supposed to be using it. There's no guarantee that in a future Java update this class will still exist, and in other Java implementations (for example IBM's JVM) this class doesn't exist.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42272
    
  64
You're probably better off using the base64 classes of the Apache Commons Codec library.


Ping & DNS - my free Android networking tools app
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
I am using it as but still throwing the same exception. What else I do?

Thanks for your reply
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
Ulf Dittmer wrote:You're probably better off using the base64 classes of the Apache Commons Codec library.

Yes I downloaded from Apache Commons Codec Library
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42272
    
  64
Good, now you just need to change your code to use it :-)
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732


These two changes I made but still getting the same warning. What else I have to change?

regards
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42272
    
  64
Where do you think you are using the org.apache.commons.codec.binary.Base64 class?
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
I think am using it here.
String encodedString = new sun.misc.BASE64Encoder().encode(fileContent);

thanks again for your prompt replies
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

Farakh khan wrote:I think am using it here.
String encodedString = new sun.misc.BASE64Encoder().encode(fileContent);

thanks again for your prompt replies


But this states that you are using the class sun.misc.BASE64Encoder. And that is an internal proprietary API and may be removed in a future release. If Sun/Oracle wants to do it.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

Really? Look again. Can you not see the difference between:

sun.misc.BASE64Encoder and org.apache.commons.codec.binary.Base64
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
Ulf Dittmer wrote:Where do you think you are using the org.apache.commons.codec.binary.Base64 class?


I tied like this:


Its throwing the following error:

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42272
    
  64
How are you defining "fileContent"? Note that the error message talks about "fileContents" - a different variable.

fileContent is byte[], and you're trying to call its "getBytes" method?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

fileContent is a byte[] in your code, and in line 5 you are calling the method getBytes() on it. That is not going to work, because a byte[] doesn't have a getBytes() method.

Also, there are more reasons why this will not work. The method read() of class FileInputStream, that you are calling in line 3, does not necessarily read the entire content of the file. It might just read the first few bytes, or even nothing at all. Just calling read() the way that you are doing is not a good way to read the content of a file into a byte array.
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732


Thanks for help
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

You haven't modified anything?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

As I already mentioned above, this is not guaranteed to work correctly:

This will not always read the entire content of the file into the byte array. The API documentation of FileInputStream.read(byte[] b) explains:
API documentation wrote:
Reads up to b.length bytes of data from this input stream into an array of bytes. This method blocks until some input is available.

Returns: the total number of bytes read into the buffer, or -1 if there is no more data because the end of the file has been reached.

Note that it might not read the entire file. It might read only the first 10 bytes, for example. Note that the method returns the number of bytes read. To read an entire file into a byte array, you need to call this method in a loop until it returns -1, or until you've filled the whole array. Like this:

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Or wrap the FileInputStream into a DataInputStream and use its readFully method. That essentially does the same.
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
Thanks Jasper from the bottom of my heart. Now I change the code like this:


Its still throwing the following error:
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42272
    
  64
Maybe you missed my earlier post:
fileContent is byte[], and you're trying to call its "getBytes" method?


And Jesper's:
fileContent is a byte[] in your code, and in line 5 you are calling the method getBytes() on it. That is not going to work, because a byte[] doesn't have a getBytes() method.
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
Thanks all

The following piece of code solved my problem


Thanks again
Kevin Simonson
Ranch Hand

Joined: Oct 22, 2011
Posts: 113
I have a problem that is similar to this. I am working on a project with existing code that refers to class BASE64Encoder, declares an object of that class, and calls one of its methods. Several people are working with this project. For each of them when they try to compile the code with Maven they get a warning that says: "sun.misc.BASE64Encoder is internal proprietary API and may be removed in a future release", but the warning message isn't enough to keep the code from executing. They can do a "mvn clean install" and have the tests all run okay. On the other hand, when I do a "mvn clean install" I also get the warning messages, but then Maven announces a "BUILD FAILURE" and generates "[ERROR]" messages telling me the same thing again: "sun.misc.BASE64Encoder is internal proprietary API and may be removed in a future release".

As an experiment I wrote a little Java program like so:

When I compile this I get the same warning messages, but it generates a "Bsfe.class" file and I can run it successfully with "java Bsfe 16". It looks like Maven is using a different virtual machine than I get when I run Java from the command line. Is there anyone out there who knows how to tell Maven which virtual machine to use, and how to tell it to use one that will treat an occurrence of BASE64Encoder as something to give a warning about, but not an error message?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

If you're using Maven, why not use Apache Commons Codec? It's just one dependency you need to add and you'll no longer get this message.
Kevin Simonson
Ranch Hand

Joined: Oct 22, 2011
Posts: 113
Rob Spoor wrote:If you're using Maven, why not use Apache Commons Codec? It's just one dependency you need to add and you'll no longer get this message.

The actual code that's messing me up is:

How would I implement this with the Apache Commons Codec?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

http://commons.apache.org/proper/commons-codec/archives/1.9/apidocs/org/apache/commons/codec/binary/Base64.html#encodeBase64String(byte[])
Kevin Simonson
Ranch Hand

Joined: Oct 22, 2011
Posts: 113
Rob Spoor wrote:http://commons.apache.org/proper/commons-codec/archives/1.9/apidocs/org/apache/commons/codec/binary/Base64.html#encodeBase64String(byte[])

As I indicated in my last post, the method I need to call is encodeBuffer() of class BASE64Encoder. The API for class org.apache.commons.codec.binary.Base64 doesn't have an encodeBuffer() method. It certainly has enough methods with encode in their names, namely four overloadings of encodeBase64(), encodeBase64Chunked(), encodeBase64String(), encodeBase64URLSafe(), encodeBase64URLSafeString(), and encodeInteger(). Are one of those equivalent to BASE64Encoder's encodeBuffer() method?
Kevin Simonson
Ranch Hand

Joined: Oct 22, 2011
Posts: 113
Kevin Simonson wrote:
Rob Spoor wrote:http://commons.apache.org/proper/commons-codec/archives/1.9/apidocs/org/apache/commons/codec/binary/Base64.html#encodeBase64String(byte[])

As I indicated in my last post, the method I need to call is encodeBuffer() of class BASE64Encoder. The API for class org.apache.commons.codec.binary.Base64 doesn't have an encodeBuffer() method. It certainly has enough methods with encode in their names, namely four overloadings of encodeBase64(), encodeBase64Chunked(), encodeBase64String(), encodeBase64URLSafe(), encodeBase64URLSafeString(), and encodeInteger(). Are one of those equivalent to BASE64Encoder's encodeBuffer() method?

Okay, now I feel stupid. Of the methods I listed up there, only two of them return a String. So I guess the question I need to ask is, are either of encodeBase64String() or encodeBase64URLSafeString() equivalent to BASE64Encoder's encodeBuffer(), and if so, which one?
Kevin Simonson
Ranch Hand

Joined: Oct 22, 2011
Posts: 113
Kevin Simonson wrote:
Kevin Simonson wrote:As I indicated in my last post, the method I need to call is encodeBuffer() of class BASE64Encoder. The API for class org.apache.commons.codec.binary.Base64 doesn't have an encodeBuffer() method. It certainly has enough methods with encode in their names, namely four overloadings of encodeBase64(), encodeBase64Chunked(), encodeBase64String(), encodeBase64URLSafe(), encodeBase64URLSafeString(), and encodeInteger(). Are one of those equivalent to BASE64Encoder's encodeBuffer() method?

Okay, now I feel stupid. Of the methods I listed up there, only two of them return a String. So I guess the question I need to ask is, are either of encodeBase64String() or encodeBase64URLSafeString() equivalent to BASE64Encoder's encodeBuffer(), and if so, which one?

I've run into yet another snag. I tried modifying my stand-alone program like so:

Then when I tried to compile it I got:

C:\K\Java\Misc>javac Bsfe.java
Bsfe.java:1: error: package org.apache.commons.codec.binary does not exist
import org.apache.commons.codec.binary.Base64;
^
Bsfe.java:2: warning: BASE64Encoder is internal proprietary API and may be remov
ed in a future release
import sun.misc.BASE64Encoder;
^
Bsfe.java:21: warning: BASE64Encoder is internal proprietary API and may be remo
ved in a future release
= (new BASE64Encoder()).encodeBuffer( bytesTimeStampUtf8Encrypted);
^
Bsfe.java:23: error: cannot find symbol
Base64 base64 = new Base64();
^
symbol: class Base64
location: class Bsfe
Bsfe.java:23: error: cannot find symbol
Base64 base64 = new Base64();
^
symbol: class Base64
location: class Bsfe
3 errors
2 warnings

C:\K\Java\Misc>

So, if I'm going to use the Apache Commons Codec, and in particular its class Base64 and its methods, how do I get my compiler to recognize it? Is there a ".jar" file I need to include on my classpath? Is there a plugin I need to insert into the relevant Maven "pom.xml" file?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

http://mvnrepository.com/artifact/commons-codec/commons-codec/1.9
Kevin Simonson
Ranch Hand

Joined: Oct 22, 2011
Posts: 113
Rob Spoor wrote:http://mvnrepository.com/artifact/commons-codec/commons-codec/1.9

Great; thanks!

Now, as I stated in a previous post, my stand-alone program is currently:

When I run it with "java -cp .;commons-codec-1.9-bin\commons-codec-1.9\commons-codec-1.9.jar Bsfe 16" I get results:

secret: "AAECAwQFBgcICQoLDA0ODw==
"
newSecret: "AAECAwQFBgcICQoLDA0ODw=="
newUsSecret: "AAECAwQFBgcICQoLDA0ODw"

so it looks like method encodeBase64String() is the one that's equivalent to BASE64Encoder's method encodeBuffer(), but that's just based on observation; can anybody out there authoritatively tell me that Base64's method encodeBase64String() is the equivalent of BASE64Encoder's method encodeBuffer() and that I can therefore replace the latter with the former in my code, with confidence that it will always give the same results?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: 175: warning: BASE64Encoder is internal proprietary API and m ay be removed in a future release