diff --git a/icu4c/source/io/ustream.cpp b/icu4c/source/io/ustream.cpp index 0426c804374..229783b95df 100644 --- a/icu4c/source/io/ustream.cpp +++ b/icu4c/source/io/ustream.cpp @@ -1,6 +1,6 @@ /* ********************************************************************** -* Copyright (C) 2001-2006, International Business Machines +* Copyright (C) 2001-2007, International Business Machines * Corporation and others. All Rights Reserved. ********************************************************************** * FILE NAME : ustream.cpp @@ -88,18 +88,25 @@ operator>>(STD_ISTREAM& stream, UnicodeString& str) char ch; UChar ch32; UBool intialWhitespace = TRUE; + UBool continueReading = TRUE; /* We need to consume one byte at a time to see what is considered whitespace. */ - while (!stream.eof()) { + while (continueReading) { ch = stream.get(); - sLimit = &ch + 1; - errorCode = U_ZERO_ERROR; + if (stream.eof()) { + // The EOF is only set after the get() of an unavailable byte. + stream.clear(STD_NAMESPACE ios_base::eofbit); + continueReading = FALSE; + } + sLimit = &ch + (int)continueReading; us = uBuffer; s = &ch; - ucnv_toUnicode(converter, &us, uLimit, &s, sLimit, 0, FALSE, &errorCode); + errorCode = U_ZERO_ERROR; + ucnv_toUnicode(converter, &us, uLimit, &s, sLimit, 0, !continueReading, &errorCode); if(U_FAILURE(errorCode)) { /* Something really bad happened */ - return stream; + stream.setstate(STD_NAMESPACE ios_base::failbit); + goto STOP_READING; } /* Was the character consumed? */ if (us != uBuffer) { diff --git a/icu4c/source/test/iotest/stream.cpp b/icu4c/source/test/iotest/stream.cpp index ccc70632235..49edff3cd48 100644 --- a/icu4c/source/test/iotest/stream.cpp +++ b/icu4c/source/test/iotest/stream.cpp @@ -28,9 +28,11 @@ // is deprecated on some platforms, and the compiler complains very loudly if you use it. #include #endif +#include using namespace std; #elif U_IOSTREAM_SOURCE >= 198506 #include +#include #endif #include @@ -127,9 +129,40 @@ static void U_CALLCONV TestStream(void) log_info("U_IOSTREAM_SOURCE is disabled\n"); #endif } + +static void U_CALLCONV TestStreamEOF(void) +{ + UnicodeString dest; + fstream fs(STANDARD_TEST_FILE, fstream::in | fstream::out | fstream::trunc); +#ifdef USE_SSTREAM + stringstream ss; +#else + strstream ss; +#endif + + fs << "EXAMPLE"; + fs.seekg(0); + ss << "EXAMPLE"; + + if (!(fs >> dest)) { + log_err("Reading of file did not return expected status result\n"); + } + if (dest != "EXAMPLE") { + log_err("Reading of file did not return expected string\n"); + } + + if (!(ss >> dest)) { + log_err("Reading of string did not return expected status result\n"); + } + if (dest != "EXAMPLE") { + log_err("Reading of string did not return expected string\n"); + } + fs.close(); +} U_CDECL_END U_CFUNC void addStreamTests(TestNode** root) { addTest(root, &TestStream, "stream/TestStream"); + addTest(root, &TestStreamEOF, "stream/TestStreamEOF"); }