I'm looking at the docs for FloatBuffer, and in many places it says
"(optional operation)". What's the deal with this? Can we use the
operation or not?
Thanks.
|
|
0
|
|
|
|
Reply
|
bob136 (381)
|
10/5/2011 4:49:53 PM |
|
On 10/5/2011 9:49 AM, bob wrote:
> I'm looking at the docs for FloatBuffer, and in many places it says
> "(optional operation)". What's the deal with this? Can we use the
> operation or not?
The term "optional operation" is used in interface and abstract class
declarations to indicate a method that the implementation may be allowed
to throw UnsupportedOperationException.
There are two useful places to look for more information:
1. The full description of the method. For example, FloatBuffer's array
method is defined to throw UnsupportedOperationException "If this buffer
is not backed by an accessible array".
2. Especially in java.util, there are interfaces with many different
implementing classes, and you may need to look at the API documentation
for the implementing class you are using to find out which operations it
supports.
Patricia
|
|
0
|
|
|
|
Reply
|
pats (3215)
|
10/5/2011 5:30:26 PM
|
|
Thanks, but I am still confused. How can a buffer not be backed by an
accessible array?
I tried this code, and hasArray returns true. However, hasArray2
returns false.
byte[] b =3D new byte[numfloats*4];
dis.read(b, 0, numfloats*4);
ByteBuffer bb =3D ByteBuffer.wrap(b);
boolean hasArray =3D bb.hasArray();
FloatBuffer fb2 =3D bb.asFloatBuffer();
boolean hasArray2 =3D fb2.hasArray();
I'm trying to get a float[] out of this, but I can't if hasArray2
returns false.
On Oct 5, 12:30=A0pm, Patricia Shanahan <p...@acm.org> wrote:
> On 10/5/2011 9:49 AM, bob wrote:
>
> > I'm looking at the docs for FloatBuffer, and in many places it says
> > "(optional operation)". =A0What's the deal with this? =A0Can we use the
> > operation or not?
>
> The term "optional operation" is used in interface and abstract class
> declarations to indicate a method that the implementation may be allowed
> to throw UnsupportedOperationException.
>
> There are two useful places to look for more information:
>
> 1. The full description of the method. For example, FloatBuffer's array
> method is defined to throw UnsupportedOperationException "If this buffer
> is not backed by an accessible array".
>
> 2. Especially in java.util, there are interfaces with many different
> implementing classes, and you may need to look at the API documentation
> for the implementing class you are using to find out which operations it
> supports.
>
> Patricia
|
|
0
|
|
|
|
Reply
|
bob136 (381)
|
10/5/2011 9:20:28 PM
|
|
On 10/5/2011 2:20 PM, bob wrote:
> Thanks, but I am still confused. How can a buffer not be backed by an
> accessible array?
I would have guessed due to this method here:
<http://download.oracle.com/javase/6/docs/api/java/nio/ByteBuffer.html#allocateDirect%28int%29>
You'll need to read the comments at the begging of that page to
understand what it is doing.
>
> I tried this code, and hasArray returns true. However, hasArray2
> returns false.
>
> byte[] b = new byte[numfloats*4];
> dis.read(b, 0, numfloats*4);
>
> ByteBuffer bb = ByteBuffer.wrap(b);
It's been a while since I've worked with ByteBuffers, but I think the
wrap() method is mostly for writing, not reading.
> boolean hasArray = bb.hasArray();
> FloatBuffer fb2 = bb.asFloatBuffer();
> boolean hasArray2 = fb2.hasArray();
>
> I'm trying to get a float[] out of this, but I can't if hasArray2
> returns false.
The trick to ByteBuffers is they work with Channels, not streams.
<http://download.oracle.com/javase/6/docs/api/java/nio/channels/Channel.html>
<http://download.oracle.com/javase/6/docs/api/java/nio/channels/FileChannel.html>
Not tested, but I think this is the idea:
FileInputStream fis = new FileInputStream( "your/filename/here" );
FileChannel fchan = fis.getChannel();
ByteBuffer inBuff = ByteBuffer.allocateDirect( 64 * 1024 );
fchan.read( inBuff );
FloatBuffer floatBuffer = inBuff.asFloatBuffer()
float[] floats = new float[ numfloats ];
floatBuffer.read( floats );
To reuse the underlying ByteBuffer, I think you have to reset() it. The
FloatBuffer does not have to be reset, it'll reset when you reset the
ByteBuffer backing it, and you shouldn't have to call asFloatBuffer()
again, you can just re-use the existing FloatBuffer. This'll help you
with speed.
You're doing a good job reading the documentation so far, you should
verify what I've done above because I haven't tested it. However I
think it's at least close and gives you the basic pattern for reading
with a ByteBuffer.
If you have more than 16 * 1024 floats, you'll need to use a loop to
read them, or increase the side of the buffer in allocateDirect.
|
|
0
|
|
|
|
Reply
|
markspace
|
10/5/2011 10:51:33 PM
|
|
On 10/5/2011 3:51 PM, markspace wrote:
> On 10/5/2011 2:20 PM, bob wrote:
>> Thanks, but I am still confused. How can a buffer not be backed by an
>> accessible array?
>
> I would have guessed due to this method here:
>
> <http://download.oracle.com/javase/6/docs/api/java/nio/ByteBuffer.html#allocateDirect%28int%29>
>
>
> You'll need to read the comments at the begging of that page to
> understand what it is doing.
Should be "beginning" not begging. Typing fast, didn't proofread.
Meant to add too that you seemed to have discovered another reason that
the array would not be accessible --- the Buffer wraps another buffer,
which is fine.
|
|
0
|
|
|
|
Reply
|
markspace
|
10/5/2011 11:05:10 PM
|
|
Here's what I tried:
AssetManager am =3D this.getResources().getAssets();
AssetFileDescriptor afd =3D am.openFd(filename);
FileInputStream fis =3D afd.createInputStream();
FileChannel fchan =3D fis.getChannel();
ByteBuffer inBuff =3D ByteBuffer.allocateDirect( 24121*4 );
int bytesread =3D fchan.read( inBuff );
FloatBuffer floatBuffer =3D inBuff.asFloatBuffer();
float[] floats =3D new float[ 24121 ];
floatBuffer.get( floats);
return floats;
However, I always get a BufferUnderflowException at
floatBuffer.get( floats);
10-05 23:37:07.400: ERROR/AndroidRuntime(3570): Caused by:
java.nio.BufferUnderflowException
For some reason, floatBuffer thinks it has a capacity of 0 after it's
created.
Any ideas?
On Oct 5, 6:05=A0pm, markspace <-@.> wrote:
> On 10/5/2011 3:51 PM, markspace wrote:
>
> > On 10/5/2011 2:20 PM, bob wrote:
> >> Thanks, but I am still confused. How can a buffer not be backed by an
> >> accessible array?
>
> > I would have guessed due to this method here:
>
> > <http://download.oracle.com/javase/6/docs/api/java/nio/ByteBuffer.html.=
...>
>
> > You'll need to read the comments at the begging of that page to
> > understand what it is doing.
>
> Should be "beginning" not begging. =A0Typing fast, didn't proofread.
>
> Meant to add too that you seemed to have discovered another reason that
> the array would not be accessible --- the Buffer wraps another buffer,
> which is fine.
|
|
0
|
|
|
|
Reply
|
bob136 (381)
|
10/6/2011 6:46:11 AM
|
|
On Wednesday, October 5, 2011 11:46:11 PM UTC-7, bob wrote:
PLEASE do not top-post!
PLEASE stop embedding TAB characters in your listings!
Dude. Really.
> Here's what I tried:
>
> AssetManager am = this.getResources().getAssets();
>
> AssetFileDescriptor afd = am.openFd(filename);
> FileInputStream fis = afd.createInputStream();
> FileChannel fchan = fis.getChannel();
> ByteBuffer inBuff = ByteBuffer.allocateDirect( 24121*4 );
> int bytesread = fchan.read( inBuff );
>
> FloatBuffer floatBuffer = inBuff.asFloatBuffer();
>
> float[] floats = new float[ 24121 ];
> floatBuffer.get( floats);
> return floats;
>
> However, I always get a BufferUnderflowException at
> floatBuffer.get( floats);
>
> 10-05 23:37:07.400: ERROR/AndroidRuntime(3570): Caused by:
> java.nio.BufferUnderflowException
>
> For some reason, floatBuffer thinks it has a capacity of 0 after it's
> created.
>
> Any ideas?
buffer 'flip()'?
--
Lew
|
|
0
|
|
|
|
Reply
|
lewbloch (1311)
|
10/6/2011 1:47:37 PM
|
|
|
6 Replies
60 Views
(page loaded in 0.774 seconds)
Similiar Articles: DerInputStream.getLength(): lengthTag=127, too big ...It could be that there are some optional fields in the certificate that are not ... The certchain is good... > >But i need to make some Operation in Java and try to use ... Does PAUSE have any Side Effect ?? - comp.lang.fortranIt seems that might count as an I/O operation, and also a subroutine call. ... An interface wouldn't do any good because that is not how you do optional arguments ... Error_IO_Pending - comp.lang.asm.x86... has been signalled, which could cause the application to believe the first operation ... GENERIC_MESSAGE) ; pSid, // user security identifier (optional ... comp.protocols.time.ntp - page 11... port and DCD-pin), I have got a message: [daemon.notice] refclock_ioctl: optional ... on poll interval? 4 86 (8/31/2003 1:30:56 PM) From The Nanokernel: "In operation, PLL ... New version of the Tcl/Tk GUI builder PureTkGUI : v0.10.0 - comp ...... cut/copy/paste feature, which is very important during GUI design operations as it ... Two kinds of warnings are liable to occur at startup : 1- optional tcl packages ... ntpd, boot time, and hot plugging - comp.protocols.time.ntp ...... it backgrounds itself and continues normal startup operations, starts working on calculating/updating the drift, etc.... At least, it should have this as an optional ... regsub (and regular expressions in general) trouble. - comp.lang ...... is related to my previous post "another newbie question, simple string operation?" ... - optional trailing space, not part of the ... shutdown vs. reboot - comp.unix.solaris'shutdown -i 6 ...' will do an 'init 6', along with an optional warning to logged in ... case, /usr/sbin/reboot is preferable since reboot will perform a sync(1M) operation ... Regex to match a numerical IP range - comp.lang.perl.misc ...... dec number, becomes hex in \x{#} utf8 char) # Input -> '#.#.#.# (optional ... It's surprisingly hard to do IP ranges well, especially if you need fast operations. Surface normals? - comp.graphics.api.openglIt appears that there are > already quite a hefty share of optional features which ... It's not a graphics operation, it's an algorithmic one, better suited to a "glu ... java - What does "optional operation" mean in Javadoc of for ...When in the java documentation for Set it says in the specification of a method Optional Operation e.g. (emphasis by me) add(E e) Adds the specified element to this ... Mandatory, Optional, and Other OperationsMandatory Methods Up: Guide to the Back-End Previous: Guide to the Back-End . Mandatory, Optional, and Other Operations. The first question the implementor of a new ... 7/18/2012 9:48:25 AM
|