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.4 KiB
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package starainrt
import (
"errors"
"fmt"
"io"
"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
}
//Exits返回指定文件夹/文件是否存在
func Exists(filepath string) bool {
_, err := os.Stat(filepath)
if err != nil && os.IsNotExist(err) {
return false
}
return true
}
//IsFile返回给定文件地址是否是一个文件
//True为是一个文件,False为不是文件或路径无效
func IsFile(fpath string) bool {
s, err := os.Stat(fpath)
if err != nil {
return false
}
return !s.IsDir()
}
//IsFolder返回给定文件地址是否是一个文件夹
//True为是一个文件夹,False为不是文件夹或路径无效
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
}
}
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
}
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]
}
/*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
}
bts[i] = b
ret++
}
if e.isClose {
return ret, io.EOF
}
return ret, nil
}
func (e *CircleByteBuffer) Write(bts []byte) (int, error) {
if e.isClose {
return 0, io.EOF
}
if bts == nil {
e.isEnd = true
return 0, io.EOF
}
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
}
ret++
}
if e.isClose {
return ret, io.EOF
}
return ret, nil
}