I would like to know which representation of a String (normal String or byte-array) would require more memory to be stored?
A byte-array representation is available to me in my code. Should I convert this to a String before transferring it over the network (in response to an AJAX call)?
1 => see: What is the Java's internal represention for String? Modified UTF-8? UTF-16?
2 => multiple options
if it's short, simply transfer a string if it's ascii, otherwise, convert it :
String serial= DatatypeConverter.printBase64Binary(bytes)
and you can use GET
decoding is possible with java, php, ...
if it's big, use POST, and binary (or native).
For 2: If it is a response to an AJAX request most probably you need to send an HTTP response and to send an HTTP response usually, in a Servlet for example, you use an OutputStream object (http://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html) or a PrintWriter and use write method to write the response.
If you use HttpServletResponse you may directly write a String with a PrintWriter
HttpServletResponse httpServletResponse;
...
String responseToClient = "HOLA";
httpServletResponse.getWriter().write(responseToClient);
If you use method getOutpuStream then you will obtain a SevletOutputStream and write method receives a byte array.
Related
In Netty, I create a response by feeding a String in body:
DefaultFullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, httpResponse.getHttpResponseStatus());
if (body != null) {
ByteBuf buf = Unpooled.copiedBuffer(body, CharsetUtil.UTF_8);
res.content().writeBytes(buf);
buf.release();
res.headers().set(HttpHeaderNames.CONTENT_LENGTH, res.content().readableBytes());
}
When I look at the response, I see content-length being twice the length of the characters in the String. I understand the Java String contains 2 bytes per character, but I can't figure out how to prevent this in Netty when returning the request.
When I look at Cloudflare responses, these contain one byte per character. So there must be a way to change this. Ideas?
As #Chris O'Toole shows in How to convert a netty ByteBuf to a String and vice versa we must
first convert the String to Byte Array using the desired charset (UTF-8 works fine) String.getBytes(Charset),
then Unpooled.wrappedBuffer(byte[]) using the Byte Array instead the
String.
One byte per character for most characters, as #rossum stated.
Use US_ASCII charset instead of UTF-8. Haven't tested, try.
I am working with the GCS API, attempting to create a survey with image data.
I am using the NuGet package Google.Apis.ConsumerSurveys.v2 version 1.14.0.564 on the .Net platform. I can create surveys that do not contain image data without problem. However, when I try to create a survey with image data I receive an error from the API.
I have on hand base64 encoded png format image data. My images display properly in an IMG tag on a web page when the src attribute is set to
'data:image/png;base64,<image base64 string>'
I want to send this image data to the API to populate the survey image. My understanding is that I need to set the Data property of the Google.Apis.ConsumerSurveys.v2.Data.SurveyQuestionImage object to a string containing the image data. I have not been successful.
I first decode my base64 string to a byte array:
byte[] bytes = Convert.FromBase64String(<image base64 string>);
I have tried setting the Data property in the SurveyQuestionImage object as:
image.Data = Encoding.Unicode.GetString(bytes);
This results in this error from the API:
Google.Apis.Requests.RequestError Invalid value for ByteString: <the Data string>
I have also tried converting the byte array to a hexadecimal encoded string as:
StringBuilder sb = new StringBuilder(bytes.Length);
foreach (Byte b in bytes)
{
sb.Append(b.ToString("X2"));
}
image.Data = sb.ToString();
This results in the more hopeful error:
Google.Apis.Requests.RequestError Invalid Value supplied to API: image_data was bad. Request Id: 579665c300ff05e6c316a09e600001737e3430322d747269616c320001707573682d30372d32322d72313000010112 [400] Errors [ Message[Invalid Value supplied to API: image_data was bad. Request Id: 579665c300ff05e6c316a09e600001737e3430322d747269616c320001707573682d30372d32322d72313000010112] Location[ - ] Reason[INVALID_VALUE] Domain[global] ]
Does anyone know the correct format for the Data property of the Google.Apis.ConsumerSurveys.v2.Data.SurveyQuestionImage object?
The data needs to be base64 encoded and also "urlsafe" or "websafe" depending on what language you are using. (python and java, respectively)
In other words, you'll need to first base64 encode then:
Web safe encoding uses '-' instead of '+', '_' instead of '/'
Hope this helps!
For c# users, check out this technique for making websafe b64:
How to achieve Base64 URL safe encoding in C#?
For .net users, look at the comments in this question:
Converting string to web-safe Base64 format
And also this link for more info about .net specific options for encoding:
http://www.codeproject.com/Tips/76650/Base-base-url-base-url-and-z-base-encoding
And to specifically answer the original poster, try this for converting your byte array to a string.
public static string ToBase64ForUrlString(byte[] input)
{
StringBuilder result = new StringBuilder(Convert.ToBase64String(input).TrimEnd('='));
result.Replace('+', '-');
result.Replace('/', '_');
return result.ToString();
}
When calling a servlet through a portlet, the javax.portlet.ResourceResponse.getPortletOutputStream() method of the portlet resource response returns a org.apache.pluto.container.util.PrintWriterServletOutputStream object.
(I am not sure whether this method does not return PrintWriterServerOutputStream always.)
PrintWriterServletOutputStream object writes an encoded string to output, instead of raw bytes as expected from a OutputStream object.
I want to modify this behavior to write raw bytes to the browser output.
Please let me know how can I achieve this?
I'm trying to read post data from a servelet doPost method the following way.
using httpservletrequest.getInputStream();
Creating an instance of bytearrayoutputstream.
writing the post data to bytearrayoutputstream from httpservletrequest.getInputStream();
Finally, output I'm getting from bytearrayoutputstream.toByteArray().
The problem with that is, when I enter 150/10 in the textfield, toByteArray gives me 150%2F10.
toByteArray seems to be encoding the special characters in the output for the / character.
What will be the elegant way to read post data from servlet doPost() method?
before you pass data to the servlet, try to encode it in client side. or else when you retrieve it, in the servlet, do a Url encode there.
try this way:
String abc = URLEncoder.encode("convert my string 150/10", "UTF-8");
byte[] barr = abc.getBytes(Charset.forName("UTF-8"));
use above with the
request.getParameter();
then you don't have to play with all these fancy codes. I guess when you convert into a byte array, you will have to specify the encoding mechanism to avoid char set ambiguity.
I did not try the above code, but would like you to have a try!
I need help. In my current development one of the requirements says:
The server will return 200-OK as a response(httpresponse).
If the panelist is verified then as a result, the server must also
return the panelist id of this panelist.
The server will place the panelist id inside the body of the 200-OK
response in the following way:
<tdcp>
<cmd>
<ack cmd=”Init”>
<panelistid>3849303</panelistid>
</ack>
</cmd>
Now I am able to put the httpresponse as
httpServletResponse.setStatus(HttpServletResponse.SC_OK);
And I can put
String responseToClient= "<tdcp><cmd><ack cmd=”Init”><panelistid>3849303</panelistid></ack></cmd></tdcp>";
Now what does putting the above xml inside the body of 200-OK response mean and how can it be achieved?
You can write the XML directly to the response as follows:
This example uses a ServletResponse.getWriter(), which is a PrintWriter to write a String to the response.
String responseToClient= "<tdcp><cmd><ack cmd=”Init”><panelistid>3849303</panelistid></ack></cmd></tdcp>";
httpServletResponse.setStatus(HttpServletResponse.SC_OK);
httpServletResponse.getWriter().write(responseToClient);
httpServletResponse.getWriter().flush();
You simply need to get the output stream (or output writer) of the servlet response, and write to that. See ServletResponse.getOutputStream() and ServletResponse.getWriter() for more details.
(Or simply read any servlet tutorial - without the ability to include data in response bodies, servlets would be pretty useless :)
If that's meant to be XML, Word has already spoiled things for you by changing the attribute quote symbol to ” instead of ".
It is worth having a look at JAXP if you want to generate XML using Java. Writing strings with < etc. in them won't scale and you'll run into problems with encodings of non-ASCII characters.