0

In my code (HttpServlet in doGet method) I need to write bytes that can take any value, from 0 to 255, in a .js file. I checked in the debugger that result[1] has the value 0. However, in the external file it is written as blank space and when I try to read it has the byte value "32". I only have this problem with the byte 0, for the rest this is working perfectly. Any ideas?

res.setContentType("image/gif");
res.setHeader("Content-Disposition","attachment;filename=myFile.js");
OutputStream os = res.getOutputStream();
byte[] result=encrypt(req.getParameter("original")); // Here result has values [64,0,81,80]
os.write(result,0,result.length);

I retrieve the values from an external JavaScript:

var whatever = data[1].charCodeAt(0); // whatever has value 32

I have seen a similar problem in a Javascript program, and they fixed this problem including this loop right before writing to file:

for (var i=0; i<result.length; i++) {
    result[i] = String.fromCharCode(result[i]); }

I have do some tests and works for Javascript. What would be the equivalent in Java?

Thanks

4
  • 2
    Why are you setting content type image/gif for a javascript file? Commented Jan 16, 2012 at 8:51
  • Because this JS file can include non-text characters (as this byte 0), and if I use content type "text/javascript" would be a problem, right? Thanks Commented Jan 16, 2012 at 11:02
  • 1
    javascript is text. I am not aware of any browser that would make sense of javascript containing binary characters. Commented Jan 16, 2012 at 11:04
  • Thanks Darin. Please see my reply to Apurv's comment below ("Let's keep..."). Commented Jan 16, 2012 at 12:09

3 Answers 3

1

32 is the ASCII value for blank space. That is why you are getting blank for 32.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks. But why is ASCII 0 written as a blank space?
@user411103 there must be an alternative to what you are doing. Why do you want to include binary data in text file ?\
Let's keep it simple. Forget it is a .js file. I just want to write in a binary file the ASCII 0 (which is NULL but no BLANK SPACE) and read it as 0 but not 32. What would be wrong in the code above? Thanks
The problem starts when try to read it with 'charCodeAt'. If you are writing something as a binary data, read it as binary data and not as text
@user411103 Can you try this codeproject.com/KB/scripting/Exsead7.aspx for reading binary data from JS ?
|
0

Reading binary data has already been answered in Stackoverflow: How do I read binary data to a byte array in Javascript?.

Content type better is application/octet-stream, without content disposition header.

8 Comments

Thanks for your help. I cannot remove the content-disposition header, because I really need this data displayed as an attachment. Then I tried "application/octet-stream" with the reading binary function, and this is still reading a "32" (blank space) where I see the OutputStream.write function has a "0". I believe this is a writing problem, not reading. Perhaps should I specify somehow the charset for the output file?
I have edited my initial question with something that fixes this problem in Javascript. But I am not sure what to do for Java.
Then the encrypt function must be erroneous, or the original parameter.
Joop, I just tried a very simple example: byte[] result=new byte[3];result[0]=65;result[1]=0;result[2]=66;os.write(result,0,result.length);os.flush();os.close(); And I am receiving [65,32,66], so it is not the encrypt function or parameter. Perhaps the setCharacterEncoding() function? Thanks
Did you try saving it as file in the browser, i.o. reading? os.write has to be okay. Maybe changing the attachment name into something binary. BTW when all works, a setContentLength might be added.
|
0

You can check these

Is there any way to send binary data with XMLHttpRequest object?

Send and receive binary data over web sockets in Javascript?

http://www.codeproject.com/KB/scripting/Javascript_binaryenc.aspx

http://support.microsoft.com/kb/296772

3 Comments

The funny story is that if I deploy this code to Google App Engine, it is working. But it is not in my test server (Eclipse). Perhaps a charset?
Are you saying that your original code is working in Google App Engine ?
That is exactly what I am saying. And working with a JS file as well. Perhaps GAE applies a different charset by default in HttpServlet doGet/doPost??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.