Golang Reader Examples

Golang Reader - 30 examples found. These are the top rated real world Golang examples of bufio.Reader extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
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
}
Example #2
0
func IsXM(reader *bufio.Reader) bool {
	header, e := reader.Peek(17)
	if e != nil {
		return false
	}
	return bytes.Equal(header[0:17], xmHeader)
}
func nextLine(reader *bufio.Reader) []byte {
	line, _, err := reader.ReadLine()

	handleError(err)

	return line
}
Example #4
0
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
	}
}
Example #5
0
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
}
Example #6
0
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
}