You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
starainrt/starainrt.go

194 lines
3.5 KiB
Go

6 years ago
package starainrt
import (
"errors"
"fmt"
"io"
6 years ago
"os"
"time"
)
var HttpNul, HttpNul2 map[string]string
var HttpTimeOut int64 = 15
var ShellRes, ShellErr string
var ShellExit bool
type CircleByteBuffer struct {
io.Reader
io.Writer
io.Closer
datas []byte
start int
end int
size int
isClose bool
isEnd bool
}
func NewCircleByteBuffer(len int) *CircleByteBuffer {
var e = new(CircleByteBuffer)
e.datas = make([]byte, len)
e.start = 0
e.end = 0
e.size = len
e.isClose = false
e.isEnd = false
return e
}
5 years ago
// Exists 返回指定文件夹/文件是否存在
6 years ago
func Exists(filepath string) bool {
_, err := os.Stat(filepath)
if err != nil && os.IsNotExist(err) {
return false
}
return true
}
5 years ago
// IsFile 返回给定文件地址是否是一个文件,
//True为是一个文件,False为不是文件或路径无效
6 years ago
func IsFile(fpath string) bool {
s, err := os.Stat(fpath)
if err != nil {
return false
}
return !s.IsDir()
}
5 years ago
// IsFolder 返回给定文件地址是否是一个文件夹,
//True为是一个文件夹,False为不是文件夹或路径无效
6 years ago
func IsFolder(fpath string) bool {
s, err := os.Stat(fpath)
if err != nil {
return false
}
return s.IsDir()
}
func (e *CircleByteBuffer) getLen() int {
if e.start == e.end {
return 0
} else if e.start < e.end {
return e.end - e.start
} else {
return e.start - e.end
}
6 years ago
}
func (e *CircleByteBuffer) getFree() int {
return e.size - e.getLen()
}
func (e *CircleByteBuffer) putByte(b byte) error {
if e.isClose {
return io.EOF
}
e.datas[e.end] = b
var pos = e.end + 1
for pos == e.start {
if e.isClose {
return io.EOF
}
time.Sleep(time.Microsecond)
}
if pos == e.size {
e.end = 0
} else {
e.end = pos
}
return nil
6 years ago
}
func (e *CircleByteBuffer) getByte() (byte, error) {
if e.isClose {
return 0, io.EOF
}
if e.isEnd && e.getLen() <= 0 {
return 0, io.EOF
}
if e.getLen() <= 0 {
return 0, errors.New("no datas")
}
var ret = e.datas[e.start]
e.start++
if e.start == e.size {
e.start = 0
}
return ret, nil
}
func (e *CircleByteBuffer) geti(i int) byte {
if i >= e.getLen() {
panic("out buffer")
}
var pos = e.start + i
if pos >= e.size {
pos -= e.size
}
return e.datas[pos]
6 years ago
}
/*func (e*CircleByteBuffer)puts(bts []byte){
for i:=0;i<len(bts);i++{
e.put(bts[i])
}
}
func (e*CircleByteBuffer)gets(bts []byte)int{
if bts==nil {return 0}
var ret=0
for i:=0;i<len(bts);i++{
if e.getLen()<=0{break}
bts[i]=e.get()
ret++
}
return ret
}*/
func (e *CircleByteBuffer) Close() error {
e.isClose = true
return nil
}
func (e *CircleByteBuffer) Read(bts []byte) (int, error) {
if e.isClose {
return 0, io.EOF
}
if bts == nil {
return 0, errors.New("bts is nil")
}
var ret = 0
for i := 0; i < len(bts); i++ {
b, err := e.getByte()
if err != nil {
if err == io.EOF {
return ret, err
}
return ret, nil
6 years ago
}
bts[i] = b
ret++
6 years ago
}
if e.isClose {
return ret, io.EOF
6 years ago
}
return ret, nil
}
func (e *CircleByteBuffer) Write(bts []byte) (int, error) {
if e.isClose {
return 0, io.EOF
6 years ago
}
if bts == nil {
e.isEnd = true
return 0, io.EOF
6 years ago
}
var ret = 0
for i := 0; i < len(bts); i++ {
err := e.putByte(bts[i])
if err != nil {
fmt.Println("Write bts err:", err)
return ret, err
6 years ago
}
ret++
6 years ago
}
if e.isClose {
return ret, io.EOF
6 years ago
}
return ret, nil
6 years ago
}