Check-in [1717bfae3b]
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: 1717bfae3bf0f8bd5a8f8df29b883a569f24de1f
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
2
3

4
5
6
7
8
9
10
1
2
3
4
5
6
7
8
9
10
11



+







package main

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

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
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







-
-
+
+


-
+













-
+













-
-
+
+








-
-
+
+







}

// 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)
		buffer     io.Writer = iou.SizedWriter(output, 4096)
		compressor io.Writer = predictor.Compressor(buffer)
	)

	_, err = io.Copy(compressor, bufio.NewReader(input))
	_, 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.Flush()
	_, 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       *bufio.Writer = bufio.NewWriter(output)
		decompressor io.Reader     = predictor.Decompressor(bufio.NewReader(input))
		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
	err = buffer.Flush()
	// 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
}