Link to home
Start Free TrialLog in
Avatar of cgray1223
cgray1223

asked on

Convert Java byte[] to String[]

Hello,

I'm trying to decide if I should expose two api methods in Java or just one.  First case, I have one method that returns a byte[] given a set of parameters and then one that returns a String[] given the same set of parameters.  Second case, I only have one method that always return a byte[].  I feel that the String[] would me useful to consumers of the API, but they could always convert the byte[] to a String[].  Thoughts?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>> but they could always convert the byte[] to a String[].  Thoughts?

That only works when the contents are printable, non-control chars. Are they?
In general it depends on many deatils of your sitaution but if theyultimately need strings in most cases better to have String returned.
Do you mean String[] corresopodnding to byte[] , or just one String corresponding to byte[] ?
Avatar of cgray1223
cgray1223

ASKER

The purpose of the API is to wrap (via JNI) a C API.  On the C side it just returns a pointer to the data...it can either be what amounts to an array of Strings or bytes depending on what is passed in (enum).  The consumers need strings 90% of the time and byte[] (to handle a blob return type) 10% of the time.  I was leaning towards creating two methods (fetchDataAsStringArray and fetchDataAsByteArray), but wasn't sure if that was proper.  Another option would be to just have fetchDataAsArray and always return a byte array and have the consumer do what they want, but then the downside is converting that to a String array for 90% of the cases...
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
(iow the exact opposite of what for_yan just said ;)) If you do give them String, be prepared for garbage-looking results (due to encoding/printability problems) and your users saying you have 'bugs'
90% of the return values will be nothing but ["abc", "ked"...ect], but if the user says give me the blob data, its just all byte data that wont make sense as String data.  So the "give me the blob data" request wont make sense as a String so I should create an asStringArray and asByteArray.  I can add error handling if they request they request blob data from the asStringArray method.
>>request wont make sense as a String so I should create an asStringArray and asByteArray

You could give that a try but it could still cause problems
Yes, that would be quite reasonable
No doubt it is easier for you to handle any problems and deal with them gracefully than for the user of the API,
as you know all ins and outs of it.