seekstream

package
v1.4.16 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

README

seekstream

seekstream allows seeking on non-seekable io.ReadCloser source by buffering read data using memory or temp file.

var source io.ReadCloser // non-seekable
var buffer seekstream.Buffer
... 
var rs io.ReadSeekCloser = seekstream.New(source, buffer) // seekable

MemoryBuffer

Use NewMemoryBuffer(size) if total size is known and can be fit inside memory:

package main

import (
	"github.com/cshum/imagor/seekstream"
	...
)

func Test(t *testing.T) {
	source := io.NopCloser(bytes.NewBuffer([]byte("0123456789")))

	rs := seekstream.New(source, seekstream.NewMemoryBuffer(10))
	defer rs.Close()

	b := make([]byte, 4)
	_, _ = rs.Read(b)
	assert.Equal(t, "0123", string(b))

	b = make([]byte, 3)
	_, _ = rs.Seek(-2, io.SeekCurrent)
	_, _ = rs.Read(b)
	assert.Equal(t, "234", string(b))

	b = make([]byte, 4)
	_, _ = rs.Seek(-5, io.SeekEnd)
	_, _ = rs.Read(b)
	assert.Equal(t, "5678", string(b))
}

TempFileBuffer

Use NewTempFileBuffer(dir, pattern) if total size is not known or does not fit inside memory:

package main

import (
	"github.com/cshum/imagor/seekstream"
	...
)

func Test(t *testing.T) {
	source := io.NopCloser(bytes.NewBuffer([]byte("0123456789")))
	
	buffer, err := seekstream.NewTempFileBuffer("", "seekstream-") 
	assert.NoError(t, err)
	rs := seekstream.New(source, buffer)
	defer rs.Close()
	
	...
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Buffer

type Buffer interface {
	io.ReadWriteSeeker
	Clear()
}

Buffer is the underlying buffer interface for allowing the non-seekable source to become seekable

type MemoryBuffer

type MemoryBuffer struct {
	// contains filtered or unexported fields
}

MemoryBuffer Buffer implementation using memory buffer suitable for known size that can be fit inside memory

func NewMemoryBuffer

func NewMemoryBuffer(size int64) *MemoryBuffer

NewMemoryBuffer new memory buffer providing data size

func (*MemoryBuffer) Clear

func (r *MemoryBuffer) Clear()

Clear performs cleanup on stream close

func (*MemoryBuffer) Read

func (r *MemoryBuffer) Read(b []byte) (n int, err error)

Read implements the io.Reader interface.

func (*MemoryBuffer) Seek

func (r *MemoryBuffer) Seek(offset int64, whence int) (int64, error)

Seek implements the io.Seeker interface

func (*MemoryBuffer) Write

func (r *MemoryBuffer) Write(p []byte) (n int, err error)

Write implements the io.Writer interface.

type SeekStream

type SeekStream struct {
	// contains filtered or unexported fields
}

SeekStream allows seeking on non-seekable io.ReadCloser source by buffering read data using memory or temp file.

func New

func New(source io.ReadCloser, buffer Buffer) *SeekStream

New SeekStream proving io.ReadCloser source and buffer interface

func (*SeekStream) Close

func (s *SeekStream) Close() (err error)

Close implements the io.Closer interface.

func (*SeekStream) Len

func (s *SeekStream) Len() int

Len returns the number of bytes of the unread portion of buffer

func (*SeekStream) Read

func (s *SeekStream) Read(p []byte) (n int, err error)

Read implements the io.Reader interface.

func (*SeekStream) Seek

func (s *SeekStream) Seek(offset int64, whence int) (int64, error)

Seek implements the io.Seeker interface.

func (*SeekStream) Size

func (s *SeekStream) Size() int64

Size returns the length of the underlying buffer

type TempFileBuffer

type TempFileBuffer struct {
	*os.File
}

TempFileBuffer Buffer implementation using temp file suitable for unknown or large size that does not fit inside memory

func NewTempFileBuffer

func NewTempFileBuffer(dir, pattern string) (*TempFileBuffer, error)

NewTempFileBuffer new temp file buffer. Using os.CreateTemp that creates a new temporary file in the directory dir. The filename is generated by taking pattern and adding a random string to the end.

func (*TempFileBuffer) Clear

func (b *TempFileBuffer) Clear()

Clear performs cleanup on stream close

Jump to

Keyboard shortcuts

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