最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to pass byte arrays between Java and JavaScript - Stack Overflow

programmeradmin2浏览0评论

I need to access SecureRandom Java Object from Javascript. My ultimate goal is to grab 4 bytes from PRNG and convert it to Javascript integer variable. According to .4.2/docs/api/java/security/SecureRandom.html, the following two lines of Java code are supposed to do grab 4 random bytes:

byte bytes[] = new byte[4];
random.nextBytes(bytes);

My problems is that I don't know how to 1) allocate byte array suitable for passing to Java method 2) parse that array into integer afterwards

So far I have managed to getSeed() method which returns an array of random bytes. When I render HTML code provided below in Firefox it shows "[B@16f70a4", which appears to be a pointer or something.

<script>
var sprng = new java.security.SecureRandom();
random = sprng.getSeed(4);
document.write(random + "<br/>\n");
</script>

This makes me think that I succeed to instantiate and access Java class, but have a problem with type conversion.

Can anyone please help me to write allocateJavaByteArray(N) and convertJavaByteArrayToInt(N) to let the following code work:

var sprng = new java.security.SecureRandom();
var nextBytes = allocateJavaByteArray(4);
srng.nextBytes(nextBytes);
var nextInt = convertJavaByteArrayToInt(4);

Thank you in advance.

I need to access SecureRandom Java Object from Javascript. My ultimate goal is to grab 4 bytes from PRNG and convert it to Javascript integer variable. According to http://download.oracle./javase/1.4.2/docs/api/java/security/SecureRandom.html, the following two lines of Java code are supposed to do grab 4 random bytes:

byte bytes[] = new byte[4];
random.nextBytes(bytes);

My problems is that I don't know how to 1) allocate byte array suitable for passing to Java method 2) parse that array into integer afterwards

So far I have managed to getSeed() method which returns an array of random bytes. When I render HTML code provided below in Firefox it shows "[B@16f70a4", which appears to be a pointer or something.

<script>
var sprng = new java.security.SecureRandom();
random = sprng.getSeed(4);
document.write(random + "<br/>\n");
</script>

This makes me think that I succeed to instantiate and access Java class, but have a problem with type conversion.

Can anyone please help me to write allocateJavaByteArray(N) and convertJavaByteArrayToInt(N) to let the following code work:

var sprng = new java.security.SecureRandom();
var nextBytes = allocateJavaByteArray(4);
srng.nextBytes(nextBytes);
var nextInt = convertJavaByteArrayToInt(4);

Thank you in advance.

Share Improve this question asked Oct 1, 2010 at 12:01 abbabb 6846 silver badges15 bronze badges 6
  • What sort of weird context are you in that you can get at Java runtime from a <script> tag? I think there may be some confusion afoot here. – Pointy Commented Oct 1, 2010 at 12:11
  • I have tested it in Firefox 3.0 on Ubuntu. See also docstore.mik.ua/orelly/webprog/jscript/ch22_03.htm – abb Commented Oct 1, 2010 at 12:23
  • find a good JSON encoding class for java and pass only JSON as any data for javascript. BTW - I don't get how You put java in <script> tags either. – naugtur Commented Oct 1, 2010 at 13:04
  • Unless you prefer to stay in denial, please consider checking the link I gave above and/or running the piece of Javascript code I have provided in the OP in a Mozilla-based browser. It does not work in IE though. – abb Commented Oct 1, 2010 at 22:40
  • Old question, I know, but did you happen to get a solution? I'm having the same problem... – Altealice Commented Mar 28, 2011 at 10:22
 |  Show 1 more ment

4 Answers 4

Reset to default 2

You could implement convertJavaByteArrayToInt like this:

function convertJavaByteArrayToInt(bytes) {
  var r = 0;
  for (var i = 0; i < bytes.length; i++) {
    r += (bytes[i] & 0xff) << (8 * i);
  }
  return r;
}

allocateJavaByteArray is difficult to implement, because we cannot get the Class of byte. So it's not possible to use java.lang.reflect.Array.newInstance to create a byte[] instance. But here is a tricky implementation:

function allocateJavaByteArray(n) {
  var r = "";
  for (var i = 0; i < n; i++) {
    r += "0";
  }
  return new java.lang.String(r).getBytes();
}

updated: It seems that above code not worked in FireFox 3.6. Here is another allocateJavaByteArray implementation, have a try:

function allocateJavaByteArray(n) {
  var r = new java.io.ByteArrayOutputStream(4);
  for (var i = 0; i < n; i++) {
    r.write(0);
  }
  return r.toByteArray();
}

Normally you'd generate the random number on the server and pass it in the Request to the jsp.

You could simply generate a random integer in the first place, like this:

var nextInt = sprng.nextInt();

Java string is the only thing that will pass Java->JS or JS->Java without headache.

byte[] or any arry will be seen in JS as JSObject.


var sprng = new java.security.SecureRandom();

is

var foo= new java.package.SomeClass();

does work in Netscape/Mozilla/FF

It needs access to classes, so any java standard class or you need to load a jar and then access the class.


to orginal question:

  1. create applet whith utility method:

    public String someStringEncodedValue(){ return 1+"|"+2; }

  2. include applet into the page with unique id

  3. JS find applet using unique id

  4. call method

  5. parse string ( split by | )

发布评论

评论列表(0)

  1. 暂无评论