Check-in [1717bfae3b]
Overview
SHA1:1717bfae3bf0f8bd5a8f8df29b883a569f24de1f
Date: 2014-12-24 22:32:42
User: spaskalev
Comment:Use 0dev.org/ioutil.SizedWriter as an output buffer for pdc in both compress and decompress modes.
Timelines: family | ancestors | descendants | both | trunk
Downloads: Tarball | ZIP archive
Other Links: files | file ages | folders | manifest
Tags And Properties
Context
2014-12-24
23:33
[701ac713de] Made SizedReader faster by keeping explicit buffer indices. (user: spaskalev, tags: trunk)
22:32
[1717bfae3b] Use 0dev.org/ioutil.SizedWriter as an output buffer for pdc in both compress and decompress modes. (user: spaskalev, tags: trunk)
21:40
[e1778aba98] 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. (user: spaskalev, tags: trunk)
Changes

Modified src/0dev.org/commands/pdc/main.go from [c6f8b3fe28] to [1dda5424c0].

1
2
3

4
5
6
7
8
9
10
..
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main

import (

	predictor "0dev.org/predictor"
	"bufio"
	"fmt"
	"io"
	"os"
)

................................................................................
}

// Compress the data from the given io.Reader and write it to the given io.Writer
// I/O is buffered for better performance
func compress(output io.Writer, input io.Reader) int {
	var (
		err        error
		buffer     *bufio.Writer = bufio.NewWriter(output)
		compressor io.Writer     = predictor.Compressor(buffer)
	)

	_, err = io.Copy(compressor, bufio.NewReader(input))
	if err != nil {
		fmt.Fprintln(os.Stderr, "Error while compressing.\n", err)
		return 1
	}

	// Flush the compressor
	_, err = compressor.Write(nil)
	if err != nil {
		fmt.Fprintln(os.Stderr, "Error while flushing compresssor buffer.\n", err)
		return 1
	}

	// Flush the buffer
	err = buffer.Flush()
	if err != nil {
		fmt.Fprintln(os.Stderr, "Error while flushing output buffer.\n", err)
		return 1
	}

	return 0
}

// Decompress the data from the given io.Reader and write it to the given io.Writer
// I/O is buffered for better performance
func decompress(output io.Writer, input io.Reader) int {
	var (
		err          error
		buffer       *bufio.Writer = bufio.NewWriter(output)
		decompressor io.Reader     = predictor.Decompressor(bufio.NewReader(input))
	)

	_, err = io.Copy(buffer, decompressor)
	if err != nil {
		fmt.Fprintln(os.Stderr, "Error while decompressing.\n", err)
		return 1
	}

	// Flush
	err = buffer.Flush()
	if err != nil {
		fmt.Fprintln(os.Stderr, "Error while flushing output buffer.\n", err)
		return 1
	}

	return 0
}



>







 







|
|


|













|













|
|








|
|







1
2
3
4
5
6
7
8
9
10
11
..
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main

import (
	iou "0dev.org/ioutil"
	predictor "0dev.org/predictor"
	"bufio"
	"fmt"
	"io"
	"os"
)

................................................................................
}

// Compress the data from the given io.Reader and write it to the given io.Writer
// I/O is buffered for better performance
func compress(output io.Writer, input io.Reader) int {
	var (
		err        error
		buffer     io.Writer = iou.SizedWriter(output, 4096)
		compressor io.Writer = predictor.Compressor(buffer)
	)

	_, err = io.Copy(compressor, input)
	if err != nil {
		fmt.Fprintln(os.Stderr, "Error while compressing.\n", err)
		return 1
	}

	// Flush the compressor
	_, err = compressor.Write(nil)
	if err != nil {
		fmt.Fprintln(os.Stderr, "Error while flushing compresssor buffer.\n", err)
		return 1
	}

	// Flush the buffer
	_, err = buffer.Write(nil)
	if err != nil {
		fmt.Fprintln(os.Stderr, "Error while flushing output buffer.\n", err)
		return 1
	}

	return 0
}

// Decompress the data from the given io.Reader and write it to the given io.Writer
// I/O is buffered for better performance
func decompress(output io.Writer, input io.Reader) int {
	var (
		err          error
		buffer       io.Writer = iou.SizedWriter(output, 4096)
		decompressor io.Reader = predictor.Decompressor(bufio.NewReader(input))
	)

	_, err = io.Copy(buffer, decompressor)
	if err != nil {
		fmt.Fprintln(os.Stderr, "Error while decompressing.\n", err)
		return 1
	}

	// Flush the buffer
	_, err = buffer.Write(nil)
	if err != nil {
		fmt.Fprintln(os.Stderr, "Error while flushing output buffer.\n", err)
		return 1
	}

	return 0
}