在 ios 下实现 DataOutputStream 和 DataInputStream

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


//
//  DataOutputStream.h
//  DataStream
//
//  Created by wangzhongbin on 11-8-16.
//  Copyright 2011 wangzhongbin. All rights reserved.
//

#import <Foundation/Foundation.h>


// 数据输出流允许应用程序以适当方式将基本数据类型写入输出流中
@interface DataOutputStream : NSObject {
	NSMutableData *data;
	NSInteger length;
}

// 将一个 char 值以 1-byte 值形式写入基础输出流中,先写入高字节。
- (void)writeChar:(int8_t)v;

//将一个 short 值以 2-byte 值形式写入基础输出流中,先写入高字节。
- (void)writeShort:(int16_t)v;

//将一个 int 值以 4-byte 值形式写入基础输出流中,先写入高字节。
- (void)writeInt:(int32_t)v;

//将一个 long 值以 8-byte 值形式写入基础输出流中,先写入高字节。
- (void)writeLong:(int64_t)v;

//以与机器无关方式使用 UTF-8 修改版编码将一个字符串写入基础输出流。
- (void)writeUTF:(NSString *)v;

//将一个 NSData byte数组写入输出流中,先写入高字节。
- (void)writeBytes:(NSData *)v;

//将此转换为 byte 序列。
- (NSData *)toByteArray;

@end


//
//  DataOutputStream.m
//  DataStream
//
//  Created by wangzhongbin on 11-8-16.
//  Copyright 2011 wangzhongbin. All rights reserved.
//

#import "DataOutputStream.h"


@implementation DataOutputStream

- (id)init{
    self = [super init];
	if(self != nil){
		data = [[NSMutableData alloc] init];
		length = 0;
	}
	return self;
}

- (void)writeChar:(int8_t)v {
	int8_t ch[1];
	ch[0] = (v & 0x0ff);
	[data appendBytes:ch length:1];
	length++;
}

- (void)writeShort:(int16_t)v {
	int8_t ch[2];
	ch[0] = (v & 0x0ff00)>>8;
	ch[1] = (v & 0x0ff);
	[data appendBytes:ch length:2];
	length = length + 2;
}

- (void)writeInt:(int32_t)v {
	int8_t ch[4];
	for(int32_t i = 0;i<4;i++){
		ch[i] = ((v >> ((3 - i)*8)) & 0x0ff);
	}
	[data appendBytes:ch length:4];
	length = length + 4;
}

- (void)writeLong:(int64_t)v {
	int8_t ch[8];
	for(int32_t i = 0;i<8;i++){
		ch[i] = ((v >> ((7 - i)*8)) & 0x0ff);
	}
	[data appendBytes:ch length:8];
	length = length + 8;
}

- (void)writeUTF:(NSString *)v {
	NSData *d = [v dataUsingEncoding:NSUTF8StringEncoding];
	NSInteger len = [d length];
	[self writeShort:len];
	[data appendData:d];
	length = length + len;
}

- (void)writeBytes:(NSData *)v {
	[data appendData:v];
	NSInteger len = [v length];
	length = length + len;
}

- (NSData *)toByteArray{
	return [[[NSData alloc] initWithData:data] autorelease];
}

- (void)dealloc{
	[data release];
	[super dealloc];
}

@end



//
//  DataInputStream.h
//  DataStream
//
//  Created by  wangzhongbin on 11-8-15.
//  Copyright 2011年 wangzhongbin. All rights reserved.
//

#import <Foundation/Foundation.h>

// 从输入流读取基本数据类型的方法,以便解组自定义值类型
@interface DataInputStream : NSObject {
	NSData *data;
	NSInteger length;
}

//
- (id)initWithData:(NSData *)data;

//
+ (id)dataInputStreamWithData:(NSData *)aData;

// 从输入流读取 char 值。
- (int8_t)readChar;

//从输入流读取 short 值。
- (int16_t)readShort;

//从输入流读取 int 值。
- (int32_t)readInt;

//从输入流读取 long 值。
- (int64_t)readLong;

//从输入流读取 NSString 字符串。
- (NSString *)readUTF;

@end


//
//  DataInputStream.m
//  DataStream
//
//  Created by  wangzhongbin on 11-8-15.
//  Copyright 2011年 wangzhongbin. 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 autorelease];
}

- (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 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
}

- (int64_t)readLong {
    int8_t ch[8];
    [data getBytes:&ch range:NSMakeRange(length,8)];
    length = length + 8;

    return (((int64_t)ch[0] << 56) +
            ((int64_t)(ch[1] & 255) << 48) +
            ((int64_t)(ch[2] & 255) << 40) +
            ((int64_t)(ch[3] & 255) << 32) +
            ((int64_t)(ch[4] & 255) << 24) +
            ((ch[5] & 255) << 16) +
            ((ch[6] & 255) <<  8) +
            ((ch[7] & 255) <<  0));

}

- (NSString *)readUTF {
    short utfLength = [self readShort];
    NSData *d = [data subdataWithRange:NSMakeRange(length,utfLength)];
    NSString *str = [[[NSString alloc] initWithData:d encoding:NSUTF8StringEncoding] autorelease];
    length = length + utfLength;
	return str;
}

- (void)dealloc{
	[data release];
	[super dealloc];
}

@end


你可能感兴趣的:(ios,exception,byte,interface,j2me,encoding)