DataOutputStreamとData InputStreamをiosで実現


私たちはjavaで通信しています。 DataOutputStreamとData InputStreamというデータの流れは、クライアントがandroidとj 2 meであれば大きな問題はありません。この問題を解決するために、私はiosの下でカプセル化しました。 DataOutputStreamとData InputStreamの2つの種類が参考になります。
//
//  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