Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Java's Unsafe class  (Read 2636 times)
0 Members and 2 Guests are viewing this topic.
Offline xfactor973

JGO n00b
*

Posts: 8


Java games rock!


« on: 2003-08-13 20:02:51 »

Anyone have experience using sun.misc.Unsafe ?  I tried using it today and it's giving an error when i run it.  

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
import sun.misc.*;

public class UnsafeTester {
  private static final Unsafe unsafe = Unsafe.getUnsafe();
  public static void main(String[] args) {
    long ptr = unsafe.allocateMemory(512);
    try{
      unsafe.putChar(ptr,'a');
      unsafe.putChar(ptr,'b');
      System.out.println("Unsafe: " + unsafe.toString());
    }
    finally{
      unsafe.freeMemory(ptr);
    }
  }
}

java.lang.ExceptionInInitializerError
Caused by: java.lang.SecurityException: Unsafe
      at sun.misc.Unsafe.getUnsafe(Unsafe.java:68)
      at garbage.UnsafeTester.<clinit>(UnsafeTester.java:8)
Exception in thread "main"

I'm not sure exactly how I should go about tackling this one so any help would be great.   Wink
Offline AndersDahlberg

Full Member
**

Posts: 196



« Reply #1 on: 2003-08-13 20:05:33 »

Well, don't use it Grin

There was a bug once upon a time Wink when you could actually use it to wrec havoc (first beta1.4.0 or something like that) - they fixed that quite fast Smiley
Offline GergisKhan

Full Member
**

Posts: 221


&quot;C8 H10 N4 O2&quot;


« Reply #2 on: 2003-08-13 20:35:38 »

Ummmm.... what is this class?

why does it curiously remind me of the structs discussion that was occurring in another thread somewhere?

And for future reference, where can I get information about the sun.misc.* classes?

gK

"Go.  Teach them not to mess with us."
          -- Cao Cao, Dynasty Warriors 3
Games published by our own members! Go get 'em!
Offline tortoise

Full Member
**

Posts: 230


<3 Shmups


« Reply #3 on: 2003-08-13 20:49:23 »

Whatever it is, I suspect it is unsafe to use Smiley

You're really not supposed to use any of the sun.* classes for real use. They are subject to change at any moment, and are only available on Sun JVMs.
Offline socketrider

JGO n00b
*

Posts: 1


Java games rock!


« Reply #4 on: 2003-08-14 06:25:03 »

Check this page: http://www.jguru.com/faq/view.jsp?EID=448031

Cheers
Offline xfactor973

JGO n00b
*

Posts: 8


Java games rock!


« Reply #5 on: 2003-08-14 07:12:54 »

Thanks for your help.  The only reason I was curious about that Unsafe class was my friend wanted to make a stringBuffer class using JNI and it turned out being alot slower than even your normal stringbuffer.  I'm guessing the JNI calls are expensive.  so we went hunting in the nio classes and found no native methods and that lead to the Unsafe class.  Only problem is that security exception that i'm getting.  Guess it's unusable at this point.   :-/
Offline princec
« League of Dukes »

JGO Kernel
*****

Posts: 8088
Medals: 95


Eh? Who? What? ... Me?


« Reply #6 on: 2003-08-14 07:32:56 »

You should have no need whatsoever to use Unsafe.
Got performance problems? You've written the code wrong. Time to run with the -Xprof flag methinks!

Cas Smiley

Offline xfactor973

JGO n00b
*

Posts: 8


Java games rock!


« Reply #7 on: 2003-08-14 07:39:16 »

yeah this is true.  It was just something i was toying around with.   Grin
Offline xfactor973

JGO n00b
*

Posts: 8


Java games rock!


« Reply #8 on: 2003-08-22 07:57:26 »

When I allocate memory with the Unsafe class what exactly is it doing?  I'm really just experimenting with the class.  And the other thing is that when i call unsafe.addressSize() it always returns 4 for some odd reason.  Anyone know how this class works? Huh
Offline abies

Sr. Member
**

Posts: 456



« Reply #9 on: 2003-08-22 08:52:14 »

Quote
 And the other thing is that when i call unsafe.addressSize() it always returns 4 for some odd reason.


I suppose that it returns size of native platform pointer in bytes. So on 64-bit system you will probably get 8 as a return.

Artur Biesiadowski
Games published by our own members! Go get 'em!
Offline xfactor973

JGO n00b
*

Posts: 8


Java games rock!


« Reply #10 on: 2003-08-23 09:46:46 »

Good thinking.  Thanks for the help
Offline Jeff

JGO Kernel
*****

Posts: 3535


Got any cats?


« Reply #11 on: 2003-09-05 18:23:22 »

Unsafe is the underpinning to Native Byte Buffers

Thats really where you want to hit this.

JK

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.071 seconds with 19 queries.