Reputation: 6751
I'm using CXF to implement a web services server.
Since I'm low on memory I don't want the web service call parameters to be translated strings which are UTF-16 I rather access the original UTF-8 buffers which are usually half in size in my case.
So if I have a web method:
void addBook(String bookText)
How can I get the bookText without CXF translating it to java string?
Upvotes: 1
Views: 94
Reputation: 14607
The XML parsers used in Java (StAX parsers for CXF) only allow getting the XML contents as either a String or char[]. Thus, it wouldn't be possible to get the raw bytes.
Upvotes: 1
Reputation: 418505
If you have a String
object in java, there is no such thing as whether it is UTF-8 or UTF-16 string. The encoding comes in when you convert a String
to or from a byte array.
A String
in java is a character array. If you already have a String
object in java (for example passed as a parameter to your addBook()
method, it has already been interpreted properly and converted to a character array.
If you want to avoid character encoding conversions, the only way to do that is to define your method to receive a byte array instead of a String
:
void addBook(byte[] bookTextUtf16);
But keep in mind that in this way you have to "remember" the encoding in which the byte array is valid (adding it to the name is one way).
If you need a java.lang.String
object, then there is nothing you can do. A String
is a character array, characters with which each being a 16-bit value. This is String
internal, no way to change the internal representation. Either accept this or don't use java.lang.String
to represent your strings.
An alternative way could be to create your own Text
class for example which honds the UTF-8 encoded byte array for example, and as long as you don't need the String
representation, keep it as a byte array and store it if you want to. Only create the java.lang.String
instance when you do need the String
.
Upvotes: 0