3

I'm trying to build a application for the iPhone, although I am completely new to Obj-C. For one problem I'd use a ByteBuffer in Java, but I don't see any appropriate class in Apple's documentation. So I probably have to implement it on my own.

My question is, how to do it best:

  • Is there a similar class in Obj-C? (That would be the best solution ;))
  • Should I do it by using Obj-C classes like NSData?
  • Or should I stay with plain C code?
Koraktor
  • 41,357
  • 10
  • 69
  • 99
  • It really depends on what you're trying to do. You should edit your question to add a little more context. – Alex Mar 24 '09 at 16:16
  • My aim is to implement a fully functional Obj-C version of java.nio.ByteBuffer. Just wanted to know what I should use to get there. – Koraktor Mar 24 '09 at 16:32

3 Answers3

8

You probably want NSMutableData.

Mark Probst
  • 7,107
  • 7
  • 40
  • 42
  • Ok... do you have any example how to read a e.g. a float from NSMutableData? Or any other data type? – Koraktor Mar 24 '09 at 16:35
  • 1
    AFAIK there are no such methods, but you can implement them yourself as a category of NSData, so they're usable with NSData and any of its subclasses. Just use getBytes:length: to fetch sizeof(float) bytes and then cast the buffer. – Mark Probst Mar 24 '09 at 17:24
6

My recollection of java.nio.ByteBuffer (from working with Java many moons ago) is that ByteBuffer implements sequential reads/writes to an array of bytes. This is analogous to an NSInputStream backed by an NSData (for input):

NSInputStream *inputStream = [NSInputStream inputStreamwithData:myData]; //assuming myData is NSData*

float myFloat;

if([inputStream hasBytesAvailable]) { // NO if you've already read to the end of myData
  NSInteger bytesRead = [inputStream read:&myFloat maxLength:sizeof(myFloat)];
  NSAssert(bytesRead == sizeof(myFloat);
}

You can do something similar with an NSOutputStream writing to an NSData.

Barry Wark
  • 107,306
  • 24
  • 181
  • 206
  • This looks pretty promising. Thank you very much. – Koraktor Mar 24 '09 at 17:40
  • 1
    I'm curious to see how your port turns out and would be very interested in using the result. Please ping me when it's ready. – Barry Wark Mar 24 '09 at 18:01
  • I'm done with a working Obj-C version of ByteBuffer. You can have a look at it here: http://github.com/koraktor/steam-condenser/blob/objc/objc/src/ByteBuffer.h, http://github.com/koraktor/steam-condenser/blob/objc/objc/src/ByteBuffer.m – Koraktor Mar 31 '09 at 08:42
  • The github.com links are broken, but I'm very excited to take a look when the code is back up. – Barry Wark Mar 31 '09 at 16:40
  • Oh I'm sorry. I had to delete the branch again as it contained some things I didn't want to be online at the moment. But I forget to push the up to GitHub again. They're online now. – Koraktor Apr 01 '09 at 05:21
  • Hey Koraktor i need your Obj-c version of Byte buffer.Would you please give me your updated github link or guide me what should i need to read for implement Java ByteBuffer with Objective c or Swift.Thanks. – Gaurav Vyas Sep 08 '16 at 07:58
  • @Koraktor could you please share Obj-c version of Byte buffer? please – Waaheeda Dec 09 '21 at 06:02
0

I have a similar question. ilya pointed my here with his answer .

My aim is to implement a fully functional Obj-C version of java.nio.ByteBuffer. Just wanted to know what I should use to get there

That will be your custom class.

For my case I think NSInputStream and NSOutputStream should be used. For sure not NSMutableData as how is the accepted and mostly up voted answer.

Community
  • 1
  • 1