// // DataInputStream.m // mtalk // // Created by maye on 13-10-24. // Copyright (c) 2013年 zuoye. All rights reserved. // #import "DataInputStream.h" @interface DataInputStream (PrivateMethods) - (int32_t)read; @end @implementation DataInputStream - (id)initWithData:(NSData *)aData { self = [self init]; if(self != nil){ data = [[NSData alloc] initWithData:aData]; } return self; } - (id)init{ self = [super init]; if(self != nil){ length = 0; } return self; } + (id)dataInputStreamWithData:(NSData *)aData { DataInputStream *dataInputStream = [[self alloc] initWithData:aData]; return dataInputStream ; } -(NSUInteger)getAvailabledLen{ return [data length]; } - (int32_t)read{ int8_t v; [data getBytes:&v range:NSMakeRange(length,1)]; length++; return ((int32_t)v & 0x0ff); } - (int8_t)readChar { int8_t v; [data getBytes:&v range:NSMakeRange(length,1)]; length++; return (v & 0x0ff); } - (int16_t)readShort { int32_t ch1 = [self read]; int32_t ch2 = [self read]; if ((ch1 | ch2) < 0){ @throw [NSException exceptionWithName:@"Exception" reason:@"EOFException" userInfo:nil]; } return (int16_t)((ch1 << 8) + (ch2 << 0)); } - (int32_t)readInt { // int32_t ch1 = [self read]; // int32_t ch2 = [self read]; // int32_t ch3 = [self read]; // int32_t ch4 = [self read]; // if ((ch1 | ch2 | ch3 | ch4) < 0){ // @throw [NSException exceptionWithName:@"Exception" reason:@"EOFException" userInfo:nil]; // } // return ((ch1 << 0) + (ch2 << 8) + (ch3 << 16) + (ch4 << 24)); int32_t i; [data getBytes:&i length:4]; length = length + 4; return i; } - (int64_t)readLong { int64_t l; [data getBytes:&l length:8]; length = length + 8; return l; } - (NSString *)readUTF { //short utfLength = [self readShort]; int32_t utfLength = [self readInt]; if (data.length < length + utfLength) { return nil; } NSData *d = [data subdataWithRange:NSMakeRange(length,utfLength)]; NSString *str = [[NSString alloc] initWithData:d encoding:NSUTF8StringEncoding]; length = length + utfLength; return str; } -(NSData *)readDataWithLength:(int)len{ NSLog(@"================>>>> lenght: %ld len:%d",(long)length,len); NSData *d =[data subdataWithRange:NSMakeRange(length, len)]; length = length +len; return d; } -(NSData *)readLeftData{ NSLog(@"=====>>> length %ld data's length %ld",(long)length,(unsigned long)[data length]); if ([data length]>length) { NSData *d =[data subdataWithRange:NSMakeRange(length, [data length])]; length = [data length]; return d; } return nil; } @end