Byte array

  • Thread starter Dirk Bruere at NeoPax
  • Start date
M

Mike Schilling

Dirk said:
Next problem I've got is that I get the message: "code too large"
when I try to compile. Have I got to break up the byte array(s) and
put them in separate constants classes?

The "too large" probably refers to your method. Break up the
intiialization into multiple methods, and write a master method that
calls all of them.
 
M

markspace

Dirk said:
Next problem I've got is that I get the message: "code too large" when I
try to compile. Have I got to break up the byte array(s) and put them in
separate constants classes?


So, copying and pasting a 20k byte buffer turned out to be a bad idea,
just like everyone said? Imagine that.

Read the file as input, make the byte buffer from that. I think you're
done with the god-awful kludges.
 
J

Joshua Cranmer

Dirk said:
Next problem I've got is that I get the message: "code too large" when I
try to compile. Have I got to break up the byte array(s) and put them in
separate constants classes?

A method is limited to 65,536 method length. The brunt of the array
would be a dup, [iconst_n | bipush | sipush | ldc], bipush, bastore. If
it's a class field, that would also include an sipush, anewarray,
aload_0, setfield, and a return: for a total of 10 bytes meta.

You have 6 5-byte pushes, 122 6-byte pushes, and the rest (until 32768)
7-byte, assuming that you don't have numbers below 6. Doing the math,
that gets you a total of 9,380 elements before you exceed method length.
 
D

Dirk Bruere at NeoPax

D

Daniel Pitts

Dirk said:
It's going to be just as hard to put them in a file and read it as hard
coding them in from the start. I'll make an int array and then convert
to bytes as I read from it.

Not so hard:
class DataClass {
private static final byte[] data;
static {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream stream = DataClass.class.getResourceAsStream("d.data");
int read;
while ((read = stream.read()) >= 0) {
out.write(read);
}
stream.close();
data = out.toByteArray();
} catch(Exception e) {
throw new RuntimeException("Could not load data");
}
}
}
 
L

Lew

Daniel said:
int read;
while ((read = stream.read()) >= 0) {
out.write(read);
}

Or, for better control of the scope of variable 'read':

for ( int read; (read = stream.read()) >= 0; )
{
out.write(read);
}
 
D

Dirk Bruere at NeoPax

Daniel said:
Dirk said:
It's going to be just as hard to put them in a file and read it as
hard coding them in from the start. I'll make an int array and then
convert to bytes as I read from it.

Not so hard:
class DataClass {
private static final byte[] data;
static {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream stream = DataClass.class.getResourceAsStream("d.data");
int read;
while ((read = stream.read()) >= 0) {
out.write(read);
}
stream.close();
data = out.toByteArray();
} catch(Exception e) {
throw new RuntimeException("Could not load data");
}
}
}

Thanks - I'll use it to tidy up the code.
I love the way a simple question turns into a huge discussion :)

--
Dirk

http://www.transcendence.me.uk/ - Transcendence UK
http://www.theconsensus.org/ - A UK political party
http://www.onetribe.me.uk/wordpress/?cat=5 - Our podcasts on weird stuff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Official Java Classes 10
Can an Applet beep? 4
ListModel name 10
Accessing static field 21
Free keyboard applet 5
Substring 53
Java in Java 10
Sorting a JList 4

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top