forked from linkedin/goavro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers_test.go
50 lines (45 loc) · 1.33 KB
/
helpers_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
// Copyright [2019] LinkedIn Corp. Licensed under the Apache License, Version
// 2.0 (the "License"); you may not use this file except in compliance with the
// License. You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
package goavro
import (
"io"
)
// ShortWriter returns a structure that wraps an io.Writer, but returns
// io.ErrShortWrite when the number of bytes to write exceeds a preset limit.
//
// Copied with author's permission from https://github.com/karrick/gorill.
//
// bb := NopCloseBuffer()
// sw := ShortWriter(bb, 16)
//
// n, err := sw.Write([]byte("short write"))
// // n == 11, err == nil
//
// n, err := sw.Write([]byte("a somewhat longer write"))
// // n == 16, err == io.ErrShortWrite
func ShortWriter(w io.Writer, max int) io.Writer {
return shortWriter{Writer: w, max: max}
}
func (s shortWriter) Write(data []byte) (int, error) {
var short bool
index := len(data)
if index > s.max {
index = s.max
short = true
}
n, err := s.Writer.Write(data[:index])
if short {
return n, io.ErrShortWrite
}
return n, err
}
type shortWriter struct {
io.Writer
max int
}