@@ -407,6 +407,7 @@ pub enum IoErrorKind {
407
407
MismatchedFileTypeForOperation ,
408
408
ResourceUnavailable ,
409
409
IoUnavailable ,
410
+ InvalidInput ,
410
411
}
411
412
412
413
// FIXME: #8242 implementing manually because deriving doesn't work for some reason
@@ -430,6 +431,7 @@ impl ToStr for IoErrorKind {
430
431
IoUnavailable => ~"IoUnavailable ",
431
432
ResourceUnavailable => ~"ResourceUnavailable ",
432
433
ConnectionAborted => ~"ConnectionAborted ",
434
+ InvalidInput => ~"InvalidInput ",
433
435
}
434
436
}
435
437
}
@@ -601,6 +603,23 @@ pub trait Reader {
601
603
return buf;
602
604
}
603
605
606
+ /// Reads all of the remaining bytes of this stream, interpreting them as a
607
+ /// UTF-8 encoded stream. The corresponding string is returned.
608
+ ///
609
+ /// # Failure
610
+ ///
611
+ /// This function will raise all the same conditions as the `read` method,
612
+ /// along with raising a condition if the input is not valid UTF-8.
613
+ fn read_to_str ( & mut self ) -> ~str {
614
+ match str:: from_utf8_owned_opt ( self . read_to_end ( ) ) {
615
+ Some ( s) => s,
616
+ None => {
617
+ io_error:: cond. raise ( standard_error ( InvalidInput ) ) ;
618
+ ~""
619
+ }
620
+ }
621
+ }
622
+
604
623
/// Create an iterator that reads a single byte on
605
624
/// each iteration, until EOF.
606
625
///
@@ -861,6 +880,28 @@ pub trait Writer {
861
880
/// decide whether their stream needs to be buffered or not.
862
881
fn flush ( & mut self ) { }
863
882
883
+ /// Write a rust string into this sink.
884
+ ///
885
+ /// The bytes written will be the UTF-8 encoded version of the input string.
886
+ /// If other encodings are desired, it is recommended to compose this stream
887
+ /// with another performing the conversion, or to use `write` with a
888
+ /// converted byte-array instead.
889
+ fn write_str ( & mut self , s : & str ) {
890
+ self . write ( s. as_bytes ( ) ) ;
891
+ }
892
+
893
+ /// Writes a string into this sink, and then writes a literal newline (`\n`)
894
+ /// byte afterwards. Note that the writing of the newline is *not* atomic in
895
+ /// the sense that the call to `write` is invoked twice (once with the
896
+ /// string and once with a newline character).
897
+ ///
898
+ /// If other encodings or line ending flavors are desired, it is recommended
899
+ /// that the `write` method is used specifically instead.
900
+ fn write_line ( & mut self , s : & str ) {
901
+ self . write_str ( s) ;
902
+ self . write ( [ '\n' as u8 ] ) ;
903
+ }
904
+
864
905
/// Write the result of passing n through `int::to_str_bytes`.
865
906
fn write_int ( & mut self , n : int ) {
866
907
int:: to_str_bytes ( n, 10 u, |bytes| self . write ( bytes) )
@@ -1053,7 +1094,7 @@ pub trait Buffer: Reader {
1053
1094
/// so they should no longer be returned in calls to `fill` or `read`.
1054
1095
fn consume ( & mut self , amt : uint ) ;
1055
1096
1056
- /// Reads the next line of input, interpreted as a sequence of utf -8
1097
+ /// Reads the next line of input, interpreted as a sequence of UTF -8
1057
1098
/// encoded unicode codepoints. If a newline is encountered, then the
1058
1099
/// newline is contained in the returned string.
1059
1100
///
@@ -1062,7 +1103,7 @@ pub trait Buffer: Reader {
1062
1103
/// This function will raise on the `io_error` condition (except for
1063
1104
/// `EndOfFile` which is swallowed) if a read error is encountered.
1064
1105
/// The task will also fail if sequence of bytes leading up to
1065
- /// the newline character are not valid utf -8.
1106
+ /// the newline character are not valid UTF -8.
1066
1107
fn read_line ( & mut self ) -> Option < ~str > {
1067
1108
self . read_until ( '\n' as u8 ) . map ( str:: from_utf8_owned)
1068
1109
}
@@ -1246,29 +1287,17 @@ pub trait Decorator<T> {
1246
1287
}
1247
1288
1248
1289
pub fn standard_error ( kind : IoErrorKind ) -> IoError {
1249
- match kind {
1250
- PreviousIoError => {
1251
- IoError {
1252
- kind : PreviousIoError ,
1253
- desc : "Failing due to a previous I/O error" ,
1254
- detail : None
1255
- }
1256
- }
1257
- EndOfFile => {
1258
- IoError {
1259
- kind : EndOfFile ,
1260
- desc : "End of file" ,
1261
- detail : None
1262
- }
1263
- }
1264
- IoUnavailable => {
1265
- IoError {
1266
- kind : IoUnavailable ,
1267
- desc : "I/O is unavailable" ,
1268
- detail : None
1269
- }
1270
- }
1290
+ let desc = match kind {
1291
+ PreviousIoError => "failing due to previous I/O error" ,
1292
+ EndOfFile => "end of file" ,
1293
+ IoUnavailable => "I/O is unavailable" ,
1294
+ InvalidInput => "invalid input" ,
1271
1295
_ => fail ! ( )
1296
+ } ;
1297
+ IoError {
1298
+ kind : kind,
1299
+ desc : desc,
1300
+ detail : None ,
1272
1301
}
1273
1302
}
1274
1303
0 commit comments