Diff

Differences From Artifact [4d4fd7d642]:

To Artifact [3d22f83cbe]:


1
2
3
4

5
6
7
8
9
10
11
// Package ioutil contains various constructs for io operations
package ioutil

import (

	"io"
)

// An function alias type that implements io.Writer
type WriterFunc func([]byte) (int, error)

// Delegates the call to the WriterFunc while implementing io.Writer




>







1
2
3
4
5
6
7
8
9
10
11
12
// Package ioutil contains various constructs for io operations
package ioutil

import (
	//"fmt"
	"io"
)

// An function alias type that implements io.Writer
type WriterFunc func([]byte) (int, error)

// Delegates the call to the WriterFunc while implementing io.Writer
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
}

// Returns a reader that will delegate calls to Read(...) while ensuring
// that the output buffer will never be smaller than the required size
func MinReader(reader io.Reader, size int) io.Reader {
	var buffer []byte = make([]byte, 0, size)

	return ReaderFunc(func(output []byte) (readCount int, e error) {
		var (
			bufferLength int = len(buffer)
			err          error
		)
	start:


		// Reply with the buffered data if there is any
		if bufferLength > 0 {
			readCount = copy(output, buffer)

			if readCount < bufferLength {
				// Advance the data in the buffer
				buffer = buffer[:copy(buffer, buffer[readCount:])]
			} else {

				// Clear the buffer
				buffer = buffer[:0]

			}
			// Stage any error for returning
			e, err = err, nil
			return readCount, e
		}

		// Delegate if the buffer is empty and the destination buffer is large enough
		if len(output) >= size {

			return reader.Read(output[:len(output)/size])
		}

		// Extend the buffer up to the desired size and perform a Read
		buffer = buffer[:size]
		readCount, err = reader.Read(buffer)

		// Size the buffer down to the read data size and restart
		buffer = buffer[:readCount]
		bufferLength = len(buffer)


		if bufferLength > 0 {
			goto start
		}
		// Stage any error for returning
		e, err = err, nil
		return 0, e
	})
}







|

|
|


>
>

|


<
|
|
|
>
|
|
>

|
|
|




>
|


<
|
|



|
>

|


<
<
|


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
}

// Returns a reader that will delegate calls to Read(...) while ensuring
// that the output buffer will never be smaller than the required size
func MinReader(reader io.Reader, size int) io.Reader {
	var buffer []byte = make([]byte, 0, size)

	return ReaderFunc(func(output []byte) (int, error) {
		var (
			readCount int
			err       error
		)
	start:
		//fmt.Println("Requesting read with length ", len(output), "buffer's length is ", len(buffer))

		// Reply with the buffered data if there is any
		if len(buffer) > 0 {
			readCount = copy(output, buffer)


			// Advance the data in the buffer
			buffer = buffer[:copy(buffer, buffer[readCount:])]

			//fmt.Println("After buffer read - buffer lenght is", len(buffer))

			if len(buffer) == 0 {
				return readCount, err
			}

			// Do not propagate an error until the buffer is exhausted
			return readCount, nil
		}

		// Delegate if the buffer is empty and the destination buffer is large enough
		if len(output) >= size {
			//fmt.Println("Delegating read for output length ", len(output), " and size ", size)
			return reader.Read(output[:(len(output)/size)*size])
		}


		// Perform a read into the buffer
		readCount, err = reader.Read(buffer[:size])

		// Size the buffer down to the read data size and restart
		buffer = buffer[:readCount]

		//fmt.Println("Read into buffer: ", len(buffer), "bytes")

		if len(buffer) > 0 {
			goto start
		}


		return 0, err
	})
}