//
// HessianInputStream.h
// AutoBlog
//
// Created by yang yi on 4/22/08.
// Copyright 2008 unic. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface HessianInputStream : NSObject {
NSData *bytedody;
int currentindex;
}
//@property (nonatomic, retain) NSInputStream *inputByteStream;
-(id) initwithbyte:(NSData *)content;
-(int) readInt;
-(NSString *)readString;
-(NSData *)readBytes;
//-(void) readBytes:(NSData *)bytebody;
-(unsigned long long) readLong;
-(BOOL)readBoolean;
@property (nonatomic, assign) NSData *bytedody;
@end
================================================
//
// HessianInputStream.m
// AutoBlog
//
// Created by yang yi on 4/22/08.
// Copyright 2008 unic. All rights reserved.
//
#import "HessianInputStream.h"
@implementation HessianInputStream
@synthesize bytedody;
-(id) initwithbyte:(NSData *)content
{
[super init];
//bytedody=[[NSData alloc] initWithData:content];
bytedody=content;
currentindex=0;
return self;
}
-(void) dealloc
{
currentindex=0;
//[bytedody release];
[super dealloc];
//[inputsteam release];
}
-(int) readInt
{
char *co=(char *)[bytedody bytes];
char tag=*(co+currentindex);
currentindex++;
if(tag=='I')
{
int b32=*(co+currentindex)& 0xFF;
currentindex++;
int b24=*(co+currentindex)& 0xFF;
currentindex++;
int b16=*(co+currentindex)& 0xFF;
currentindex++;
int b8=*(co+currentindex)& 0xFF;
currentindex++;
int result = (b32<<24)+(b24<<16)+(b16<<8)+b8;
// int len=[bytedody length];
return result;
}
return 0;
}
-(NSString *)readString
{
char *co=(char *)[bytedody bytes];
char tag=*(co+currentindex);
currentindex++;
if(tag=='N')
{
return NULL;
}
if(tag=='S')
{
int b16=*(co+currentindex)& 0xFF;
currentindex++;
int b8=*(co+currentindex)& 0xFF;
currentindex++;
int len=(b16<<8)+b8;
NSString * stringcontent=NULL;
for(int i=0;i<len;i++)
{
int ch=*(co+currentindex) & 0xFF;
currentindex++;
if (ch < 0x80)
{
//char * ic=(char *) &ch;
if(stringcontent)
{
//NSString * stringtemp=[NSString stringWithFormat:@"%C", ch];
stringcontent=[stringcontent stringByAppendingFormat:@"%C",ch];
//[stringtemp release];
}else
{
stringcontent= [NSString stringWithFormat:@"%C", ch];
}
}
else if ((ch & 0xe0) == 0xc0) {
int ch1 = *(co+currentindex) & 0xFF;
currentindex++;
int v = ((ch & 0x1f) << 6) + (ch1 & 0x3f);
if(stringcontent)
{
//NSString * stringtemp=[NSString stringWithFormat:@"%C", v];
stringcontent=[stringcontent stringByAppendingFormat:@"%C",v];
//[stringtemp release];
}else
{
stringcontent= [NSString stringWithFormat:@"%C", v];
}
}else if ((ch & 0xf0) == 0xe0) {
int ch1 =*(co+currentindex) & 0xFF;
currentindex++;
int ch2 =*(co+currentindex) & 0xFF;
currentindex++;
int v = ((ch & 0x0f) << 12) + ((ch1 & 0x3f) << 6) + (ch2 & 0x3f);
if(stringcontent)
{
//NSString * stringtemp=[NSString stringWithFormat:@"%C", v];
stringcontent=[stringcontent stringByAppendingFormat:@"%C",v];
//[stringtemp release];
}else
{
stringcontent= [NSString stringWithFormat:@"%C", v];
}
}
}
//NSError *error;
//[stringcontent writeToFile:@"/Users/yangyi/c.txt" atomically:TRUE encoding:NSUTF8StringEncoding error:&error];
//[error release];
//umask(022);
//FILE *newStderr = freopen("/Users/yangyi/c.txt", "a", stderr);
//NSLog(@"read string %@", stringcontent);
return stringcontent;
}
return NULL;
}
/*-(void) readBytes:(NSData *)bytebody
{
unsigned char *co=(unsigned char *)[bytedody bytes];
char tag=*(co+currentindex);
currentindex++;
if(tag=='N')
{
bytebody=NULL;
}
if(tag=='B')
{
int b16=*(co+currentindex)& 0xFF;
currentindex++;
int b8=*(co+currentindex)& 0xFF;
currentindex++;
int len=(b16<<8)+b8;
//int startbegin=currentindex;
//currentindex=startbegin+len;
unsigned char * content=malloc(len);
for(int i=0;i<len;i++)
{
*content=*(co+currentindex);
currentindex++;
content++;
}
bytebody=[[NSData alloc] initWithBytes:content length:len];
free(content);
//delete content;
}
}*/
-(NSData *)readBytes
{
char *co=(char *)[bytedody bytes];
char tag=*(co+currentindex);
currentindex++;
if(tag=='N')
{
return NULL;
}
if(tag=='B')
{
int b16=*(co+currentindex)& 0xFF;
currentindex++;
int b8=*(co+currentindex)& 0xFF;
currentindex++;
int len=(b16<<8)+b8;
int startbegin=currentindex;
currentindex=startbegin+len;
NSRange arange={startbegin,len};
NSData * bytecontent=[bytedody subdataWithRange:arange];
return bytecontent;
}
return NULL;
}
-(unsigned long long) readLong
{
char *co=(char *)[bytedody bytes];
char tag=*(co+currentindex);
currentindex++;
unsigned long long value;
if(tag=='L')
{
unsigned long long b64 = *(co+currentindex) & 0xFF;
currentindex++;
unsigned long long b56 =*(co+currentindex) & 0xFF;
currentindex++;
unsigned long long b48 =*(co+currentindex) & 0xFF;
currentindex++;
unsigned long long b40 = *(co+currentindex) & 0xFF;
currentindex++;
unsigned long long b32 =*(co+currentindex) & 0xFF;
currentindex++;
unsigned long long b24 = *(co+currentindex) & 0xFF;
currentindex++;
unsigned long long b16 = *(co+currentindex) & 0xFF;
currentindex++;
unsigned long long b8 = *(co+currentindex) & 0xFF;
currentindex++;
value = (b64 << 56) +(b56 << 48) +(b48 << 40) +(b40 << 32) +(b32 << 24) +(b24 << 16) +(b16 << 8) +b8;
}
return value;
}
-(BOOL)readBoolean
{
char *co=(char *)[bytedody bytes];
char tag=*(co+currentindex);
currentindex++;
switch (tag) {
case 'T':
{
return YES;
}
case 'F':
{
return NO;
}
}
return YES;
}
@end
=================================================
//
// HessianOutPutStream.h
// AutoBlog
//
// Created by yang yi on 4/24/08.
// Copyright 2008 unic. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface HessianOutPutStream : NSObject {
NSMutableData * bytebody;
}
@property (copy,readwrite) NSMutableData * bytebody;
-(void) writeInt:(int)aint;
-(void)writeString:(NSString *) aString;
-(void)writeBytes:(NSData *)adata;
@end
===========================================================
//
// HessianOutPutStream.m
// AutoBlog
//
// Created by yang yi on 4/24/08.
// Copyright 2008 unic. All rights reserved.
//
#import "HessianOutPutStream.h"
@implementation HessianOutPutStream
@synthesize bytebody;
-(void)dealloc
{
[bytebody release];
[super dealloc];
}
-(void) writeInt:(int)aint
{
char tag='I';
if(bytebody)
{
[bytebody appendBytes:&tag length:1];
}else
{
bytebody=[[NSMutableData alloc] initWithBytes:&tag length:1];
}
//NSString * stringcontent=[NSString stringWithFormat:@"%c", 'I'];
unsigned char b32 = aint >> 24;
unsigned char b24 = (aint >> 16) & 0x000000FF;
unsigned char b16 = (aint >> 8) & 0x000000FF;
unsigned char b8 = aint & 0x000000FF;
[bytebody appendBytes:&b32 length:1];
[bytebody appendBytes:&b24 length:1];
[bytebody appendBytes:&b16 length:1];
[bytebody appendBytes:&b8 length:1];
}
-(void)writeString:(NSString *) aString
{
char tag='S';
if(bytebody)
{
[bytebody appendBytes:&tag length:1];
}else
{
bytebody=[[NSMutableData alloc] initWithBytes:&tag length:1];
}
const char * atempstring=[aString UTF8String];
unsigned short slen = [aString length];
unsigned char b16 = slen >> 8;
unsigned char b8 = slen & 0x00FF;
[bytebody appendBytes:&b16 length:1];
[bytebody appendBytes:&b8 length:1];
[bytebody appendBytes:atempstring length:slen];
//free();
}
-(void)writeBytes:(NSData *)adata
{
char tag='B';
if(bytebody)
{
[bytebody appendBytes:&tag length:1];
}else
{
bytebody=[[NSMutableData alloc] initWithBytes:&tag length:1];
}
char * buffer=(char *)[adata bytes];
//unsigned int slen = strlen(buffer);
unsigned short int slen =[adata length];
unsigned char b16 = slen >> 8;
unsigned char b8 = slen & 0x00FF;
[bytebody appendBytes:&b16 length:1];
[bytebody appendBytes:&b8 length:1];
[bytebody appendBytes:buffer length:slen];
}
@end
不用介绍了,不知道就别用了~~~~~
THANKS [email protected]
A more complete implementation for iPhone and Mac OS X that he have implemented.
Source code, downloads, etc at:
http://sourceforge.net/projects/hessiankit
|
请发表评论