As a reference from the https://stackoverflow.com/a/2407930/8970520 It was told to use properties file to extend the allocated size .But i couldn't understand how to do that.Could you please elaborate and explain how it works?Thanks in advance.
-
Why not make the method smaller? – Thorbjørn Ravn Andersen Jul 21 '20 at 12:07
-
I can't make the method smaller because it contain data – sandeysh Jul 21 '20 at 12:12
-
1data is not supposed to be inside the source code. Load it from an external resource, you already mentioned properties. – f1sh Jul 21 '20 at 12:13
-
Where can i write the data? I don't know how to mention properties.Can you explain how? – sandeysh Jul 21 '20 at 12:15
-
Split the data in multiple parts and combine them at runtime. – Thorbjørn Ravn Andersen Jul 21 '20 at 12:16
-
What kind of data do you have inside a method that makes the method exceed 64KB of bytecode? **Edit** the question and show example. – Andreas Jul 21 '20 at 12:20
-
Check this link.It has the method which i wrote.I am working on captcha.So i am loading data into an hashmap. https://onlinegdb.com/Hk9W08Egw – sandeysh Jul 21 '20 at 12:24
-
Wow, [that](https://onlinegdb.com/Hk9W08Egw) is just horrendous!!! Why would you store a **bit** as 1-character String objects? What a waste of memory. Each string object takes up *more than* 30 bytes of memory, to store one **bit**!!! – Andreas Jul 21 '20 at 12:29
-
@Andreas I changed them to boolean which stores 1 bit https://onlinegdb.com/Hk9W08Egw even then it shows code too large – sandeysh Jul 21 '20 at 13:02
-
That's the same link. – Andreas Jul 21 '20 at 13:03
-
http://tpcg.io/BRCfYHJU @Andreas here is the link – sandeysh Jul 21 '20 at 13:13
2 Answers
In this case, using the properties file to extend the size is the wrong approach. Some people have already pointed this out, but you really, really don't want to include data in the source code. If the data changes, you would have to recompile your application and do a re-deploy of your app.
If the data does not change frequently, you could consider an XML file or SQLite database. If the data changes, though, you'll definitely need a mechanism to update it (even if the data changes rarely). Perhaps consider a web service.
If the data changes frequently, you'll definitely want to consider a web service to download the data.

- 11,977
- 56
- 49
- 78
-
-
this is a good answer and i'm not disagreeing with this, but in the original link OP posted it also shows there that someone was doing relatively the same thing, so i don't know if this question is really any different to the one there, this might just be a duplicate entirely – a_local_nobody Jul 21 '20 at 12:28
-
@sandeysh In that case, I'd recommend a SQLite database or XML file. – EJoshuaS - Stand with Ukraine Jul 21 '20 at 12:38
Your code is like this:
public String[] checks(String qww){
Map<String, String[]> bitmaps = new HashMap<>();
String[] cs;
cs=new String[]{"1", "1", "1", "1", "1", ... 775 more bits ... };
bitmaps.put("a",cs);
cs=new String[]{"1", "1", "1", "1", "1", ... 775 more bits ... };
bitmaps.put("A",cs);
... 49 more entries ...
return bitmaps.get(qww);
}
That is just wrong on so many levels:
Storing bits as 1-character strings is a waste of memory, since it uses more than 30 bytes per string, just to store a bit.
At the very least, you could have stored the bit-characters in a
char
array, created from a single string, e.g.Map<String, char[]> bitmaps = new HashMap<>(); bitmaps.put("a", "11111 ... 775 more bits ...".toCharArray()); bitmaps.put("A", "11111 ... 775 more bits ...".toCharArray()); ... 49 more entries ... return bitmaps.get(qww);
The code builds up the entire
bitmaps
map on every call, then gets one of the map values and discards the map. Waste of processing time.Either build the
bitmaps
map once only, or use aswitch
statement instead:public char[] checks(String qww) { switch (qww) { case "a": return "11111 ... 775 more bits ...".toCharArray(); case "A": return "11111 ... 775 more bits ...".toCharArray(); ... 49 more entries ... default: return null; } }
That amount of data should be stored outside the code, e.g. in a property file.
When loaded into memory, the bits should be stored in a
BitSet
So, first create a property file, e.g. checks.bitmaps
. The name can be anything, it is just a text file. Content:
a = 111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111000000000111111110000000000000001111000000000000000001110000000000000000001100000000000000000001000111111110000000010111111111110000000011111111111110000000111100000000000000000000000000000000000000000000000000000000000000000000000000000000000111110000000000000111111110000000000001111111100000000000011111110000000000000111111000000000000000111100000000000000000000000000000000000000000000000000000000000000100000001000000000111000000011000000111110000000111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
A = 111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110000000011111111100000000000001111110000000000000000111100000000000000001111000001111100000001110011111111100000011101111111111100000111111111111111000001111111111111110000001111100000000000000011100000000000000000100000000000000000001000000000000000000000000000111111000000000000111111110000000000001111111100000000000011111110000000000000111111100000000000000111100000000010000000000000000000100000000000000000001100000000000100000011110000000111000000111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
... 49 more entries ...
Another advantage of this is that the data is so much easier to view and edit.
Change the code as follows:
class Main {
private static final Map<String, BitSet> bitmaps = new HashMap<>();
static {
try (InputStream inStream = Main.class.getResourceAsStream("checks.bitmaps")) {
Properties props = new Properties();
props.load(inStream);
for (String key : props.stringPropertyNames()) {
char[] bits = props.getProperty(key).toCharArray();
BitSet bitmap = new BitSet(bits.length);
for (int i = 0; i < bits.length; i++)
bitmap.set(i, bits[i] == '1');
bitmaps.put(key, bitmap);
}
} catch (Exception e) {
throw new IllegalStateException("Bitmap file failed to load: " + e, e);
}
}
public BitSet checks(String qww) {
return bitmaps.get(qww);
}
}
The caller can now check for 1-bits as follows:
BitSet bitmap = main.checks(qww);
if (bitmap.get(index)) {
// Bit is '1'
} else {
// Bit is '0'
}

- 154,647
- 11
- 152
- 247
-
-
@sandeysh **All 3!** --- 1) the bits are stored as a single string of 0's and 1's, 2) the `bitmaps` map is only built once, 3) the data for the `bitmaps` map is stored outside the class. --- So, **the second half of the answer**, that's why I spent time writing all that. – Andreas Jul 21 '20 at 13:20