浏览文件
ioutilx.go
25 行 · 462 B历史下载| 1 | // SPDX-FileCopyrightText: Copyright The Lima Authors |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| 4 | package ioutilx |
| 5 | |
| 6 | import ( |
| 7 | "errors" |
| 8 | "fmt" |
| 9 | "io" |
| 10 | ) |
| 11 | |
| 12 | // ReadAtMaximum reads n at maximum. |
| 13 | func ReadAtMaximum(r io.Reader, n int64) ([]byte, error) { |
| 14 | lr := &io.LimitedReader{ |
| 15 | R: r, |
| 16 | N: n, |
| 17 | } |
| 18 | b, err := io.ReadAll(lr) |
| 19 | if err != nil { |
| 20 | if errors.Is(err, io.EOF) && lr.N <= 0 { |
| 21 | err = fmt.Errorf("exceeded the limit (%d bytes): %w", n, err) |
| 22 | } |
| 23 | } |
| 24 | return b, err |
| 25 | } |

