本文整理汇总了Golang中bufio.Reader类的典型用法代码示例。如果您正苦于以下问题:Golang Reader类的具体用法?Golang Reader怎么用?Golang Reader使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Reader类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: readline
func readline(reader *bufio.Reader, buffer *bytes.Buffer) (line string, size int, err error) {
var (
segment []byte
)
for {
if segment, err = reader.ReadBytes('\n'); err != nil {
if err != io.EOF {
log.Errorf("read line failed: %s", err)
}
return
}
if _, err = buffer.Write(segment); err != nil {
log.Errorf("write buffer failed: %s", err)
return
}
if isPartialLine(segment) {
time.Sleep(1 * time.Second)
} else {
size = buffer.Len()
line = buffer.String()
buffer.Reset()
line = strings.TrimRight(line, "\r\n")
return
}
}
return
}
示例2: IsXM
func IsXM(reader *bufio.Reader) bool {
header, e := reader.Peek(17)
if e != nil {
return false
}
return bytes.Equal(header[0:17], xmHeader)
}
示例3: nextLine
func nextLine(reader *bufio.Reader) []byte {
line, _, err := reader.ReadLine()
handleError(err)
return line
}
示例4: readTrimmedLine
func readTrimmedLine(r *bufio.Reader) ([]byte, error) {
if line, err := r.ReadBytes('\n'); err != nil {
return nil, err
} else {
return bytes.TrimRight(line, "\r\n"), nil
}
}
示例5: readError
func readError(r *bufio.Reader) (*Message, error) {
b, err := r.ReadBytes(delimEnd)
if err != nil {
return nil, err
}
return &Message{Type: Err, val: b[1 : len(b)-2], raw: b}, nil
}
示例6: ReadTillEOL
func ReadTillEOL(reader *bufio.Reader, buf []byte) int {
i := 0
for {
ch, err := reader.ReadByte()
if err != nil {
return -1
}
if i >= len(buf) {
return i
}
if ch == '\r' {
ch, err := reader.ReadByte()
if err != nil {
return -1
}
if ch == '\n' {
return i
} else {
buf[i] = '\r'
buf[i+1] = ch
i += 1
}
} else {
buf[i] = ch
}
i += 1
}
return i
}
示例7: escapeKey
// translate EscX to Meta+X
func escapeKey(r rune, reader *bufio.Reader) rune {
switch r {
case 'b':
r = MetaBackward
case 'f':
r = MetaForward
case 'd':
r = MetaDelete
case CharTranspose:
r = MetaTranspose
case CharBackspace:
r = MetaBackspace
case 'O':
d, _, _ := reader.ReadRune()
switch d {
case 'H':
r = CharLineStart
case 'F':
r = CharLineEnd
default:
reader.UnreadRune()
}
case CharEsc:
}
return r
}
示例8: parseStatsSettingsResponse
func parseStatsSettingsResponse(r *bufio.Reader, stats *SettingsStats) error {
pattern := "STAT %s %s\r\n"
var (
key string
value []byte
)
for {
line, err := r.ReadSlice('\n')
if err != nil {
return err
}
if bytes.Equal(line, resultEnd) {
return nil
}
n, err := fmt.Sscanf(string(line), pattern, &key, &value)
if err != nil || n != 2 {
return fmt.Errorf("memcache: unexpected line in stats response: %q", line)
}
err = stats.Set(key, value)
if err != nil && err != ErrInvalidStatsKey {
return err
}
}
panic("unreached")
}
示例9: parseStatsItemsResponse
func parseStatsItemsResponse(r *bufio.Reader, slabMap map[int]*ItemStats) error {
pattern := "STAT items:%d:%s %s\r\n"
var (
slabIndex int
key string
value []byte
)
for {
line, err := r.ReadSlice('\n')
if err != nil {
return err
}
if bytes.Equal(line, resultEnd) {
return nil
}
n, err := fmt.Sscanf(string(line), pattern, &slabIndex, &key, &value)
if err != nil || n != 3 {
return fmt.Errorf("memcache: unexpected line in stats items response: %q", line)
}
_, ok := slabMap[slabIndex]
if !ok {
slabMap[slabIndex] = new(ItemStats)
}
err = slabMap[slabIndex].Set(key, value)
if err != nil && err != ErrInvalidStatsKey {
return err
}
}
panic("unreached")
}
示例10: readBulkData
func readBulkData(conn *bufio.Reader, len int64) ([]byte, os.Error) {
buff := make([]byte, len)
_, e := io.ReadFull(conn, buff)
if e != nil {
return nil, NewErrorWithCause(SYSTEM_ERR, "Error while attempting read of bulkdata", e)
}
// fmt.Println ("Read ", n, " bytes. data: ", buff);
crb, e1 := conn.ReadByte()
if e1 != nil {
return nil, os.NewError("Error while attempting read of bulkdata terminal CR:" + e1.String())
}
if crb != CR_BYTE {
return nil, os.NewError("<BUG> bulkdata terminal was not CR as expected")
}
lfb, e2 := conn.ReadByte()
if e2 != nil {
return nil, os.NewError("Error while attempting read of bulkdata terminal LF:" + e2.String())
}
if lfb != LF_BYTE {
return nil, os.NewError("<BUG> bulkdata terminal was not LF as expected.")
}
return buff, nil
}
示例11: parseGetResponse
// parseGetResponse reads a GET response from r and calls cb for each
// read and allocated Item
func parseGetResponse(r *bufio.Reader, cb func(*Item)) error {
for {
line, err := r.ReadSlice('\n')
if err != nil {
return err
}
if bytes.Equal(line, resultEnd) {
return nil
}
it := new(Item)
size, err := scanGetResponseLine(line, it)
if err != nil {
return err
}
it.Value, err = ioutil.ReadAll(io.LimitReader(r, int64(size)+2))
if err != nil {
return err
}
if !bytes.HasSuffix(it.Value, crlf) {
return fmt.Errorf("memcache: corrupt get result read")
}
it.Value = it.Value[:size]
cb(it)
}
panic("unreached")
}
示例12: parseGetResponse
// parseGetResponse reads a GET response from r and calls cb for each
// read and allocated Item
func parseGetResponse(r *bufio.Reader, cb func(*Item)) error {
for {
line, err := r.ReadSlice('\n')
if err != nil {
return err
}
if bytes.Equal(line, resultEnd) {
return nil
}
it := new(Item)
var size int
n, err := fmt.Sscanf(string(line), "VALUE %s %d %d %d\r\n",
&it.Key, &it.Flags, &size, &it.casid)
if err != nil {
return err
}
if n != 4 {
return fmt.Errorf("memcache: unexpected line in get response: %q", string(line))
}
it.Value, err = ioutil.ReadAll(io.LimitReader(r, int64(size)+2))
if err != nil {
return err
}
if !bytes.HasSuffix(it.Value, crlf) {
return fmt.Errorf("memcache: corrupt get result read")
}
it.Value = it.Value[:size]
cb(it)
}
panic("unreached")
}
示例13: readBulkString
func readBulkString(r *bufio.Reader) (interface{}, error) {
_, err := r.ReadByte()
if err != nil {
return EMPTY_STRING, err
}
countAsStr, err := ReadLine(r)
if err != nil {
return EMPTY_STRING, err
}
count, err := strconv.Atoi(countAsStr)
if err != nil {
return EMPTY_STRING, err
}
if count == -1 {
return countAsStr, nil
}
line, err := ReadLine(r)
if err != nil {
return EMPTY_STRING, err
}
if len(line) != count {
return EMPTY_STRING, errors.New("Expected " + countAsStr + " characters in string and got " + line)
}
return line, nil
}
示例14: readArray
func readArray(r *bufio.Reader) (interface{}, error) {
_, err := r.ReadByte()
if err != nil {
return EMPTY_STRING, err
}
countAsStr, err := ReadLine(r)
if err != nil {
return EMPTY_STRING, err
}
arrLen, err := strconv.Atoi(countAsStr)
if err != nil {
return EMPTY_STRING, err
}
l := list.New()
for i := 0; i < arrLen; i++ {
val, err := readType(r)
if err != nil {
return EMPTY_STRING, err
}
l.PushBack(val)
}
return l, nil
}
示例15: Decode
func (c *ListCommand) Decode(r *bufio.Reader) error {
started := false
for {
resp, err := r.ReadString('\n')
if err != nil {
return err
}
// Handle the start condition
if !started {
if resp != "START\n" {
return fmt.Errorf("expect list start block")
}
started = true
continue
}
// Check for the end
if resp == "END\n" {
c.done = true
return nil
}
// Store the line
c.lines = append(c.lines, resp)
}
return nil
}