在 ios 下实现 DataOutputStream 和 DataInputStream

17 篇文章 0 订阅

 我们在java通讯,常用 DataOutputStream 和 DataInputStream 这两个数据流,客户端是android和j2me 的话没有什么大问题,如果是iphone和bada就比较麻烦了。为解决这问题我在ios下封装了 DataOutputStream 和 DataInputStream 这两个类,提供大家参考。


[cpp]  view plain copy
  1. //  
  2. //  DataOutputStream.h  
  3. //  DataStream  
  4. //  
  5. //  Created by wangzhongbin on 11-8-16.  
  6. //  Copyright 2011 wangzhongbin. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10.   
  11.   
  12. // 数据输出流允许应用程序以适当方式将基本数据类型写入输出流中  
  13. @interface DataOutputStream : NSObject {  
  14.     NSMutableData *data;  
  15.     NSInteger length;  
  16. }  
  17.   
  18. // 将一个 char 值以 1-byte 值形式写入基础输出流中,先写入高字节。  
  19. - (void)writeChar:(int8_t)v;  
  20.   
  21. //将一个 short 值以 2-byte 值形式写入基础输出流中,先写入高字节。  
  22. - (void)writeShort:(int16_t)v;  
  23.   
  24. //将一个 int 值以 4-byte 值形式写入基础输出流中,先写入高字节。  
  25. - (void)writeInt:(int32_t)v;  
  26.   
  27. //将一个 long 值以 8-byte 值形式写入基础输出流中,先写入高字节。  
  28. - (void)writeLong:(int64_t)v;  
  29.   
  30. //以与机器无关方式使用 UTF-8 修改版编码将一个字符串写入基础输出流。  
  31. - (void)writeUTF:(NSString *)v;  
  32.   
  33. //将一个 NSData byte数组写入输出流中,先写入高字节。  
  34. - (void)writeBytes:(NSData *)v;  
  35.   
  36. //将此转换为 byte 序列。  
  37. - (NSData *)toByteArray;  
  38.   
  39. @end  


[cpp]  view plain copy
  1. //  
  2. //  DataOutputStream.m  
  3. //  DataStream  
  4. //  
  5. //  Created by wangzhongbin on 11-8-16.  
  6. //  Copyright 2011 wangzhongbin. All rights reserved.  
  7. //  
  8.   
  9. #import "DataOutputStream.h"  
  10.   
  11.   
  12. @implementation DataOutputStream  
  13.   
  14. - (id)init{  
  15.     self = [super init];  
  16.     if(self != nil){  
  17.         data = [[NSMutableData alloc] init];  
  18.         length = 0;  
  19.     }  
  20.     return self;  
  21. }  
  22.   
  23. - (void)writeChar:(int8_t)v {  
  24.     int8_t ch[1];  
  25.     ch[0] = (v & 0x0ff);  
  26.     [data appendBytes:ch length:1];  
  27.     length++;  
  28. }  
  29.   
  30. - (void)writeShort:(int16_t)v {  
  31.     int8_t ch[2];  
  32.     ch[0] = (v & 0x0ff00)>>8;  
  33.     ch[1] = (v & 0x0ff);  
  34.     [data appendBytes:ch length:2];  
  35.     length = length + 2;  
  36. }  
  37.   
  38. - (void)writeInt:(int32_t)v {  
  39.     int8_t ch[4];  
  40.     for(int32_t i = 0;i<4;i++){  
  41.         ch[i] = ((v >> ((3 - i)*8)) & 0x0ff);  
  42.     }  
  43.     [data appendBytes:ch length:4];  
  44.     length = length + 4;  
  45. }  
  46.   
  47. - (void)writeLong:(int64_t)v {  
  48.     int8_t ch[8];  
  49.     for(int32_t i = 0;i<8;i++){  
  50.         ch[i] = ((v >> ((7 - i)*8)) & 0x0ff);  
  51.     }  
  52.     [data appendBytes:ch length:8];  
  53.     length = length + 8;  
  54. }  
  55.   
  56. - (void)writeUTF:(NSString *)v {  
  57.     NSData *d = [v dataUsingEncoding:NSUTF8StringEncoding];  
  58.     NSInteger len = [d length];  
  59.     [self writeShort:len];  
  60.     [data appendData:d];  
  61.     length = length + len;  
  62. }  
  63.   
  64. - (void)writeBytes:(NSData *)v {  
  65.     [data appendData:v];  
  66.     NSInteger len = [v length];  
  67.     length = length + len;  
  68. }  
  69.   
  70. - (NSData *)toByteArray{  
  71.     return [[[NSData alloc] initWithData:data] autorelease];  
  72. }  
  73.   
  74. - (void)dealloc{  
  75.     [data release];  
  76.     [super dealloc];  
  77. }  
  78.   
  79. @end  



[cpp]  view plain copy
  1. //  
  2. //  DataInputStream.h  
  3. //  DataStream  
  4. //  
  5. //  Created by  wangzhongbin on 11-8-15.  
  6. //  Copyright 2011年 wangzhongbin. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10.   
  11. // 从输入流读取基本数据类型的方法,以便解组自定义值类型  
  12. @interface DataInputStream : NSObject {  
  13.     NSData *data;  
  14.     NSInteger length;  
  15. }  
  16.   
  17. //  
  18. - (id)initWithData:(NSData *)data;  
  19.   
  20. //  
  21. + (id)dataInputStreamWithData:(NSData *)aData;  
  22.   
  23. // 从输入流读取 char 值。  
  24. - (int8_t)readChar;  
  25.   
  26. //从输入流读取 short 值。  
  27. - (int16_t)readShort;  
  28.   
  29. //从输入流读取 int 值。  
  30. - (int32_t)readInt;  
  31.   
  32. //从输入流读取 long 值。  
  33. - (int64_t)readLong;  
  34.   
  35. //从输入流读取 NSString 字符串。  
  36. - (NSString *)readUTF;  
  37.   
  38. @end  


[cpp]  view plain copy
  1. //  
  2. //  DataInputStream.m  
  3. //  DataStream  
  4. //  
  5. //  Created by  wangzhongbin on 11-8-15.  
  6. //  Copyright 2011年 wangzhongbin. All rights reserved.  
  7. //  
  8.   
  9. #import "DataInputStream.h"  
  10.   
  11. @interface DataInputStream (PrivateMethods)  
  12. - (int32_t)read;  
  13. @end  
  14.   
  15. @implementation DataInputStream  
  16.   
  17. - (id)initWithData:(NSData *)aData {  
  18.     self = [self init];  
  19.     if(self != nil){  
  20.         data = [[NSData alloc] initWithData:aData];  
  21.     }  
  22.     return self;  
  23. }  
  24.   
  25. - (id)init{  
  26.     self = [super init];  
  27.     if(self != nil){  
  28.         length = 0;  
  29.     }  
  30.     return self;  
  31. }  
  32.   
  33. + (id)dataInputStreamWithData:(NSData *)aData {  
  34.     DataInputStream *dataInputStream = [[self alloc] initWithData:aData];  
  35.     return [dataInputStream autorelease];  
  36. }  
  37.   
  38. - (int32_t)read{  
  39.     int8_t v;  
  40.     [data getBytes:&v range:NSMakeRange(length,1)];  
  41.     length++;  
  42.     return ((int32_t)v & 0x0ff);  
  43. }  
  44.   
  45. - (int8_t)readChar {  
  46.     int8_t v;  
  47.     [data getBytes:&v range:NSMakeRange(length,1)];  
  48.     length++;  
  49.     return (v & 0x0ff);  
  50. }  
  51.   
  52. - (int16_t)readShort {  
  53.     int32_t ch1 = [self read];  
  54.     int32_t ch2 = [self read];  
  55.     if ((ch1 | ch2) < 0){  
  56.         @throw [NSException exceptionWithName:@"Exception" reason:@"EOFException" userInfo:nil];  
  57.     }  
  58.     return (int16_t)((ch1 << 8) + (ch2 << 0));  
  59.       
  60. }  
  61.   
  62. - (int32_t)readInt {  
  63.     int32_t ch1 = [self read];  
  64.     int32_t ch2 = [self read];  
  65.     int32_t ch3 = [self read];  
  66.     int32_t ch4 = [self read];  
  67.     if ((ch1 | ch2 | ch3 | ch4) < 0){  
  68.         @throw [NSException exceptionWithName:@"Exception" reason:@"EOFException" userInfo:nil];  
  69.     }  
  70.     return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));  
  71. }  
  72.   
  73. - (int64_t)readLong {  
  74.     int8_t ch[8];  
  75.     [data getBytes:&ch range:NSMakeRange(length,8)];  
  76.     length = length + 8;  
  77.   
  78.     return (((int64_t)ch[0] << 56) +  
  79.             ((int64_t)(ch[1] & 255) << 48) +  
  80.             ((int64_t)(ch[2] & 255) << 40) +  
  81.             ((int64_t)(ch[3] & 255) << 32) +  
  82.             ((int64_t)(ch[4] & 255) << 24) +  
  83.             ((ch[5] & 255) << 16) +  
  84.             ((ch[6] & 255) <<  8) +  
  85.             ((ch[7] & 255) <<  0));  
  86.   
  87. }  
  88.   
  89. - (NSString *)readUTF {  
  90.     short utfLength = [self readShort];  
  91.     NSData *d = [data subdataWithRange:NSMakeRange(length,utfLength)];  
  92.     NSString *str = [[[NSString alloc] initWithData:d encoding:NSUTF8StringEncoding] autorelease];  
  93.     length = length + utfLength;  
  94.     return str;  
  95. }  
  96.   
  97. - (void)dealloc{  
  98.     [data release];  
  99.     [super dealloc];  
  100. }  
  101.   
  102. @end  

转自:http://blog.csdn.net/jxncwzb/article/details/6694425

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值