Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #28821 > unrolled thread
| Started by | www <xsli2@yahoo.com> |
|---|---|
| First post | 2014-06-23 09:02 -0700 |
| Last post | 2014-06-24 11:11 -0700 |
| Articles | 20 on this page of 28 — 11 participants |
Back to article view | Back to comp.lang.java.programmer
is that possible to include a small PNG file as part of Java class? www <xsli2@yahoo.com> - 2014-06-23 09:02 -0700
Re: is that possible to include a small PNG file as part of Java class? Martin Gregorie <martin@address-in-sig.invalid> - 2014-06-23 16:35 +0000
Re: is that possible to include a small PNG file as part of Java class? Donkey Hottie <donkey@fredriksson.dy.fi> - 2014-06-24 15:54 +0300
Re: is that possible to include a small PNG file as part of Java class? Jeff Higgins <jeff@invalid.invalid> - 2014-06-24 10:07 -0400
Re: is that possible to include a small PNG file as part of Java class? Jeff Higgins <jeff@invalid.invalid> - 2014-06-24 10:49 -0400
Re: is that possible to include a small PNG file as part of Java class? Roedy Green <see_website@mindprod.com.invalid> - 2014-06-25 01:25 -0700
Re: is that possible to include a small PNG file as part of Java class? Marcel Müller <news.5.maazl@spamgourmet.org> - 2014-06-23 18:25 +0200
Re: is that possible to include a small PNG file as part of Java class? Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-06-23 13:03 -0400
Re: is that possible to include a small PNG file as part of Java class? Wanja Gayk <brixomatic@yahoo.com> - 2014-06-29 11:14 +0200
Re: is that possible to include a small PNG file as part of Java class? Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-06-29 08:15 -0400
Re: is that possible to include a small PNG file as part of Java class? markspace <markspace@nospam.nospam> - 2014-06-23 10:39 -0700
Re: is that possible to include a small PNG file as part of Java class? Jeff Higgins <jeff@invalid.invalid> - 2014-06-23 15:04 -0400
Re: is that possible to include a small PNG file as part of Java class? Roedy Green <see_website@mindprod.com.invalid> - 2014-06-25 01:34 -0700
Re: is that possible to include a small PNG file as part of Java class? Joerg Meier <joergmmeier@arcor.de> - 2014-06-23 21:15 +0200
Re: is that possible to include a small PNG file as part of Java class? Jeff Higgins <jeff@invalid.invalid> - 2014-06-23 15:46 -0400
Re: is that possible to include a small PNG file as part of Java class? Joerg Meier <joergmmeier@arcor.de> - 2014-06-23 22:08 +0200
Re: is that possible to include a small PNG file as part of Java class? Jeff Higgins <jeff@invalid.invalid> - 2014-06-23 16:19 -0400
Re: is that possible to include a small PNG file as part of Java class? Joerg Meier <joergmmeier@arcor.de> - 2014-06-23 22:34 +0200
Re: is that possible to include a small PNG file as part of Java class? Jeff Higgins <jeff@invalid.invalid> - 2014-06-23 16:43 -0400
Re: is that possible to include a small PNG file as part of Java class? Joerg Meier <joergmmeier@arcor.de> - 2014-06-24 00:21 +0200
Re: is that possible to include a small PNG file as part of Java class? Jeff Higgins <jeff@invalid.invalid> - 2014-06-23 21:02 -0400
Re: is that possible to include a small PNG file as part of Java class? Jeff Higgins <jeff@invalid.invalid> - 2014-06-23 21:10 -0400
Re: is that possible to include a small PNG file as part of Java class? Jeff Higgins <jeff@invalid.invalid> - 2014-06-23 21:23 -0400
Re: is that possible to include a small PNG file as part of Java class? Jeff Higgins <jeff@invalid.invalid> - 2014-06-24 16:17 -0400
Re: is that possible to include a small PNG file as part of Java class? Jeff Higgins <jeff@invalid.invalid> - 2014-06-23 16:33 -0400
Re: is that possible to include a small PNG file as part of Java class? Jeff Higgins <jeff@invalid.invalid> - 2014-06-24 16:00 -0400
Re: is that possible to include a small PNG file as part of Java class? Andreas Leitgeb <avl@auth.logic.tuwien.ac.at> - 2014-06-24 17:17 +0000
Re: is that possible to include a small PNG file as part of Java class? Roedy Green <see_website@mindprod.com.invalid> - 2014-06-24 11:11 -0700
Page 1 of 2 [1] 2 Next page →
| From | www <xsli2@yahoo.com> |
|---|---|
| Date | 2014-06-23 09:02 -0700 |
| Subject | is that possible to include a small PNG file as part of Java class? |
| Message-ID | <dd7b4b53-4f46-4b80-9cdb-a6eebac84693@googlegroups.com> |
Hi:
I have a simple Java class: Abc.java. The method within it needs to access a small PNG file(it's an icon picture). Right now, this picture file("abc.png") is located at nonsrc/icons/abc.png
So this class is a bit pain. Abc.class won't work in the absence of abc.png. When I create an executable JAR, this is pain: always need to remember to include abc.png file inside it.
I am wondering if there is a way that I can "convert" abc.png into some sort of Java code and I can put it in Abc.java. So Abc.class alone can perform the job, without the need of presence of abc.png. Is that possible?
Hope I have explained my intention well.
Thank you.
[toc] | [next] | [standalone]
| From | Martin Gregorie <martin@address-in-sig.invalid> |
|---|---|
| Date | 2014-06-23 16:35 +0000 |
| Message-ID | <lo9l15$t3c$1@dont-email.me> |
| In reply to | #28821 |
On Mon, 23 Jun 2014 09:02:20 -0700, www wrote:
> Hi:
>
> I have a simple Java class: Abc.java. The method within it needs to
> access a small PNG file(it's an icon picture). Right now, this picture
> file("abc.png") is located at nonsrc/icons/abc.png
>
> So this class is a bit pain. Abc.class won't work in the absence of
> abc.png. When I create an executable JAR, this is pain: always need to
> remember to include abc.png file inside it.
>
> I am wondering if there is a way that I can "convert" abc.png into some
> sort of Java code and I can put it in Abc.java. So Abc.class alone can
> perform the job, without the need of presence of abc.png. Is that
> possible?
>
Not that I'm aware of.
Why not turn the problem up the other way? Include the image file
somewhere inside the package level directory that contains Abc.class and
use ant to run the compilation and, if that succeeds, assemble the jar
file.
It sounds as though you don't use ant or maven to run your compiles. I
use ant, which is fairly easy to learn. Using it has the advantave that
once you've set it up to compile a package hierarchy and create a jar
file containing it, you can easily add/rename/scrap classes etc without
changing the build file, which is what you create to tell ant what you
want it to do: once its set up, compilation, javadocs and jar creation
are all handled automatically by the "ant" command.
--
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |
[toc] | [prev] | [next] | [standalone]
| From | Donkey Hottie <donkey@fredriksson.dy.fi> |
|---|---|
| Date | 2014-06-24 15:54 +0300 |
| Message-ID | <o19o7b-m97.ln1@tempest.fredriksson.dy.fi> |
| In reply to | #28822 |
23.06.2014 19:35, Martin Gregorie kirjoitti:
> On Mon, 23 Jun 2014 09:02:20 -0700, www wrote:
>
>> Hi:
>>
>> I have a simple Java class: Abc.java. The method within it needs to
>> access a small PNG file(it's an icon picture). Right now, this picture
>> file("abc.png") is located at nonsrc/icons/abc.png
>>
>> So this class is a bit pain. Abc.class won't work in the absence of
>> abc.png. When I create an executable JAR, this is pain: always need to
>> remember to include abc.png file inside it.
>>
>> I am wondering if there is a way that I can "convert" abc.png into some
>> sort of Java code and I can put it in Abc.java. So Abc.class alone can
>> perform the job, without the need of presence of abc.png. Is that
>> possible?
>>
> Not that I'm aware of.
>
> Why not turn the problem up the other way? Include the image file
> somewhere inside the package level directory that contains Abc.class and
> use ant to run the compilation and, if that succeeds, assemble the jar
> file.
>
> It sounds as though you don't use ant or maven to run your compiles. I
> use ant, which is fairly easy to learn. Using it has the advantave that
> once you've set it up to compile a package hierarchy and create a jar
> file containing it, you can easily add/rename/scrap classes etc without
> changing the build file, which is what you create to tell ant what you
> want it to do: once its set up, compilation, javadocs and jar creation
> are all handled automatically by the "ant" command.
>
>
Of course it is possible to include an image to a Java file. Just create
a tool which will take an image as input and write a fragment of Java
source as byte[] image = {...<contents of the image>...} ;
That was very common one when we wrote programs in C/C++ and it should
work now too.
--
The fashion wears out more apparel than the man.
-- William Shakespeare, "Much Ado About Nothing"
[toc] | [prev] | [next] | [standalone]
| From | Jeff Higgins <jeff@invalid.invalid> |
|---|---|
| Date | 2014-06-24 10:07 -0400 |
| Message-ID | <loc115$der$1@dont-email.me> |
| In reply to | #28838 |
On 06/24/2014 08:54 AM, Donkey Hottie wrote:
> 23.06.2014 19:35, Martin Gregorie kirjoitti:
>> On Mon, 23 Jun 2014 09:02:20 -0700, www wrote:
>>
>>> Hi:
>>>
>>> I have a simple Java class: Abc.java. The method within it needs to
>>> access a small PNG file(it's an icon picture). Right now, this picture
>>> file("abc.png") is located at nonsrc/icons/abc.png
>>>
>>> So this class is a bit pain. Abc.class won't work in the absence of
>>> abc.png. When I create an executable JAR, this is pain: always need to
>>> remember to include abc.png file inside it.
>>>
>>> I am wondering if there is a way that I can "convert" abc.png into some
>>> sort of Java code and I can put it in Abc.java. So Abc.class alone can
>>> perform the job, without the need of presence of abc.png. Is that
>>> possible?
>>>
>> Not that I'm aware of.
>>
>> Why not turn the problem up the other way? Include the image file
>> somewhere inside the package level directory that contains Abc.class and
>> use ant to run the compilation and, if that succeeds, assemble the jar
>> file.
>>
>> It sounds as though you don't use ant or maven to run your compiles. I
>> use ant, which is fairly easy to learn. Using it has the advantave that
>> once you've set it up to compile a package hierarchy and create a jar
>> file containing it, you can easily add/rename/scrap classes etc without
>> changing the build file, which is what you create to tell ant what you
>> want it to do: once its set up, compilation, javadocs and jar creation
>> are all handled automatically by the "ant" command.
>>
>>
>
> Of course it is possible to include an image to a Java file. Just create
> a tool which will take an image as input and write a fragment of Java
> source as byte[] image = {...<contents of the image>...} ;
>
> That was very common one when we wrote programs in C/C++ and it should
> work now too.
>
>
<http://www.doctoroz.com/videos/secret-memory-enhancers>
Plus may help outside the immediate programming environment.
[toc] | [prev] | [next] | [standalone]
| From | Jeff Higgins <jeff@invalid.invalid> |
|---|---|
| Date | 2014-06-24 10:49 -0400 |
| Message-ID | <loc3g8$vao$1@dont-email.me> |
| In reply to | #28838 |
On 06/24/2014 08:54 AM, Donkey Hottie wrote:
> Of course it is possible to include an image to a Java file. Just create
> a tool which will take an image as input and write a fragment of Java
> source as byte[] image = {...<contents of the image>...} ;
>
> That was very common one when we wrote programs in C/C++ and it should
> work now too.
>
I purchased TurboCAD some years ago. They compiled in their icons.
This added several minutes to the process of swiping the icons.
I suspect the OP has obfuscation rather than the pain of CRS as motive.
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2014-06-25 01:25 -0700 |
| Message-ID | <ul1lq9p0l59ich9kp03cfgcfggil1bue0b@4ax.com> |
| In reply to | #28838 |
On Tue, 24 Jun 2014 15:54:48 +0300, Donkey Hottie
<donkey@fredriksson.dy.fi> wrote, quoted or indirectly quoted someone
who said :
>Of course it is possible to include an image to a Java file. Just create
>a tool which will take an image as input and write a fragment of Java
>source as byte[] image = {...<contents of the image>...} ;
I don't know if they have fixed this, but the byte code for
initialising a byte array used to be very bulky. It amounted to
load literal , store in array[ i], load literal, store in array [i+1].
In contrast a PNG stored as a resource will by one byte per byte then
zip compressed.
--
Roedy Green Canadian Mind Products http://mindprod.com
Why program by hand in 5 days what you can spend
2 to 5 years of your life automating.
~ Terence Parr 1964-08-17
[toc] | [prev] | [next] | [standalone]
| From | Marcel Müller <news.5.maazl@spamgourmet.org> |
|---|---|
| Date | 2014-06-23 18:25 +0200 |
| Message-ID | <53a854eb$0$6667$9b4e6d93@newsspool3.arcor-online.net> |
| In reply to | #28821 |
On 23.06.14 18.02, www wrote:
> I have a simple Java class: Abc.java. The method within it needs to access a small PNG file(it's an icon picture). Right now, this picture file("abc.png") is located at nonsrc/icons/abc.png
You are seeking for embedded resources.
getResourceAsStream is your friend.
Marcel
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2014-06-23 13:03 -0400 |
| Message-ID | <lo9mla$gkm$1@dont-email.me> |
| In reply to | #28821 |
On 6/23/2014 12:02 PM, www wrote:
> Hi:
>
> I have a simple Java class: Abc.java. The method within it needs to access a small PNG file(it's an icon picture). Right now, this picture file("abc.png") is located at nonsrc/icons/abc.png
>
> So this class is a bit pain. Abc.class won't work in the absence of abc.png. When I create an executable JAR, this is pain: always need to remember to include abc.png file inside it.
>
> I am wondering if there is a way that I can "convert" abc.png into some sort of Java code and I can put it in Abc.java. So Abc.class alone can perform the job, without the need of presence of abc.png. Is that possible?
>
> Hope I have explained my intention well.
Martin Gregorie's response seems to be good advice. However, if
you simply *must* incorporate your PNG image as a class, you could do
so with a big byte[] array:
private static final byte[] PNG_BUFFER = {
(byte)0x89, 0x50, 0x4E, 0x47, ... };
public static Image getMyPNG() {
return ImageIO.read(new ByteArrayInputStream(PNG_BUFFER));
}
However, this approach will only work for fairly small images. The
problem is that Java class files have no way to store an initialized
array: The byte[] array above actually compiles to the equivalent of
private static final byte[] PNG_BUFFER = new byte[whatever];
static {
PNG_BUFFER[0] = (byte)0x89;
PNG_BUFFER[1] = 0x50;
PNG_BUFFER[2] = 0x4E;
PNG_BUFFER[3] = 0x47;
...
}
... with about four or so bytecode instructions per array element.
At that rate of bloat, even a modest-sized image risks generating
more code than a Java method can hold.
You might evade the limits by writing the initialization explicitly
and breaking it up into multiple methods:
private static final byte[] PNG_BUFFER = new byte[whatever];
static {
firstK();
secondK();
...
}
private static void firstK() {
PNG_BUFFER[0] = (byte)0x89;
PNG_BUFFER[1] = 0x50;
PNG_BUFFER[2] = 0x4E;
PNG_BUFFER[3] = 0x47;
...
PNG_BUFFER[1023] = whatever;
}
private static void secondK() {
PNG_BUFFER[1024] = whatever;
...
PNG_BUFFER[2047] = whatever;
}
...
IMHO, no sane person would do this.
--
Eric Sosman
esosman@comcast-dot-net.invalid
[toc] | [prev] | [next] | [standalone]
| From | Wanja Gayk <brixomatic@yahoo.com> |
|---|---|
| Date | 2014-06-29 11:14 +0200 |
| Message-ID | <MPG.2e19f7339c6cdbfe20@202.177.16.121> |
| In reply to | #28824 |
In article <lo9mla$gkm$1@dont-email.me>, Eric Sosman (esosman@comcast-
dot-net.invalid) says...
>
> On 6/23/2014 12:02 PM, www wrote:
> > Hi:
> >
> > I have a simple Java class: Abc.java. The method within it needs to access a small PNG file(it's an icon picture). Right now, this picture file("abc.png") is located at nonsrc/icons/abc.png
> >
> > So this class is a bit pain. Abc.class won't work in the absence of abc.png. When I create an executable JAR, this is pain: always need to remember to include abc.png file inside it.
> >
> > I am wondering if there is a way that I can "convert" abc.png into some sort of Java code and I can put it in Abc.java. So Abc.class alone can perform the job, without the need of presence of abc.png. Is that possible?
> >
> > Hope I have explained my intention well.
>
> Martin Gregorie's response seems to be good advice. However, if
> you simply *must* incorporate your PNG image as a class, you could do
> so with a big byte[] array:
>
> private static final byte[] PNG_BUFFER = {
> (byte)0x89, 0x50, 0x4E, 0x47, ... };
>
> public static Image getMyPNG() {
> return ImageIO.read(new ByteArrayInputStream(PNG_BUFFER));
> }
>
> However, this approach will only work for fairly small images. The
> problem is that Java class files have no way to store an initialized
> array: The byte[] array above actually compiles to the equivalent of
>
> private static final byte[] PNG_BUFFER = new byte[whatever];
> static {
> PNG_BUFFER[0] = (byte)0x89;
> PNG_BUFFER[1] = 0x50;
> PNG_BUFFER[2] = 0x4E;
> PNG_BUFFER[3] = 0x47;
> ...
> }
>
> ... with about four or so bytecode instructions per array element.
Just wondering: why not use a string?
private static final String imageAsBase64
= "SOME_BASE64_STRING_GOES_HERE";
public static Image getMyPNG() {
return ImageIO.read(
new ByteArrayInputStream(
SomeUtil.base64ToBytes(imageAsBase64)));
}
Of course Base64 is notoriously inefficient (there's still Base85
though), but he's talking about "small PNG files", not large ones.
One might also like to read this here:
http://www.javaworld.com/article/2077481/learn-java/java-tip-117--
transfer-binary-data-in-an-xml-document.html
Kind regards,
Wanja
--
..Alesi's problem was that the back of the car was jumping up and down
dangerously - and I can assure you from having been teammate to
Jean Alesi and knowing what kind of cars that he can pull up with,
when Jean Alesi says that a car is dangerous - it is. [Jonathan Palmer]
--- news://freenews.netfront.net/ - complaints: news@netfront.net ---
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2014-06-29 08:15 -0400 |
| Message-ID | <lop01n$7k6$1@dont-email.me> |
| In reply to | #28867 |
On 6/29/2014 5:14 AM, Wanja Gayk wrote:
> In article <lo9mla$gkm$1@dont-email.me>, Eric Sosman (esosman@comcast-
> dot-net.invalid) says...
>>
>> On 6/23/2014 12:02 PM, www wrote:
>>> Hi:
>>>
>>> I have a simple Java class: Abc.java. The method within it needs to access a small PNG file(it's an icon picture). Right now, this picture file("abc.png") is located at nonsrc/icons/abc.png
>>>
>>> So this class is a bit pain. Abc.class won't work in the absence of abc.png. When I create an executable JAR, this is pain: always need to remember to include abc.png file inside it.
>>>
>>> I am wondering if there is a way that I can "convert" abc.png into some sort of Java code and I can put it in Abc.java. So Abc.class alone can perform the job, without the need of presence of abc.png. Is that possible?
>>>
>>> Hope I have explained my intention well.
>>
>> Martin Gregorie's response seems to be good advice. However, if
>> you simply *must* incorporate your PNG image as a class, you could do
>> so with a big byte[] array:
>>
>> private static final byte[] PNG_BUFFER = {
>> (byte)0x89, 0x50, 0x4E, 0x47, ... };
>>
>> public static Image getMyPNG() {
>> return ImageIO.read(new ByteArrayInputStream(PNG_BUFFER));
>> }
>>
>> However, this approach will only work for fairly small images. The
>> problem is that Java class files have no way to store an initialized
>> array: The byte[] array above actually compiles to the equivalent of
>>
>> private static final byte[] PNG_BUFFER = new byte[whatever];
>> static {
>> PNG_BUFFER[0] = (byte)0x89;
>> PNG_BUFFER[1] = 0x50;
>> PNG_BUFFER[2] = 0x4E;
>> PNG_BUFFER[3] = 0x47;
>> ...
>> }
>>
>> ... with about four or so bytecode instructions per array element.
>
> Just wondering: why not use a string?
>
> private static final String imageAsBase64
> = "SOME_BASE64_STRING_GOES_HERE";
>
> public static Image getMyPNG() {
> return ImageIO.read(
> new ByteArrayInputStream(
> SomeUtil.base64ToBytes(imageAsBase64)));
> }
>
> Of course Base64 is notoriously inefficient (there's still Base85
> though), but he's talking about "small PNG files", not large ones.
>[...]
I'd thought about using something like
private static final String PNG_BUFFER =
"\u8950\u4E47...";
... which could then be unpacked at run time into a byte[] of twice
as many elements and used as before. But that seemed so foully ugly
that I had to suppress the urge to vomit, so I didn't mention it.
Base64 simply didn't occur to me, and when I saw it mentioned
by others I said "Duh!" and delivered a self-administered dope slap.
--
Eric Sosman
esosman@comcast-dot-net.invalid
[toc] | [prev] | [next] | [standalone]
| From | markspace <markspace@nospam.nospam> |
|---|---|
| Date | 2014-06-23 10:39 -0700 |
| Message-ID | <lo9oob$18c$1@dont-email.me> |
| In reply to | #28821 |
On 6/23/2014 9:02 AM, www wrote:
> I have a simple Java class: Abc.java. The method within it needs to
> access a small PNG file(it's an icon picture). Right now, this
> picture file("abc.png") is located at nonsrc/icons/abc.png
^^^^^^
> I am wondering if there is a way that I can "convert" abc.png into
> some sort of Java code and I can put it in Abc.java. So Abc.class
> alone can perform the job, without the need of presence of abc.png.
> Is that possible?
I think Marcel has the right of it. You should be using getResource()
(or getResourceAsStream()) to load this file.
However the basic problem is that you need to include the file. You
should be using a build script that automatically copies the file. Most
IDEs will do this *if you put the image in the source directory.*
I see here you've deliberately excluded the image from the source.
That's normally wrong. Try something like:
src/my/stuff/Abc.java
src/my/stuff/abc.png
Most build system will now automatically figure out that you want the
PNG file to be included in the Jar file, and do the copy for you. If
you're using entirely home-rolled tools, you'll have to do the copying
yourself, but at least add it to an automated build script so you don't
have to do the copy manually each time.
Notice on this page how they do that exact thing just by calling
getResource():
http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html
[toc] | [prev] | [next] | [standalone]
| From | Jeff Higgins <jeff@invalid.invalid> |
|---|---|
| Date | 2014-06-23 15:04 -0400 |
| Message-ID | <lo9u3i$cmr$1@dont-email.me> |
| In reply to | #28821 |
On 06/23/2014 12:02 PM, www wrote:
> Hi:
>
> I have a simple Java class: Abc.java. The method within it needs to access a small PNG file(it's an icon picture). Right now, this picture file("abc.png") is located at nonsrc/icons/abc.png
>
> So this class is a bit pain. Abc.class won't work in the absence of abc.png. When I create an executable JAR, this is pain: always need to remember to include abc.png file inside it.
>
> I am wondering if there is a way that I can "convert" abc.png into some sort of Java code and I can put it in Abc.java. So Abc.class alone can perform the job, without the need of presence of abc.png. Is that possible?
>
> Hope I have explained my intention well.
Here is a string that base64 encodes a 16 x 16 "winged coffee bean" png
image.
Thanks to Canadian Mind Products
<http://mindprod.com/jgloss/duke.html>
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAACo0lEQVR42mNscOP//5+R8Qs
DIyMjMxMTKwPDP5Yvv/gfOkbnT/ZMLOxnIAAY7y8V+Q9mMDL8Z2L6/+3Xb6FPf1UmpcxvyFnX
uu0mNzMzy1+8BvwH2j852/P227snlJmZGRiYmBgY4mvbC2Y197c2b7nFQ9AFIOL/v3/Mj1aK/
IEJsnAJPrv0IoyFlVcrl/HHRyFNO+fdUiqad3EaAAMTM9zOfXh4Rs/Cw2DfxrknrQX/feViAH
rwxmfhl5XzloabuHgcxGsACNy9dUe+1s/+gLvmUwUFJQYGvz4GhjQ1BgZ+OaXrBWsvmlzetyac
R0Dwna5DwEasBlQlxszhurEk2dYKonlaGAPDh7cMDI/e83z5/Ps/k2923pdjm9dy//79l8U+Iq
cSw4BAPeXrBa53NUCaNxUxMPz6wcCwbhsDg5KB7FM9B9VrOgoPtf9JlHR9+6dwe25t5hQMA9ob
O3Lb6ysnbC7+z3T/IQPDrkMMDGxqtnuqG7XuL+tZnZQQx8C8eOk/hohw0efLlr8RRzHgxbV10Z
vnzpl44vLFz3ev/OD4//s3u4aty7aWmXOyHuxrbzq2/6JdgP0Zw4WL/zEkxDF/XbCIgRXFgPYA
jv8RpZ354krWRz8xKd75/fsnu6yM1CuQ3LcvX7i5eHi+Plwu9H/egn8MifEs/+cv+vsNbMDjSy
tjX56qmyAtLyvw9NHTf2ziYZ26njVNjMysv9C9CEozk3O8bvpan1FesOjPF8bLu3urTu/c2eob
6dI0p7murnLDD0aQSwKKZyRq2iYswJZ4QKn3wTLRfwsX/mZg3F4r9d82Z5cTj7jO/re39jvMLv
fYp+5c2eAWkzmTW0D8JcGkPLfY7X9I6WQNfgn1m4QUYzVg/ZSGunf3jzUm9+5iJMcAAJSpGEl+
q1AjAAAAAElFTkSuQmCC
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2014-06-25 01:34 -0700 |
| Message-ID | <3t1lq9pa75c5oh9uv6lj50345mahrf5cm6@4ax.com> |
| In reply to | #28826 |
On Mon, 23 Jun 2014 15:04:59 -0400, Jeff Higgins <jeff@invalid.invalid> wrote, quoted or indirectly quoted someone who said : >Here is a string that base64 encodes a 16 x 16 "winged coffee bean" png >image. >Thanks to Canadian Mind Products ><http://mindprod.com/jgloss/duke.html> for a base64 encoder/decoder see http://mindprod.com/products1.html#BASE64 If you don't like it, find others via http://mindprod.com/jgloss/base64.html I think those will be stored in the class file as UTF-8 strings So you will take taking roughly 1 byte per char. Base64 encodes each 8 bits in 6 and "wastes" 2 bits. Better than hex that encodes each byte in 2 chars. I would think an encoding for resources would roughly first compress, then encode as a base n number where n is the number of permitable convenient characters placed between "..." for a String initialisation. then fluff it up with a custom getResource. -- Roedy Green Canadian Mind Products http://mindprod.com Why program by hand in 5 days what you can spend 2 to 5 years of your life automating. ~ Terence Parr 1964-08-17
[toc] | [prev] | [next] | [standalone]
| From | Joerg Meier <joergmmeier@arcor.de> |
|---|---|
| Date | 2014-06-23 21:15 +0200 |
| Message-ID | <1brhcp5gdqr4.1ah31etxt4x8u.dlg@40tude.net> |
| In reply to | #28821 |
On Mon, 23 Jun 2014 09:02:20 -0700 (PDT), www wrote:
> I have a simple Java class: Abc.java. The method within it needs to access a small PNG file(it's an icon picture). Right now, this picture file("abc.png") is located at nonsrc/icons/abc.png
> So this class is a bit pain. Abc.class won't work in the absence of abc.png. When I create an executable JAR, this is pain: always need to remember to include abc.png file inside it.
> I am wondering if there is a way that I can "convert" abc.png into some sort of Java code and I can put it in Abc.java. So Abc.class alone can perform the job, without the need of presence of abc.png. Is that possible?
> Hope I have explained my intention well.
This is obviously a horrible idea. As others have said, you should learn
how to use a build tool. That being said, who doesn't like a challenge ?
The posted solutions here are pretty horrible too, so I made this
(apologies for line length):
public class EmbedFile {
public static void main(String[] args) throws IOException {
byte[] bytes = IOUtils.toByteArray(new FileInputStream(args[0]));
String data[] = (" String data = \"" +
DatatypeConverter.printBase64Binary(bytes) + "\";")
.split("(?<=\\G.{83})");
StringBuilder output = new StringBuilder();
output.append(data[0] + "\n");
for (int i = 1; i < data.length; i++) {
output.append(" + \"" + data[i] + "\"\n");
}
System.out.println(output.toString());
}
}
Feeding it with a binary file as parameter 1 produces an output such as:
String data =
"iVBORw0KGgoAAAANSUhEUgAAAMgAAACWCAMAAACsAjcrAAAAAXNSR0IArs4c6QAA
+"AARnQU1BAACxjwv8YQUAAAMAUExURRcaGRoaFhkZGRkaHBkcGhodHRwZGhwbHB4cGR0dHR0fIBwgHh4hICI"
+"eHSIjIyYpKS4nJCssLCwuMC4xMTQvLjIxLzIyMjU3ODU4NzY4ODc8Ojc9PTk2NTk6Ojo6PDk8Ozo9PTw+Pj"
...
which increases the effective size of the class file by 1.6x the size of
the binary file you want to include - a far cry from
Liebe Gruesse,
Joerg
--
Ich lese meine Emails nicht, replies to Email bleiben also leider
ungelesen.
[toc] | [prev] | [next] | [standalone]
| From | Jeff Higgins <jeff@invalid.invalid> |
|---|---|
| Date | 2014-06-23 15:46 -0400 |
| Message-ID | <loa0gi$vp5$1@dont-email.me> |
| In reply to | #28827 |
On 06/23/2014 03:15 PM, Joerg Meier wrote:
> This is obviously a horrible idea. As others have said, you should learn
> how to use a build tool. That being said, who doesn't like a challenge ?
> The posted solutions here are pretty horrible too, so I made this
> (apologies for line length):
JDK 8 has java.util.Base64, that will make your example different.
>
> public class EmbedFile {
> public static void main(String[] args) throws IOException {
> byte[] bytes = IOUtils.toByteArray(new FileInputStream(args[0]));
> String data[] = (" String data = \"" +
> DatatypeConverter.printBase64Binary(bytes) + "\";")
> .split("(?<=\\G.{83})");
> StringBuilder output = new StringBuilder();
> output.append(data[0] + "\n");
> for (int i = 1; i < data.length; i++) {
> output.append(" + \"" + data[i] + "\"\n");
> }
> System.out.println(output.toString());
> }
> }
[toc] | [prev] | [next] | [standalone]
| From | Joerg Meier <joergmmeier@arcor.de> |
|---|---|
| Date | 2014-06-23 22:08 +0200 |
| Message-ID | <8gz583tqrh2g.1eh9m8c46qwfn.dlg@40tude.net> |
| In reply to | #28828 |
On Mon, 23 Jun 2014 15:46:05 -0400, Jeff Higgins wrote: > On 06/23/2014 03:15 PM, Joerg Meier wrote: >> This is obviously a horrible idea. As others have said, you should learn >> how to use a build tool. That being said, who doesn't like a challenge ? >> The posted solutions here are pretty horrible too, so I made this >> (apologies for line length): > JDK 8 has java.util.Base64, that will make your example different. Is there any reason to use that over the already existing DatatypeConverter .printBase64Binary ? Liebe Gruesse, Joerg -- Ich lese meine Emails nicht, replies to Email bleiben also leider ungelesen.
[toc] | [prev] | [next] | [standalone]
| From | Jeff Higgins <jeff@invalid.invalid> |
|---|---|
| Date | 2014-06-23 16:19 -0400 |
| Message-ID | <loa2ej$gle$1@dont-email.me> |
| In reply to | #28829 |
On 06/23/2014 04:08 PM, Joerg Meier wrote:
> On Mon, 23 Jun 2014 15:46:05 -0400, Jeff Higgins wrote:
>
>> On 06/23/2014 03:15 PM, Joerg Meier wrote:
>>> This is obviously a horrible idea. As others have said, you should learn
>>> how to use a build tool. That being said, who doesn't like a challenge ?
>>> The posted solutions here are pretty horrible too, so I made this
>>> (apologies for line length):
>> JDK 8 has java.util.Base64, that will make your example different.
>
> Is there any reason to use that over the already existing DatatypeConverter
> .printBase64Binary ?
Not so much DatatypeConverter, but
Apache Commons IO IOUtils means another lib.
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Base64;
public class PrintFile {
public static void main(String[] args)
throws IOException {
File file = new File(args[0]);
try (FileInputStream input =
new FileInputStream(file)) {
byte[] src = new byte[(int) file.length()];
input.read(src);
System.out.println(
Base64.getMimeEncoder().encodeToString(src));
}
}
}
[toc] | [prev] | [next] | [standalone]
| From | Joerg Meier <joergmmeier@arcor.de> |
|---|---|
| Date | 2014-06-23 22:34 +0200 |
| Message-ID | <qb3f7ocf36y6$.bh0a27zj8v9q$.dlg@40tude.net> |
| In reply to | #28830 |
On Mon, 23 Jun 2014 16:19:08 -0400, Jeff Higgins wrote: > On 06/23/2014 04:08 PM, Joerg Meier wrote: >> On Mon, 23 Jun 2014 15:46:05 -0400, Jeff Higgins wrote: >>> On 06/23/2014 03:15 PM, Joerg Meier wrote: >>>> This is obviously a horrible idea. As others have said, you should learn >>>> how to use a build tool. That being said, who doesn't like a challenge ? >>>> The posted solutions here are pretty horrible too, so I made this >>>> (apologies for line length): >>> JDK 8 has java.util.Base64, that will make your example different. >> Is there any reason to use that over the already existing DatatypeConverter >> .printBase64Binary ? > Not so much DatatypeConverter, but > Apache Commons IO IOUtils means another lib. I don't see how java.util.Base64 relates to my use of Apache Commons. I must admit that I am terribly unfamiliar with the "primitive" Java APIs in regards to doing things like loading files, and I didn't know off hand if FIS.read could be used that easily or if more care had to be taken. A short trip to Google showed examples where people double checked read byte count, read in small increases and all kinds of nonsense I didn't want to worry about in a simple example. As this code was only to be used to generate the initial String, I figured the additional lib dependency didn't really matter - although with respect to OPs admitted lack of proper built tool that probably was incorrect. Liebe Gruesse, Joerg -- Ich lese meine Emails nicht, replies to Email bleiben also leider ungelesen.
[toc] | [prev] | [next] | [standalone]
| From | Jeff Higgins <jeff@invalid.invalid> |
|---|---|
| Date | 2014-06-23 16:43 -0400 |
| Message-ID | <loa3s7$ria$1@dont-email.me> |
| In reply to | #28831 |
On 06/23/2014 04:34 PM, Joerg Meier wrote:
> I don't see how java.util.Base64 relates to my use of Apache Commons.
It doesn't.
It relates to:
String data[] = (" String data = \"" +
DatatypeConverter.printBase64Binary(bytes) + "\";")
.split("(?<=\\G.{83})");
StringBuilder output = new StringBuilder();
output.append(data[0] + "\n");
for (int i = 1; i < data.length; i++) {
output.append(" + \"" + data[i] + "\"\n");
}
vs
Base64.getMimeEncoder().encodeToString(src)
Sorry, just didn't want to bring the "H" word into the mix. :)
[toc] | [prev] | [next] | [standalone]
| From | Joerg Meier <joergmmeier@arcor.de> |
|---|---|
| Date | 2014-06-24 00:21 +0200 |
| Message-ID | <sntb9phi3doj$.1tpbvdvu8f1oj$.dlg@40tude.net> |
| In reply to | #28833 |
On Mon, 23 Jun 2014 16:43:28 -0400, Jeff Higgins wrote:
> On 06/23/2014 04:34 PM, Joerg Meier wrote:
>> I don't see how java.util.Base64 relates to my use of Apache Commons.
> It doesn't.
> It relates to:
> String data[] = (" String data = \"" +
> DatatypeConverter.printBase64Binary(bytes) + "\";")
> .split("(?<=\\G.{83})");
> StringBuilder output = new StringBuilder();
> output.append(data[0] + "\n");
> for (int i = 1; i < data.length; i++) {
> output.append(" + \"" + data[i] + "\"\n");
> }
> vs
> Base64.getMimeEncoder().encodeToString(src)
> Sorry, just didn't want to bring the "H" word into the mix. :)
Well now, let's be fair - it's:
DatatypeConverter.printBase64Binary(bytes)
vs
Base64.getMimeEncoder().encodeToString(src)
The rest is just formatting so one can copy and paste the output directly
into a java source file.
Not sure what "H" word you are referring to.
Liebe Gruesse,
Joerg
--
Ich lese meine Emails nicht, replies to Email bleiben also leider
ungelesen.
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.java.programmer
csiph-web