Re: Is there a more efficient way to get the first 4 bytes off a NSInputStream to compare

2009-01-28 Thread Jeremy Pereira
On 28 Jan 2009, at 17:08, Michael Ash wrote: On Wed, Jan 28, 2009 at 6:00 AM, Jeremy Pereira wrote: On 27 Jan 2009, at 23:07, Graham Cox wrote: However, I would argue that this is the C compiler behaving in a deliberately inconsistent way, since in C, arrays and pointers are supposed to

Re: Is there a more efficient way to get the first 4 bytes off a NSInputStream to compare

2009-01-28 Thread Michael Ash
On Wed, Jan 28, 2009 at 6:00 AM, Jeremy Pereira wrote: > > On 27 Jan 2009, at 23:07, Graham Cox wrote: > >> >> On 28 Jan 2009, at 2:24 am, Jeremy Pereira wrote: >> >>> Yes. That is correct, but since buffer is already a pointer to the first >>> byte of the array and then you are taking a referenc

Re: Is there a more efficient way to get the first 4 bytes off a NSInputStream to compare

2009-01-28 Thread Jeremy Pereira
On 27 Jan 2009, at 23:07, Graham Cox wrote: On 28 Jan 2009, at 2:24 am, Jeremy Pereira wrote: Yes. That is correct, but since buffer is already a pointer to the first byte of the array and then you are taking a reference to it, key will end up containing the address of the buffer. You r

Re: Is there a more efficient way to get the first 4 bytes off a NSInputStream to compare

2009-01-28 Thread Ken Thomases
On Jan 27, 2009, at 8:23 AM, Adam Venturella wrote: Will thanks for the heads up, for my purposes this time around, the input stream will be coming in off the file system, but I did not want to load the whole thing into an NSData object. Have you considered NSFileHandle and its -readDataOfLeng

Re: Is there a more efficient way to get the first 4 bytes off a NSInputStream to compare

2009-01-27 Thread Michael Ash
On Tue, Jan 27, 2009 at 6:43 PM, Jean-Daniel Dupas wrote: > > Le 27 janv. 09 à 23:52, Michael Ash a écrit : > >> On Tue, Jan 27, 2009 at 5:15 PM, William Jon Shipley >> wrote: Will thanks for the heads up, for my purposes this time around, the input stream will be coming in off the

Re: Is there a more efficient way to get the first 4 bytes off a NSInputStream to compare

2009-01-27 Thread Jean-Daniel Dupas
Le 27 janv. 09 à 23:52, Michael Ash a écrit : On Tue, Jan 27, 2009 at 5:15 PM, William Jon Shipley wrote: Will thanks for the heads up, for my purposes this time around, the input stream will be coming in off the file system, but I did not want to load the whole thing into an NSData object

Re: Is there a more efficient way to get the first 4 bytes off a NSInputStream to compare

2009-01-27 Thread Graham Cox
On 28 Jan 2009, at 10:23 am, Chris Suter wrote: In which case using & later is then going to cause it to break. Yes, you have a good point. But I did want to make clear that the statement: since buffer is already a pointer to the first byte of the array and then you are taking a refere

Re: Is there a more efficient way to get the first 4 bytes off a NSInputStream to compare

2009-01-27 Thread Chris Suter
Hi Graham, On Wed, Jan 28, 2009 at 10:07 AM, Graham Cox wrote: > It's a good habit IMO to always > take the address in these cases to make it clear in your code what your > intentions were when you wrote it. I'm afraid I have to disagree with this approach. In my opinion, it's more likely that

Re: Is there a more efficient way to get the first 4 bytes off a NSInputStream to compare

2009-01-27 Thread Graham Cox
On 28 Jan 2009, at 2:24 am, Jeremy Pereira wrote: Yes. That is correct, but since buffer is already a pointer to the first byte of the array and then you are taking a reference to it, key will end up containing the address of the buffer. You really need: uint key = *(uint*)buffer;

Re: Is there a more efficient way to get the first 4 bytes off a NSInputStream to compare

2009-01-27 Thread Michael Ash
On Tue, Jan 27, 2009 at 5:15 PM, William Jon Shipley wrote: >> Will thanks for the heads up, for my purposes this time around, the >> input stream will be coming in off the file system, but I did not want >> to load the whole thing into an NSData object. > > Ah! Well, your approach will work, but

Re: Is there a more efficient way to get the first 4 bytes off a NSInputStream to compare

2009-01-27 Thread Kyle Sluder
On Tue, Jan 27, 2009 at 5:15 PM, William Jon Shipley wrote: > In my tests "NSUncachedRead" is very, VERY fast, great if you are only going > to use each byte once. "NSMappedRead" has a different set of constraints but > can also be very fast. Since he's apparently unwilling, I'll link to Wil's bl

Re: Is there a more efficient way to get the first 4 bytes off a NSInputStream to compare

2009-01-27 Thread Steve Christensen
On 27 Jan 2009, at 14:23, Adam Venturella wrote: The file I am reading is assured to be in little-endian, and I am checking what the host byte ordering is first. Leopard is little endian ( at least on the intel chips, but I have read there are other macs that are big-endian, so I am trying to c

Re: Is there a more efficient way to get the first 4 bytes off a NSInputStream to compare

2009-01-27 Thread William Jon Shipley
Will thanks for the heads up, for my purposes this time around, the input stream will be coming in off the file system, but I did not want to load the whole thing into an NSData object. Ah! Well, your approach will work, but if your file is less than 4GB I highly recommend something like:

Re: Is there a more efficient way to get the first 4 bytes off a NSInputStream to compare

2009-01-27 Thread Sean McBride
On 1/27/09 3:24 PM, Jeremy Pereira said: >> Leopard is little endian ( at least on >> the intel chips, but I have read there are other macs that are >> big-endian, so I am trying to catch and handle that accordingly) > >The endianness is dependent on the processor architecture, not the >operating

Re: Is there a more efficient way to get the first 4 bytes off a NSInputStream to compare

2009-01-27 Thread Jeremy Pereira
On 27 Jan 2009, at 14:23, Adam Venturella wrote: Leopard is little endian ( at least on the intel chips, but I have read there are other macs that are big-endian, so I am trying to catch and handle that accordingly) The endianness is dependent on the processor architecture, not the operat

Re: Is there a more efficient way to get the first 4 bytes off a NSInputStream to compare

2009-01-27 Thread Adam Venturella
Will thanks for the heads up, for my purposes this time around, the input stream will be coming in off the file system, but I did not want to load the whole thing into an NSData object. Graham, thanks for the byte-ordering nod as well. The file I am reading is assured to be in little-endian, and

Re: Is there a more efficient way to get the first 4 bytes off a NSInputStream to compare

2009-01-27 Thread William Jon Shipley
[inputStream open]; [inputStream read: buffer maxLength:sizeof(buffer)]; ... Note that although your call to -read:maxLength: will work as you expect most of the time, it's not guaranteed to work and is bad practice. The docs for NSInputStream say this method will "Return the actual numb

Re: Is there a more efficient way to get the first 4 bytes off a NSInputStream to compare

2009-01-27 Thread Jean-Daniel Dupas
Le 27 janv. 09 à 06:28, Graham Cox a écrit : On 27 Jan 2009, at 4:17 pm, Adam Venturella wrote: Thanks! I knew I was doing to many steps! uint key = *(uint*)&buffer; You will also need to consider byte-ordering if your app or the data could be used on different architectures. If for

Re: Is there a more efficient way to get the first 4 bytes off a NSInputStream to compare

2009-01-26 Thread Graham Cox
On 27 Jan 2009, at 4:17 pm, Adam Venturella wrote: Thanks! I knew I was doing to many steps! uint key = *(uint*)&buffer; You will also need to consider byte-ordering if your app or the data could be used on different architectures. If for example your input data is known to be big-end

Re: Is there a more efficient way to get the first 4 bytes off a NSInputStream to compare

2009-01-26 Thread Adam Venturella
Thanks! I knew I was doing to many steps! On Mon, Jan 26, 2009 at 8:44 PM, Graham Cox wrote: > > On 27 Jan 2009, at 2:09 pm, Adam Venturella wrote: > >> NSData * d = [NSData dataWithBytes:buffer length:sizeof(buffer)]; >> uint * key = (uint *) malloc(sizeof(uint)); >> [d getBytes:key length:sizeo

Re: Is there a more efficient way to get the first 4 bytes off a NSInputStream to compare

2009-01-26 Thread Graham Cox
On 27 Jan 2009, at 2:09 pm, Adam Venturella wrote: NSData * d = [NSData dataWithBytes:buffer length:sizeof(buffer)]; uint * key = (uint *) malloc(sizeof(uint)); [d getBytes:key length:sizeof(uint)]; uint key; [d getBytes:&key length:sizeof(uint)]; but since you have merely wrapped in NSDa

Is there a more efficient way to get the first 4 bytes off a NSInputStream to compare

2009-01-26 Thread Adam Venturella
Here is what I am doing now.. I just feel like I have an extra step in converting the buffer into NSData...malloc...free uint sample = 0x04034b50; uint8_t buffer[sizeof(uint)]; [inputStream open]; [inputStream read: buffer maxLength:sizeof(buffer)]; NSData * d = [NSData dataWithBytes:buf