Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #8575 > unrolled thread

optional operation

Started bybob <bob@coolgroups.com>
First post2011-10-05 09:49 -0700
Last post2011-10-06 06:47 -0700
Articles 7 — 4 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  optional operation bob <bob@coolgroups.com> - 2011-10-05 09:49 -0700
    Re: optional operation Patricia Shanahan <pats@acm.org> - 2011-10-05 10:30 -0700
      Re: optional operation bob <bob@coolgroups.com> - 2011-10-05 14:20 -0700
        Re: optional operation markspace <-@.> - 2011-10-05 15:51 -0700
          Re: optional operation markspace <-@.> - 2011-10-05 16:05 -0700
            Re: optional operation bob <bob@coolgroups.com> - 2011-10-05 23:46 -0700
              Re: optional operation Lew <lewbloch@gmail.com> - 2011-10-06 06:47 -0700

#8575 — optional operation

Frombob <bob@coolgroups.com>
Date2011-10-05 09:49 -0700
Subjectoptional operation
Message-ID<5eb0fcba-199f-46e4-b913-2f3e851d89fc@q26g2000vby.googlegroups.com>
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.

[toc] | [next] | [standalone]


#8577

FromPatricia Shanahan <pats@acm.org>
Date2011-10-05 10:30 -0700
Message-ID<c_idnaoeDvisCRHTnZ2dnUVZ_rWdnZ2d@earthlink.com>
In reply to#8575
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

[toc] | [prev] | [next] | [standalone]


#8583

Frombob <bob@coolgroups.com>
Date2011-10-05 14:20 -0700
Message-ID<6745899f-3c13-4d2b-81fb-52975c560a3b@q24g2000vby.googlegroups.com>
In reply to#8577
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 = new byte[numfloats*4];
			dis.read(b, 0, numfloats*4);

			ByteBuffer bb = ByteBuffer.wrap(b);
			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.







On Oct 5, 12:30 pm, 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)".  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

[toc] | [prev] | [next] | [standalone]


#8584

Frommarkspace <-@.>
Date2011-10-05 15:51 -0700
Message-ID<j6in1n$fil$1@dont-email.me>
In reply to#8583
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.

[toc] | [prev] | [next] | [standalone]


#8585

Frommarkspace <-@.>
Date2011-10-05 16:05 -0700
Message-ID<j6inr7$jms$1@dont-email.me>
In reply to#8584
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.

[toc] | [prev] | [next] | [standalone]


#8592

Frombob <bob@coolgroups.com>
Date2011-10-05 23:46 -0700
Message-ID<a1d9260d-6f6f-456c-a49c-ae8a0bfd2c9a@5g2000yqo.googlegroups.com>
In reply to#8585
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?












On Oct 5, 6:05 pm, 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.  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.

[toc] | [prev] | [next] | [standalone]


#8594

FromLew <lewbloch@gmail.com>
Date2011-10-06 06:47 -0700
Message-ID<10179923.1102.1317908858001.JavaMail.geo-discussion-forums@prcs9>
In reply to#8592
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web