Java Ninja Chronicles By Norris Shelton

Things I learned in the pursuit of code

This seems like a simple thing, but I rarely have the need for a byte[]. Even then, when I do, it is usually an array of binary data and there is no proper String representation of it.

I recently had a case where I had an array of bytes that was stored in the database, but it was actually String data. I started with a person object with a byte[] that I needed to assign into kycModel as a String:

kycModel.setSsnLast4(person.getSsnLast4());

IntelliJ complained that I couldn’t assign a byte[] into a String and suggested I wrap it with a String.valueOf.

kycModel.setSsnLast4(String.valueOf(person.getSsnLast4()));

That still wasn’t good enough. IntelliJ was complaining that there was an implicit call to toString on the byte array. The code compiled correctly, but didn’t work. After a little scouring of the internet I found my solution. String has a constructor that takes a byte[].

kycModel.setSsnLast4(new String(person.getSsnLast4()));

One note about that implementation is that it decodes the byte[] by using the default character set. In Java, this is UTF-8. If you need to specify a characterset, you can use another constructor that takes that value, like the following. Please note, that if you use the constructor that allows you to specify a characterset, then you must also catch UnsupportedEncodingException.

try {
    kycModel.setSsnLast4(new String(person.getSsnLast4(), "UTF-8"));
} catch (UnsupportedEncodingException e) {
    logger.error("", e);
    throw e;
}

September 16th, 2015

Posted In: Java, java ninja, Javaninja

Tags: , ,

Leave a Comment

The usual usage of a Springframework RestTemplate is to get back an object. Sometimes the object will contain a list. What needs to happen when you need to get back a list of Strings? RestTemplate accepts a parameter that tells it what the data it receives will be put in. I used a String array. It converts the JSON object into a String[] and away I go.

                    String[] pokerTableNames =
                        restTemplate.getForObject(pokerSettingsService.getValueByName(PokerSettings.IGP_ADAPTER_URL) + "/poker/tableNames",
                            String[].class);

February 20th, 2015

Posted In: Java, java ninja, Javaninja, json, Spring

Tags: , , , , , , ,

Leave a Comment

I had a need to remove duplicates from a pre-existing Java List. I tried:

ids = new LinkedList<WatchedId>(new LinkedHashSet<WatchedId>(ids));

Seems easy enough, but the list kept returning no elements. I started clicking through the code and found the LinkedList constructor that takes a collection eventually calls

LinkedList.addAll(int size, Collection)

That method has the following line

Object[] a = c.toArray();

The implementation was in AbstractCollection.toArray(). I stepped through the code and I saw that the nodes were not being moved. Maybe that is a Mac JVM bug? I changed the type to an ArrayList and it worked. Maybe it has something to do with going from a double-linked backing object to a double-linked backing object.

ids = new ArrayList<WatchedId>(new LinkedHashSet<WatchedId>(ids));

September 27th, 2012

Posted In: Collections, Java

Tags: , , , , , , , ,

Leave a Comment

LinkedIn Auto Publish Powered By : XYZScripts.com