NSInputStream을 사용하여 iOS에서 대용량 파일을 읽으려고하면 줄 바꿈으로 파일 줄을 구분하려고합니다 (너무 많은 메모리를 사용하므로 componentsSeparatedByCharactersInSet
을 사용하고 싶지 않습니다).UTF-8로 NSInputStream을 읽는 방법은 무엇입니까?
그러나 모든 행이 UTF-8로 인코딩 된 것처럼 보이지는 않습니다 (ASCII, 같은 바이트로 표시 될 수 있음). 나는 종종 Incorrect NSStringEncoding value 0x0000 detected. Assuming NSASCIIStringEncoding. Will stop this compatiblity mapping behavior in the near future.
경고를받습니다.
내 질문은 :이 경고를 예를 들어 예 : 컴파일러 플래그를 설정 하시겠습니까?
추가 : 바이트 스트림에서 읽은 다음 버퍼를 문자열로 변환 한 다음 문자열을 추가하면 문자열이 손상 될 수 있으므로 두 개의 버퍼 읽기를 추가/연결하지 않습니까?
아래의 예는 바이트 단위의 문자열 변환이 UTF-8 문자의 첫 번째와 두 번째 절반을 유효하지 않은 것으로 삭제한다는 것을 보여줍니다.
- (void)NSInputStreamTest {
uint8_t testString[] = {0xd0, 0x91}; // @"Б"
// Test 1: Read max 1 byte at a time of UTF-8 string
uint8_t buf1[1], buf2[1];
NSString *s1, *s2, *s3;
NSInteger c1, c2;
NSInputStream *inStream = [[NSInputStream alloc] initWithData:[[NSData alloc] initWithBytes:testString length:2]];
[inStream open];
c1 = [inStream read:buf1 maxLength:1];
s1 = [[NSString alloc] initWithBytes:buf1 length:1 encoding:NSUTF8StringEncoding];
NSLog(@"Test 1: Read %d byte(s): %@", c1, s1);
c2 = [inStream read:buf2 maxLength:1];
s2 = [[NSString alloc] initWithBytes:buf2 length:1 encoding:NSUTF8StringEncoding];
NSLog(@"Test 1: Read %d byte(s): %@", c2, s2);
s3 = [s1 stringByAppendingString:s2];
NSLog(@"Test 1: Concatenated: %@", s3);
[inStream close];
// Test 2: Read max 2 bytes at a time of UTF-8 string
uint8_t buf4[2];
NSString *s4;
NSInteger c4;
NSInputStream *inStream2 = [[NSInputStream alloc] initWithData:[[NSData alloc] initWithBytes:testString length:2]];
[inStream2 open];
c4 = [inStream2 read:buf4 maxLength:2];
s4 = [[NSString alloc] initWithBytes:buf4 length:2 encoding:NSUTF8StringEncoding];
NSLog(@"Test 2: Read %d byte(s): %@", c4, s4);
[inStream2 close];
}
출력 :
2013-02-10 21:16:23.412 Test[11144:c07] Test 1: Read 1 byte(s): (null)
2013-02-10 21:16:23.413 Test[11144:c07] Test 1: Read 1 byte(s): (null)
2013-02-10 21:16:23.413 Test[11144:c07] Test 1: Concatenated: (null)
2013-02-10 21:16:23.413 Test[11144:c07] Test 2: Read 2 byte(s): Б
그럼, 무엇이 당신의 질문입니까? –
@ 0x7fffffff 질문을 추가했습니다. 죄송합니다. 또한 2 배가 될 수도 있습니다. – Kreisquadratur