Overview
Comment: | Use 0dev.org/ioutil.SizedWriter as an output buffer for pdc in both compress and decompress modes. |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
1717bfae3bf0f8bd5a8f8df29b883a56 |
User & Date: | spaskalev on 2014-12-24 22:32:42 |
Other Links: | manifest | tags |
Context
2014-12-24
| ||
23:33 | Made SizedReader faster by keeping explicit buffer indices. check-in: 701ac713de user: spaskalev tags: trunk | |
22:32 | Use 0dev.org/ioutil.SizedWriter as an output buffer for pdc in both compress and decompress modes. check-in: 1717bfae3b user: spaskalev tags: trunk | |
21:40 | Fixed SizedWriter behavior so that it follows io.Writer's Write(...) contract. Added more tests for 100% CC on the ioutil package. Predictor's compressor now uses SizedWriter and no longer has to do any internal buffering. check-in: e1778aba98 user: spaskalev tags: trunk | |
Changes
Modified src/0dev.org/commands/pdc/main.go from [c6f8b3fe28] to [1dda5424c0].
1 1 package main 2 2 3 3 import ( 4 + iou "0dev.org/ioutil" 4 5 predictor "0dev.org/predictor" 5 6 "bufio" 6 7 "fmt" 7 8 "io" 8 9 "os" 9 10 ) 10 11 ................................................................................ 20 21 } 21 22 22 23 // Compress the data from the given io.Reader and write it to the given io.Writer 23 24 // I/O is buffered for better performance 24 25 func compress(output io.Writer, input io.Reader) int { 25 26 var ( 26 27 err error 27 - buffer *bufio.Writer = bufio.NewWriter(output) 28 - compressor io.Writer = predictor.Compressor(buffer) 28 + buffer io.Writer = iou.SizedWriter(output, 4096) 29 + compressor io.Writer = predictor.Compressor(buffer) 29 30 ) 30 31 31 - _, err = io.Copy(compressor, bufio.NewReader(input)) 32 + _, err = io.Copy(compressor, input) 32 33 if err != nil { 33 34 fmt.Fprintln(os.Stderr, "Error while compressing.\n", err) 34 35 return 1 35 36 } 36 37 37 38 // Flush the compressor 38 39 _, err = compressor.Write(nil) 39 40 if err != nil { 40 41 fmt.Fprintln(os.Stderr, "Error while flushing compresssor buffer.\n", err) 41 42 return 1 42 43 } 43 44 44 45 // Flush the buffer 45 - err = buffer.Flush() 46 + _, err = buffer.Write(nil) 46 47 if err != nil { 47 48 fmt.Fprintln(os.Stderr, "Error while flushing output buffer.\n", err) 48 49 return 1 49 50 } 50 51 51 52 return 0 52 53 } 53 54 54 55 // Decompress the data from the given io.Reader and write it to the given io.Writer 55 56 // I/O is buffered for better performance 56 57 func decompress(output io.Writer, input io.Reader) int { 57 58 var ( 58 59 err error 59 - buffer *bufio.Writer = bufio.NewWriter(output) 60 - decompressor io.Reader = predictor.Decompressor(bufio.NewReader(input)) 60 + buffer io.Writer = iou.SizedWriter(output, 4096) 61 + decompressor io.Reader = predictor.Decompressor(bufio.NewReader(input)) 61 62 ) 62 63 63 64 _, err = io.Copy(buffer, decompressor) 64 65 if err != nil { 65 66 fmt.Fprintln(os.Stderr, "Error while decompressing.\n", err) 66 67 return 1 67 68 } 68 69 69 - // Flush 70 - err = buffer.Flush() 70 + // Flush the buffer 71 + _, err = buffer.Write(nil) 71 72 if err != nil { 72 73 fmt.Fprintln(os.Stderr, "Error while flushing output buffer.\n", err) 73 74 return 1 74 75 } 75 76 76 77 return 0 77 78 }