本文整理匯總了Golang中io.Writer類的典型用法代碼示例。如果您正苦於以下問題:Golang Writer類的具體用法?Golang Writer怎麽用?Golang Writer使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Writer類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: UploadFromFileToUrl
func UploadFromFileToUrl(url, fileName string, w io.Writer) (http.Header, error) {
if url == "" {
return http.Header{}, fmt.Errorf("To upload from a file to a url, I need a URL.")
}
w.Write([]byte("Uploading =>\t\t\t" + fileName + ":" + url + "\n"))
input, err := os.Open(fileName)
if err != nil {
return http.Header{}, err
}
defer input.Close()
request, err := http.NewRequest("POST", url, input)
if err != nil {
return http.Header{}, err
}
request.Header.Set("Content-Type", "application/json")
client := &http.Client{}
response, err := client.Do(request)
if err != nil {
return http.Header{}, err
}
defer response.Body.Close()
return response.Header, nil
}
示例2: handleSocks4
func (server *SocksServer) handleSocks4(reader io.Reader, writer io.Writer, auth protocol.Socks4AuthenticationRequest) error {
result := protocol.Socks4RequestGranted
if auth.Command == protocol.CmdBind {
result = protocol.Socks4RequestRejected
}
socks4Response := protocol.NewSocks4AuthenticationResponse(result, auth.Port, auth.IP[:])
responseBuffer := alloc.NewSmallBuffer().Clear()
socks4Response.Write(responseBuffer)
writer.Write(responseBuffer.Value)
responseBuffer.Release()
if result == protocol.Socks4RequestRejected {
log.Warning("Unsupported socks 4 command %d", auth.Command)
return UnsupportedSocksCommand
}
dest := v2net.NewTCPDestination(v2net.IPAddress(auth.IP[:], auth.Port))
data, err := v2net.ReadFrom(reader, nil)
if err != nil {
return err
}
packet := v2net.NewPacket(dest, data, true)
server.transport(reader, writer, packet)
return nil
}
示例3: serializeWithoutHeaders
// serializeWithoutHeaders marshals the PublicKey to w in the form of an
// OpenPGP public key packet, not including the packet header.
func (pk *PublicKeyV3) serializeWithoutHeaders(w io.Writer) (err error) {
var buf [8]byte
// Version 3
buf[0] = 3
// Creation time
t := uint32(pk.CreationTime.Unix())
buf[1] = byte(t >> 24)
buf[2] = byte(t >> 16)
buf[3] = byte(t >> 8)
buf[4] = byte(t)
// Days to expire
buf[5] = byte(pk.DaysToExpire >> 8)
buf[6] = byte(pk.DaysToExpire)
// Public key algorithm
buf[7] = byte(pk.PubKeyAlgo)
if _, err = w.Write(buf[:]); err != nil {
return
}
switch pk.PubKeyAlgo {
case PubKeyAlgoRSA, PubKeyAlgoRSAEncryptOnly, PubKeyAlgoRSASignOnly:
return writeMPIs(w, pk.n, pk.e)
}
return errors.InvalidArgumentError("bad public-key algorithm")
}
示例4: debugFailf
func debugFailf(out io.Writer, body bool, s string, v ...interface{}) func(*http.Response) os.Error {
return func(in *http.Response) os.Error {
ob, _ := http.DumpResponse(in, body)
out.Write(ob)
return os.NewError(fmt.Sprintf(s, v...))
}
}
示例5: EncodeToStream
// EncodeToStream serializes the provided object to the given writer. Overrides is ignored.
func (s *Serializer) EncodeToStream(obj runtime.Object, w io.Writer, overrides ...unversioned.GroupVersion) error {
if s.yaml {
json, err := json.Marshal(obj)
if err != nil {
return err
}
data, err := yaml.JSONToYAML(json)
if err != nil {
return err
}
_, err = w.Write(data)
return err
}
if s.pretty {
data, err := json.MarshalIndent(obj, "", " ")
if err != nil {
return err
}
_, err = w.Write(data)
return err
}
encoder := json.NewEncoder(w)
return encoder.Encode(obj)
}
示例6: emitCDATA
// emitCDATA writes to w the CDATA-wrapped plain text data s.
// It escapes CDATA directives nested in s.
func emitCDATA(w io.Writer, s []byte) error {
if len(s) == 0 {
return nil
}
if _, err := w.Write(cdataStart); err != nil {
return err
}
for {
i := bytes.Index(s, cdataEnd)
if i >= 0 && i+len(cdataEnd) <= len(s) {
// Found a nested CDATA directive end.
if _, err := w.Write(s[:i]); err != nil {
return err
}
if _, err := w.Write(cdataEscape); err != nil {
return err
}
i += len(cdataEnd)
} else {
if _, err := w.Write(s); err != nil {
return err
}
break
}
s = s[i:]
}
if _, err := w.Write(cdataEnd); err != nil {
return err
}
return nil
}
示例7: WriteValue
func (par field) WriteValue(out io.Writer, valueOf reflect.Value) error {
if (par.Flags & (fieldNotAny | fieldFollowedBy)) != 0 {
return nil
}
if par.Index < 0 { // We can not out this value in all cases but if it was literal we can do it
// TODO: Check if it is string and output only in case it is literal
p := par.Parse
v := valueOf
for {
switch tp := p.(type) {
case *ptrParser:
p = tp.Parser
if v.IsNil() {
if tp.Optional {
return nil
}
return errors.New("Ptr value is nil")
}
v = v.Elem()
break
case *literalParser:
_, err := out.Write([]byte(tp.Literal))
return err
default:
return errors.New("Could not out anonymous field if it is not literal")
}
}
} else {
f := valueOf.Field(par.Index)
return par.Parse.WriteValue(out, f)
}
}
示例8: write
func (c *ClickWindow) write(ww io.Writer) (err error) {
var tmp [2]byte
tmp[0] = byte(c.ID >> 0)
if _, err = ww.Write(tmp[:1]); err != nil {
return
}
tmp[0] = byte(c.Slot >> 8)
tmp[1] = byte(c.Slot >> 0)
if _, err = ww.Write(tmp[:2]); err != nil {
return
}
tmp[0] = byte(c.Button >> 0)
if _, err = ww.Write(tmp[:1]); err != nil {
return
}
tmp[0] = byte(c.ActionNumber >> 8)
tmp[1] = byte(c.ActionNumber >> 0)
if _, err = ww.Write(tmp[:2]); err != nil {
return
}
tmp[0] = byte(c.Mode >> 0)
if _, err = ww.Write(tmp[:1]); err != nil {
return
}
if err = c.ClickedItem.Serialize(ww); err != nil {
return
}
return
}
示例9: Print
func (vs *Vertexes) Print(w io.Writer, xPos, zPos int) (count int) {
var buf = make([]byte, 64)
copy(buf[0:2], "v ")
count = 0
for i := 0; i < len(*vs); i += 129 {
var x, z = (i / 129) / 17, (i / 129) % 17
var column = (*vs)[i : i+129]
for y, offset := range column {
if offset != -1 {
count++
var (
xa = x + xPos*16
ya = y - 64
za = z + zPos*16
)
buf = buf[:2]
buf = appendCoord(buf, xa)
buf = append(buf, ' ')
buf = appendCoord(buf, ya)
buf = append(buf, ' ')
buf = appendCoord(buf, za)
buf = append(buf, '\n')
w.Write(buf)
}
}
}
return
}
示例10: writeTxIn
// writeTxIn encodes ti to the bitcoin protocol encoding for a transaction
// input (TxIn) to w.
func writeTxIn(w io.Writer, pver uint32, version uint32, ti *TxIn) error {
err := writeOutPoint(w, pver, version, &ti.PreviousOutpoint)
if err != nil {
return err
}
slen := uint64(len(ti.SignatureScript))
err = writeVarInt(w, pver, slen)
if err != nil {
return err
}
b := []byte(ti.SignatureScript)
_, err = w.Write(b)
if err != nil {
return err
}
err = writeElement(w, &ti.Sequence)
if err != nil {
return err
}
return nil
}
示例11: pipe
func pipe(r io.Reader, w io.Writer) {
var err error
defer func() {
if err != nil && err != io.EOF {
log.Print(err)
}
}()
firstChunk := true
buf := make([]byte, cmdline.bufsize)
for {
var n int
n, err = r.Read(buf)
if n <= 0 {
return
}
if firstChunk {
firstChunk = false
} else if cmdline.maxPostDelay > 0 {
delay(cmdline.minPostDelay, cmdline.maxPostDelay)
}
_, err = w.Write(buf[:n])
if err != nil {
return
}
}
}
示例12: writeText
// Write text to w; optionally html-escaped.
func writeText(w io.Writer, text []byte, html bool) {
if html {
template.HTMLEscape(w, text)
return
}
w.Write(text)
}
示例13: writeMPI
// writeMPI serializes a big integer to w.
func writeMPI(w io.Writer, bitLength uint16, mpiBytes []byte) (err error) {
_, err = w.Write([]byte{byte(bitLength >> 8), byte(bitLength)})
if err == nil {
_, err = w.Write(mpiBytes)
}
return
}
示例14: serializeHeader
// serializeHeader writes an OpenPGP packet header to w. See RFC 4880, section
// 4.2.
func serializeHeader(w io.Writer, ptype packetType, length int) (err error) {
var buf [6]byte
var n int
buf[0] = 0x80 | 0x40 | byte(ptype)
if length < 192 {
buf[1] = byte(length)
n = 2
} else if length < 8384 {
length -= 192
buf[1] = 192 + byte(length>>8)
buf[2] = byte(length)
n = 3
} else {
buf[1] = 255
buf[2] = byte(length >> 24)
buf[3] = byte(length >> 16)
buf[4] = byte(length >> 8)
buf[5] = byte(length)
n = 6
}
_, err = w.Write(buf[:n])
return
}
示例15: ImageInsert
func (srv *Server) ImageInsert(name, url, path string, out io.Writer, sf *utils.StreamFormatter) (string, error) {
out = utils.NewWriteFlusher(out)
img, err := srv.runtime.repositories.LookupImage(name)
if err != nil {
return "", err
}
file, err := utils.Download(url, out)
if err != nil {
return "", err
}
defer file.Body.Close()
config, _, _, err := ParseRun([]string{img.ID, "echo", "insert", url, path}, srv.runtime.capabilities)
if err != nil {
return "", err
}
b := NewBuilder(srv.runtime)
c, err := b.Create(config)
if err != nil {
return "", err
}
if err := c.Inject(utils.ProgressReader(file.Body, int(file.ContentLength), out, sf.FormatProgress("Downloading", "%v/%v (%v)"), sf), path); err != nil {
return "", err
}
// FIXME: Handle custom repo, tag comment, author
img, err = b.Commit(c, "", "", img.Comment, img.Author, nil)
if err != nil {
return "", err
}
out.Write(sf.FormatStatus(img.ID))
return img.ShortID(), nil
}