copycat

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 27, 2021 License: MIT Imports: 5 Imported by: 0

README

copycat GoDoc Report card

Package copycat provides unidirectional and bidirectional copy functions.

go get github.com/humuzhu/copycat

Internally, it use buffer pool from humuzhu/bytespool to copy data around.

Usage

src := strings.NewReader("copycat")
dst := &bytes.Buffer{}
err := copycat.Undi(dst, src)
// link two connection together
local := // net.Conn
remote := // net.Conn
err := copycat.Bidi(local, remote)

Documentation

Overview

Package copycat provides unidirectional and bidirectional copy functions.

Example
package main

import (
	"bytes"
	"fmt"
	"io"
	"strings"

	"github.com/humuzhu/copycat"
)

func main() {
	// undirection copy
	src := strings.NewReader("copycat")
	dst := &bytes.Buffer{}
	err := copycat.Undi(dst, src)
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(dst.String())
	}

	// bidirectional copy
	local := &ExampleReadWriter{Reader: strings.NewReader("copy"), Writer: &bytes.Buffer{}}
	remote := &ExampleReadWriter{Reader: strings.NewReader("bidi"), Writer: &bytes.Buffer{}}
	err = copycat.Bidi(local, remote)
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println("copycat", local.Writer.String(), remote.Writer.String())
	}

}

type ExampleReadWriter struct {
	Reader io.Reader
	Writer *bytes.Buffer
}

func (rw *ExampleReadWriter) Read(p []byte) (n int, err error) {
	return rw.Reader.Read(p)
}

func (rw *ExampleReadWriter) Write(p []byte) (n int, err error) {
	return rw.Writer.Write(p)
}
Output:

copycat
copycat bidi copy

Index

Examples

Constants

View Source
const (
	// DefaultBlockSize is the default block size used in copy working.
	DefaultBlockSize = 32 * 1024
)

Variables

This section is empty.

Functions

func Bidi

func Bidi(u, v io.ReadWriter, options ...Option) error

Bidi do bidirectional copy works.

func Undi

func Undi(dst io.Writer, src io.Reader, options ...Option) error

Undi do unidirectional copy works.

Types

type Option

type Option func(opt *copyOption)

Option represents setting of works.

func WithBlockSize

func WithBlockSize(n int) Option

WithBlockSize sets the block size used in copy works.

func WithContext

func WithContext(ctx context.Context) Option

WithContext sets the context used in copy works. If context canceled, copy function returns with error.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL