代码拉取完成,页面将自动刷新
package gobase
import (
"bufio"
"os"
)
type CacheFile struct {
filename string
fh *os.File
fw *bufio.Writer
cachesize int
filesize int
}
func NewCacheFile(filename string, cachesize int) *CacheFile {
rval := &CacheFile{filename: filename, cachesize: cachesize}
return rval
}
func (this *CacheFile) GetFileName() string {
return this.filename
}
func (this *CacheFile) GetFileSize() int {
return this.filesize
}
func (this *CacheFile) Flush() {
if this.fw != nil {
this.fw.Flush()
this.fw = nil
}
}
func (this *CacheFile) Close() error {
if this.fw != nil {
this.fw.Flush()
this.fw = nil
}
if this.fh != nil {
this.fh.Close()
this.fh = nil
}
return nil
}
func (this *CacheFile) CheckOpen() error {
if this.fw != nil {
return nil
}
f1, err := os.OpenFile(this.filename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
return err
}
info, _ := os.Stat(this.filename)
this.fh = f1
this.filesize = int(info.Size())
this.fw = bufio.NewWriterSize(f1, this.cachesize)
return nil
}
func (this *CacheFile) WriteString(s string) (int, error) {
return this.Write([]byte(s))
}
func (this *CacheFile) Write(buf []byte) (int, error) {
if this.fw == nil {
err := this.CheckOpen()
if err != nil {
return -1, err
}
}
n, err := this.fw.Write(buf)
if n > 0 {
this.filesize += n
}
return n, err
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。