Golang Reader.ReadBytes Examples

Golang Reader.ReadBytes - 30 examples found. These are the top rated real world Golang examples of bufio.Reader.ReadBytes extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
File: index.go Project: jbrukh/ggit
func parseIndexEntry(r *bufio.Reader) (entry *IndexEntry, err error) {
	var binEntry indexEntry
	err = binary.Read(r, ord, &binEntry)
	if err != nil {
		return nil, err
	}

	// TODO: what if it is corrupted and too long?
	name, e := r.ReadBytes(token.NUL)
	if e != nil {
		return nil, e
	}
	name = util.TrimLastByte(name) // get rid of NUL

	// don't ask me how I figured this out after
	// a 14 hour workday
	leftOver := 7 - (len(name)+6)%8
	for j := 0; j < leftOver; j++ {
		// TODO: read the bytes at once somehow
		if _, err = r.ReadByte(); err != nil {
			return nil, err
		}
	}

	// record the entry
	return toIndexEntry(&binEntry, string(name)), nil
}
Example #2
0
func writeTranslatedFile(context *FileContext, outFile *os.File) error {

	var sourceFile *os.File
	var sourceReader *bufio.Reader
	var line string
	var rawLine []byte
	var err error

	sourceFile, err = os.Open(context.fullPath)
	if err != nil {
		return err
	}
	defer sourceFile.Close()

	sourceReader = bufio.NewReader(sourceFile)

	for {
		rawLine, err = sourceReader.ReadBytes('\n')
		if err != nil {
			if err == io.EOF {
				break
			}
			return err
		}

		line = context.TranslateLine(string(rawLine))
		outFile.Write([]byte(line))
	}
	return nil
}
Example #3
0
func Unpack(reader *bufio.Reader) ([]byte, error) {

	return reader.ReadBytes('\n')
	//if global.PackSplitType =="breakline" {

	//	return reader.ReadBytes('\n')
	//}

	lengthByte, _ := reader.Peek(4)
	lengthBuff := bytes.NewBuffer(lengthByte)
	var length int32
	err := binary.Read(lengthBuff, binary.LittleEndian, &length)
	if err != nil {
		return nil, err
	}
	fmt.Println("Get length :", length)
	if int32(reader.Buffered()) < length+4 {
		return nil, err
	}

	pack := make([]byte, int(4+length))
	_, err = reader.Read(pack)
	if err != nil {
		return nil, err
	}
	return pack[4:], nil
}
Example #4
0
func (p Delimiter) Process(r *bufio.Reader) ([]byte, error) {
	b, err := r.ReadBytes(p.Delim)
	if err != nil {
		return nil, err
	}
	return bytes.TrimSpace(b), nil
}
Example #5
0
func readSimpleStr(r *bufio.Reader) (Resp, error) {
	b, err := r.ReadBytes(delimEnd)
	if err != nil {
		return Resp{}, err
	}
	return Resp{typ: SimpleStr, val: b[1 : len(b)-2]}, nil
}
Example #6
0
func readNetString(r *bufio.Reader) ([]byte, error) {

	lenBytes, err := r.ReadBytes(':')
	if err != nil {
		return []byte{}, err
	}
	i, err := strconv.ParseInt(string(lenBytes[:len(lenBytes)-1]), 10, 0)
	dataLen := int(i) // 0 on error, safe to check this first
	if dataLen > MAXLEN {
		err = fmt.Errorf("Proposed Length > MAXLEN")
		return []byte{}, err
	}
	if err != nil {
		err = fmt.Errorf("Error Parsing Length %#v", lenBytes)
		return []byte{}, err
	}

	data = data[:dataLen]
	_, err = io.ReadFull(r, data)
	if err != nil {
		err = fmt.Errorf("Error reading data")
		return []byte{}, err
	}
	b, err := r.ReadByte()
	if err != nil || b != ',' {
		err = fmt.Errorf("Missing terminator")
		return []byte{}, err
	}

	return data, nil
}