github.com/balzaczyy/golucene@v0.0.0-20151210033525-d0be9ee89713/core/store/outputStream.go (about)

     1  package store
     2  
     3  import (
     4  	"hash"
     5  	"hash/crc32"
     6  	"io"
     7  )
     8  
     9  // store/OutputStreamIndexOutput.java
    10  
    11  /* Implementation class for buffered IndexOutput that writes to a WriterCloser. */
    12  type OutputStreamIndexOutput struct {
    13  	*IndexOutputImpl
    14  
    15  	crc hash.Hash32
    16  	os  io.WriteCloser
    17  
    18  	bytesWritten int64
    19  }
    20  
    21  /* Creates a new OutputStreamIndexOutput with the given buffer size. */
    22  func newOutputStreamIndexOutput(out io.WriteCloser, bufferSize int) *OutputStreamIndexOutput {
    23  	ans := &OutputStreamIndexOutput{
    24  		crc: crc32.NewIEEE(),
    25  		os:  out,
    26  	}
    27  	ans.IndexOutputImpl = NewIndexOutput(ans)
    28  	return ans
    29  }
    30  
    31  func (out *OutputStreamIndexOutput) WriteByte(b byte) error {
    32  	out.crc.Write([]byte{b})
    33  	if _, err := out.os.Write([]byte{b}); err != nil {
    34  		return err
    35  	}
    36  	out.bytesWritten++
    37  	return nil
    38  }
    39  
    40  func (out *OutputStreamIndexOutput) WriteBytes(p []byte) error {
    41  	out.crc.Write(p)
    42  	if _, err := out.os.Write(p); err != nil {
    43  		return err
    44  	}
    45  	out.bytesWritten += int64(len(p))
    46  	return nil
    47  }
    48  
    49  func (out *OutputStreamIndexOutput) Close() error {
    50  	return out.os.Close()
    51  }
    52  
    53  func (out *OutputStreamIndexOutput) FilePointer() int64 {
    54  	return out.bytesWritten
    55  }
    56  
    57  func (out *OutputStreamIndexOutput) Checksum() int64 {
    58  	return int64(out.crc.Sum32())
    59  }