Convert Java byte[] to String[]


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?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

>> 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[] ?
cgray1223Author Commented:
The purpose of the API is to wrap (via JNI) a C API.  On the C side it just returns a pointer to the 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...
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

No, if 90% they need Strings - better to have suxch method which returns String[]
You don't know hiow knowledgedable will be your users, and they may know something not in this area,
and with thie step which seems minor it may decreease usuage. I would have made both methods

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's not really a question of whether it's more convenient to give them String/byte[] or whether you supply two or more methods. The important thing is whether what you return can be properly represented as a String. If there is any doubt at all, byte[] should be what you use
(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'
cgray1223Author Commented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.