本文整理汇总了Golang中bytes.Buffer类的典型用法代码示例。如果您正苦于以下问题:Golang Buffer类的具体用法?Golang Buffer怎么用?Golang Buffer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Buffer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: csv2String
func csv2String() string {
buf := new(bytes.Buffer)
for _, pub := range csv2.pubkeys {
buf.WriteString(fmt.Sprintf("%s,\n", pub))
}
return string(buf.Bytes())
}
示例2: bigIntToNetIPv6
// bigIntToNetIPv6 is a helper function that correctly returns a net.IP with the
// correctly padded values.
func bigIntToNetIPv6(bi *big.Int) *net.IP {
x := make(net.IP, IPv6len)
ipv6Bytes := bi.Bytes()
// It's possibe for ipv6Bytes to be less than IPv6len bytes in size. If
// they are different sizes we to pad the size of response.
if len(ipv6Bytes) < IPv6len {
buf := new(bytes.Buffer)
buf.Grow(IPv6len)
for i := len(ipv6Bytes); i < IPv6len; i++ {
if err := binary.Write(buf, binary.BigEndian, byte(0)); err != nil {
panic(fmt.Sprintf("Unable to pad byte %d of input %v: %v", i, bi, err))
}
}
for _, b := range ipv6Bytes {
if err := binary.Write(buf, binary.BigEndian, b); err != nil {
panic(fmt.Sprintf("Unable to preserve endianness of input %v: %v", bi, err))
}
}
ipv6Bytes = buf.Bytes()
}
i := copy(x, ipv6Bytes)
if i != IPv6len {
panic("IPv6 wrong size")
}
return &x
}
示例3: processTask
// Processes new tasks
func (m *etcdMinion) processTask(t *task.Task) error {
var buf bytes.Buffer
// Update state of task to indicate that we are now processing it
t.State = task.TaskStateProcessing
m.SaveTaskResult(t)
cmd := exec.Command(t.Command, t.Args...)
cmd.Stdout = &buf
cmd.Stderr = &buf
log.Printf("Processing task %s\n", t.TaskID)
cmdError := cmd.Run()
t.TimeProcessed = time.Now().Unix()
t.Result = buf.String()
if cmdError != nil {
log.Printf("Failed to process task %s\n", t.TaskID)
t.Error = cmdError.Error()
t.State = task.TaskStateFailed
} else {
log.Printf("Finished processing task %s\n", t.TaskID)
t.State = task.TaskStateSuccess
}
m.SaveTaskResult(t)
return cmdError
}
示例4: TestDumper
func TestDumper(t *testing.T) {
var in [40]byte
for i := range in {
in[i] = byte(i + 30)
}
for stride := 1; stride < len(in); stride++ {
var out bytes.Buffer
dumper := Dumper(&out)
done := 0
for done < len(in) {
todo := done + stride
if todo > len(in) {
todo = len(in)
}
dumper.Write(in[done:todo])
done = todo
}
dumper.Close()
if !bytes.Equal(out.Bytes(), expectedHexDump) {
t.Errorf("stride: %d failed. got:\n%s\nwant:\n%s", stride, out.Bytes(), expectedHexDump)
}
}
}
示例5: Format
// Format implements the NodeFormatter interface.
func (node IndexElem) Format(buf *bytes.Buffer, f FmtFlags) {
FormatNode(buf, f, node.Column)
if node.Direction != DefaultDirection {
buf.WriteByte(' ')
buf.WriteString(node.Direction.String())
}
}
示例6: TestMd5sum
func TestMd5sum(t *testing.T) {
r := NewRun(t)
defer r.Finalise()
file1 := r.WriteBoth("potato2", "------------------------------------------------------------", t1)
file2 := r.WriteBoth("empty space", "", t2)
fstest.CheckItems(t, r.fremote, file1, file2)
var buf bytes.Buffer
err := fs.Md5sum(r.fremote, &buf)
if err != nil {
t.Fatalf("List failed: %v", err)
}
res := buf.String()
if !strings.Contains(res, "d41d8cd98f00b204e9800998ecf8427e empty space\n") &&
!strings.Contains(res, " UNSUPPORTED empty space\n") &&
!strings.Contains(res, " empty space\n") {
t.Errorf("empty space missing: %q", res)
}
if !strings.Contains(res, "6548b156ea68a4e003e786df99eee76 potato2\n") &&
!strings.Contains(res, " UNSUPPORTED potato2\n") &&
!strings.Contains(res, " potato2\n") {
t.Errorf("potato2 missing: %q", res)
}
}
示例7: Render
func (self *Task) Render() {
for _, config := range self.Config.Templates {
log.Printf("Render: %s", config.Dst)
f, err := ioutil.ReadFile(self.Controller.Atom.Config.TmplDir + "/" + config.Src)
if err != nil {
log.Printf("Error:: Read - %s", err)
continue
}
var output *bytes.Buffer
if self.Controller.Atom.Config.Verbose {
output = bytes.NewBuffer(nil)
}
src := bytes.NewBuffer(f)
tmpl, err := template.New("").Funcs(self.Config.Store.FuncMap).Parse(src.String())
if err != nil {
log.Printf("Error:: Render - %s", err)
continue
}
vars := map[string]interface{}{"cli": self.Controller.Atom.CLI}
if self.Controller.Atom.Config.Verbose {
if err := tmpl.Execute(output, vars); err != nil {
log.Printf("Error:: Write - %s", err)
}
fmt.Print(output.String())
}
dst, err := os.Create(config.Dst)
if err != nil {
log.Printf("Error:: Open - %s", err)
continue
}
if err := tmpl.Execute(dst, vars); err != nil {
log.Printf("Error:: Write - %s", err)
}
}
}
示例8: TestProgressWriterIgnoreTotal
func TestProgressWriterIgnoreTotal(t *testing.T) {
filename := "progress_test.go"
f, err := os.Open(filename)
defer f.Close()
if err != nil {
log.Fatalln(err)
}
fs, err := os.Stat(filename)
if err != nil {
log.Fatalln(err)
}
p := New()
p.IgnoreTotal = true
p.Progress = func(current, total, expected int64) {
log.Println("Ignore total writing", current, total, expected)
assert.Equal(t, true, current >= total)
}
b := new(bytes.Buffer)
w := io.MultiWriter(p, b)
_, err = io.Copy(w, f)
if err != nil {
log.Fatalln(err)
}
assert.Equal(t, fs.Size(), int64(b.Len()))
}
示例9: loadNotificationConfig
// loads notification config if any for a given bucket, returns
// structured notification config.
func loadNotificationConfig(bucket string, objAPI ObjectLayer) (*notificationConfig, error) {
// Construct the notification config path.
ncPath := path.Join(bucketConfigPrefix, bucket, bucketNotificationConfig)
// Acquire a write lock on notification config before modifying.
objLock := globalNSMutex.NewNSLock(minioMetaBucket, ncPath)
objLock.RLock()
defer objLock.RUnlock()
var buffer bytes.Buffer
err := objAPI.GetObject(minioMetaBucket, ncPath, 0, -1, &buffer) // Read everything.
if err != nil {
// 'notification.xml' not found return
// 'errNoSuchNotifications'. This is default when no
// bucket notifications are found on the bucket.
if isErrObjectNotFound(err) || isErrIncompleteBody(err) {
return nil, errNoSuchNotifications
}
errorIf(err, "Unable to load bucket-notification for bucket %s", bucket)
// Returns error for other errors.
return nil, err
}
// Unmarshal notification bytes.
notificationConfigBytes := buffer.Bytes()
notificationCfg := ¬ificationConfig{}
if err = xml.Unmarshal(notificationConfigBytes, ¬ificationCfg); err != nil {
return nil, err
}
// Return success.
return notificationCfg, nil
}
示例10: TestDecompressor
func TestDecompressor(t *testing.T) {
b := new(bytes.Buffer)
for _, tt := range zlibTests {
in := bytes.NewBuffer(tt.compressed)
zlib, err := NewReaderDict(in, tt.dict)
if err != nil {
if err != tt.err {
t.Errorf("%s: NewReader: %s", tt.desc, err)
}
continue
}
defer zlib.Close()
b.Reset()
n, err := io.Copy(b, zlib)
if err != nil {
if err != tt.err {
t.Errorf("%s: io.Copy: %v want %v", tt.desc, err, tt.err)
}
continue
}
s := b.String()
if s != tt.raw {
t.Errorf("%s: got %d-byte %q want %d-byte %q", tt.desc, n, s, len(tt.raw), tt.raw)
}
}
}
示例11: BtcEncode
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
// This is part of the Message interface implementation.
func (msg *MsgAlert) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
var err error
var serializedpayload []byte
if msg.Payload != nil {
// try to Serialize Payload if possible
r := new(bytes.Buffer)
err = msg.Payload.Serialize(r, pver)
if err != nil {
// Serialize failed - ignore & fallback
// to SerializedPayload
serializedpayload = msg.SerializedPayload
} else {
serializedpayload = r.Bytes()
}
} else {
serializedpayload = msg.SerializedPayload
}
slen := uint64(len(serializedpayload))
if slen == 0 {
return messageError("MsgAlert.BtcEncode", "empty serialized payload")
}
err = WriteVarBytes(w, pver, serializedpayload)
if err != nil {
return err
}
return WriteVarBytes(w, pver, msg.Signature)
}
示例12: main
func main() {
var (
config Config
)
configFileName := flag.String("config", "", "Config file")
headerReport := flag.Bool("headerReport", false, "Produce a report of header mappings")
flag.Parse()
configFile, err := os.Open(*configFileName)
if err != nil {
fmt.Println("Error opening config:", err)
return
}
defer configFile.Close()
configBuf := new(bytes.Buffer)
configBuf.ReadFrom(configFile)
xml.Unmarshal(configBuf.Bytes(), &config)
// Parse templates
textTemplates, err := makeTemplates(&config.TemplateConfig)
if err != nil {
panic(err)
}
// Process each input file config
for _, fileConfig := range config.FileConfig {
if *headerReport {
processHeader(fileConfig, textTemplates)
continue
}
processFile(fileConfig, textTemplates)
}
}
示例13: Close
// Method Close encrypts and writes the encrypted data to the key file
func (k *KeyStore) Close(secret []byte) {
// Encode a gob of the keystore
var buff bytes.Buffer
enc := gob.NewEncoder(&buff)
enc.Encode(k)
buffString := buff.String()
// Encrypt the data
block, err := aes.NewCipher(secret)
if err != nil {
panic(err)
}
ciphertext := make([]byte, aes.BlockSize+len(buffString))
iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic(err)
}
stream := cipher.NewCFBEncrypter(block, iv)
stream.XORKeyStream(ciphertext[aes.BlockSize:], []byte(buffString))
// Write the encrypted data to the file
kFile, err := os.OpenFile(kf, os.O_WRONLY, 0644)
if err != nil {
panic(err)
}
bytesWritten, err := kFile.Write(ciphertext)
if err != nil || bytesWritten == 0 {
panic(err)
}
}
示例14: TestMultiWriter
func TestMultiWriter(t *testing.T) {
sha1 := sha1.New()
sink := new(bytes.Buffer)
mw := MultiWriter(sha1, sink)
sourceString := "My input text."
source := strings.NewReader(sourceString)
written, err := Copy(mw, source)
if written != int64(len(sourceString)) {
t.Errorf("short write of %d, not %d", written, len(sourceString))
}
if err != nil {
t.Errorf("unexpected error: %v", err)
}
sha1hex := fmt.Sprintf("%x", sha1.Sum())
if sha1hex != "01cb303fa8c30a64123067c5aa6284ba7ec2d31b" {
t.Error("incorrect sha1 value")
}
if sink.String() != sourceString {
t.Errorf("expected %q; got %q", sourceString, sink.String())
}
}
示例15: SaveGameState
// SaveGameState saves the current gamestate for a user. Does not delete old gamestates.
func (db *GameStateDB) SaveGameState(
tx *sqlx.Tx, userRow UserRow, gameState libgame.GameState) error {
var binarizedState bytes.Buffer
encoder := gob.NewEncoder(&binarizedState)
encoder.Encode(gameState)
dataStruct := GameStateRow{}
dataStruct.UserID = userRow.ID
dataStruct.BinarizedState = binarizedState.Bytes()
dataMap := make(map[string]interface{})
dataMap["user_id"] = dataStruct.UserID
dataMap["binarized_state"] = dataStruct.BinarizedState
insertResult, err := db.InsertIntoTable(tx, dataMap)
if err != nil {
logrus.Warning("error saving game state:", err)
return err
}
rowsAffected, err := insertResult.RowsAffected()
if err != nil || rowsAffected != 1 {
return errors.New(
fmt.Sprintf("expected to change 1 row, changed %d", insertResult.RowsAffected))
}
id, err := insertResult.LastInsertId()
logrus.Infof("Saved new gamestate (id %d) to db", id)
return nil
}