github.com/blend/go-sdk@v1.20220411.3/fileutil/read_chunks.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package fileutil
     9  
    10  import (
    11  	"io"
    12  	"os"
    13  
    14  	"github.com/blend/go-sdk/ex"
    15  )
    16  
    17  // ReadChunks reads a file in `chunkSize` pieces, dispatched to the handler.
    18  func ReadChunks(filePath string, chunkSize int, handler func([]byte) error) error {
    19  	f, err := os.Open(filePath)
    20  	if err != nil {
    21  		return ex.New(err)
    22  	}
    23  	defer f.Close()
    24  
    25  	chunk := make([]byte, chunkSize)
    26  	for {
    27  		readBytes, err := f.Read(chunk)
    28  		if err == io.EOF {
    29  			break
    30  		}
    31  		readData := chunk[:readBytes]
    32  		err = handler(readData)
    33  		if err != nil {
    34  			return ex.New(err)
    35  		}
    36  	}
    37  	return nil
    38  }