本文整理匯總了Golang中compress/zlib.NewWriter函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewWriter函數的具體用法?Golang NewWriter怎麽用?Golang NewWriter使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewWriter函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: encodeQuery
func encodeQuery(query []byte) string {
var compressed_query bytes.Buffer
w := zlib.NewWriter(&compressed_query)
w.Write(query)
w.Close()
return base64.URLEncoding.EncodeToString(compressed_query.Bytes())
}
示例2: compress
func compress(data []byte) []byte {
var compressedData bytes.Buffer
writer := zlib.NewWriter(&compressedData)
writer.Write(data)
writer.Close()
return compressedData.Bytes()
}
示例3: SetActionPlan
func (rs *RedisStorage) SetActionPlan(key string, ats *ActionPlan, overwrite bool) (err error) {
if len(ats.ActionTimings) == 0 {
// delete the key
err = rs.db.Cmd("DEL", utils.ACTION_PLAN_PREFIX+key).Err
cache2go.RemKey(utils.ACTION_PLAN_PREFIX + key)
return err
}
if !overwrite {
// get existing action plan to merge the account ids
if existingAts, _ := rs.GetActionPlan(key, true); existingAts != nil {
if ats.AccountIDs == nil && len(existingAts.AccountIDs) > 0 {
ats.AccountIDs = make(utils.StringMap)
}
for accID := range existingAts.AccountIDs {
ats.AccountIDs[accID] = true
}
}
}
result, err := rs.ms.Marshal(ats)
if err != nil {
return err
}
var b bytes.Buffer
w := zlib.NewWriter(&b)
w.Write(result)
w.Close()
return rs.db.Cmd("SET", utils.ACTION_PLAN_PREFIX+key, b.Bytes()).Err
}
示例4: main
func main() {
app := cli.NewApp()
app.Name = "zlib"
app.Usage = "A command-line tool for using the zlib compression algorithm."
app.Action = func(c *cli.Context) {
var reader io.Reader = os.Stdin
if c.Bool("decompress") {
compressorReadCloser, err := zlib.NewReader(reader)
if err != nil {
exit(err.Error(), 1)
}
if _, err := io.Copy(os.Stdout, compressorReadCloser); err != nil {
exit(err.Error(), 1)
}
compressorReadCloser.Close()
} else {
var writer io.Writer = os.Stdout
compressorWriteCloser := zlib.NewWriter(writer)
if _, err := io.Copy(compressorWriteCloser, reader); err != nil {
exit(err.Error(), 1)
}
compressorWriteCloser.Close()
}
}
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "d, decompress",
Usage: "Decompresses the input instead of compressing the output.",
},
}
app.Run(os.Args)
}
示例5: custom
func custom(log, cors, validate bool, f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
addr := r.RemoteAddr
if ip, found := header(r, "X-Forwarded-For"); found {
addr = ip
}
// compress settings
ioWriter := w.(io.Writer)
for _, val := range misc.ParseCsvLine(r.Header.Get("Accept-Encoding")) {
if val == "gzip" {
w.Header().Set("Content-Encoding", "gzip")
g := gzip.NewWriter(w)
defer g.Close()
ioWriter = g
break
}
if val == "deflate" {
w.Header().Set("Content-Encoding", "deflate")
z := zlib.NewWriter(w)
defer z.Close()
ioWriter = z
break
}
}
writer := &customResponseWriter{Writer: ioWriter, ResponseWriter: w, status: http.StatusOK}
// route to the controllers
f(writer, r)
// access log
if log && cfg.AccessLog {
logs.Info.Printf("%s %s %s %s", addr, strconv.Itoa(writer.status), r.Method, r.URL)
}
}
}
示例6: serializeChunkData
// serializeChunkData produces the compressed chunk NBT data.
func serializeChunkData(w *nbtChunkWriter) (chunkData []byte, err os.Error) {
// Reserve room for the chunk data header at the start.
buffer := bytes.NewBuffer(make([]byte, chunkDataHeaderSize, chunkDataGuessSize))
if zlibWriter, err := zlib.NewWriter(buffer); err != nil {
return nil, err
} else {
if err = nbt.Write(zlibWriter, w.RootTag()); err != nil {
zlibWriter.Close()
return nil, err
}
if err = zlibWriter.Close(); err != nil {
return nil, err
}
}
chunkData = buffer.Bytes()
// Write chunk data header
header := chunkDataHeader{
DataSize: uint32(len(chunkData)) - chunkDataHeaderSize,
Version: chunkCompressionZlib,
}
buffer = bytes.NewBuffer(chunkData[:0])
if err = binary.Write(buffer, binary.BigEndian, header); err != nil {
return nil, err
}
return chunkData, nil
}
示例7: custom
func custom(log, cors, validate bool, f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
// compress settings
ioWriter := w.(io.Writer)
for _, val := range misc.ParseCsvLine(r.Header.Get("Accept-Encoding")) {
if val == "gzip" {
w.Header().Set("Content-Encoding", "gzip")
g := gzip.NewWriter(w)
defer g.Close()
ioWriter = g
break
}
if val == "deflate" {
w.Header().Set("Content-Encoding", "deflate")
z := zlib.NewWriter(w)
defer z.Close()
ioWriter = z
break
}
}
writer := &customResponseWriter{Writer: ioWriter, ResponseWriter: w, status: 200}
// route to the controllers
f(writer, r)
}
}
示例8: Marshal
func Marshal(compression Compression, out io.Writer, v interface{}) (err error) {
defer func() {
if r := recover(); r != nil {
if s, ok := r.(string); ok {
err = fmt.Errorf(s)
} else {
err = r.(error)
}
}
}()
if out == nil {
panic(fmt.Errorf("nbt: Output stream is nil"))
}
switch compression {
case Uncompressed:
break
case GZip:
w := gzip.NewWriter(out)
defer w.Close()
out = w
case ZLib:
w := zlib.NewWriter(out)
defer w.Close()
out = w
default:
panic(fmt.Errorf("nbt: Unknown compression type: %d", compression))
}
writeRootTag(out, reflect.ValueOf(v))
return
}
示例9: committer
func (f *ZlibFilter) committer(fr FilterRunner, h PluginHelper, wg *sync.WaitGroup) {
initBatch := make([]byte, 0, 10000)
f.backChan <- initBatch
var (
tag string
//ok bool
outBatch []byte
)
tag = f.ZlibTag
for outBatch = range f.batchChan {
pack, e := h.PipelinePack(f.msgLoopCount)
if e != nil {
fr.LogError(e)
break
}
var b bytes.Buffer
w := zlib.NewWriter(&b)
w.Write(outBatch)
w.Close()
tagField, _ := message.NewField("ZlibTag", tag, "")
pack.Message.AddField(tagField)
pack.Message.SetUuid(uuid.NewRandom())
pack.Message.SetPayload(b.String())
fr.Inject(pack)
outBatch = outBatch[:0]
f.backChan <- outBatch
}
wg.Done()
}
示例10: SetActionPlan
func (ms *MongoStorage) SetActionPlan(key string, ats *ActionPlan, overwrite bool) error {
// clean dots from account ids map
if len(ats.ActionTimings) == 0 {
cache2go.RemKey(utils.ACTION_PLAN_PREFIX + key)
err := ms.db.C(colApl).Remove(bson.M{"key": key})
if err != mgo.ErrNotFound {
return err
}
return nil
}
if !overwrite {
// get existing action plan to merge the account ids
if existingAts, _ := ms.GetActionPlan(key, true); existingAts != nil {
if ats.AccountIDs == nil && len(existingAts.AccountIDs) > 0 {
ats.AccountIDs = make(utils.StringMap)
}
for accID := range existingAts.AccountIDs {
ats.AccountIDs[accID] = true
}
}
}
result, err := ms.ms.Marshal(ats)
if err != nil {
return err
}
var b bytes.Buffer
w := zlib.NewWriter(&b)
w.Write(result)
w.Close()
_, err = ms.db.C(colApl).Upsert(bson.M{"key": key}, &struct {
Key string
Value []byte
}{Key: key, Value: b.Bytes()})
return err
}
示例11: Compress
func Compress(data []byte) bytes.Buffer {
var b bytes.Buffer
w := zlib.NewWriter(&b)
w.Write(data)
w.Close()
return b
}
示例12: Close
func (f *file) Close() error {
if !f.closed {
f.f.Lock()
defer f.f.Unlock()
if !f.closed {
if f.f.Mode&ModeCompress != 0 {
var buf bytes.Buffer
zw := zlib.NewWriter(&buf)
if _, err := zw.Write(f.data); err != nil {
return err
}
if err := zw.Close(); err != nil {
return err
}
if buf.Len() < len(f.data) {
f.f.Data = buf.Bytes()
} else {
f.f.Mode &= ^ModeCompress
f.f.Data = f.data
}
} else {
f.f.Data = f.data
}
f.closed = true
}
}
return nil
}
示例13: compress
func compress(data []byte) []byte {
var b bytes.Buffer
w := zlib.NewWriter(&b)
w.Write(data)
w.Close()
return b.Bytes()
}
示例14: SetActionPlan
func (rs *RedisStorage) SetActionPlan(key string, ats *ActionPlan, overwrite bool, transactionID string) (err error) {
cCommit := cacheCommit(transactionID)
if len(ats.ActionTimings) == 0 {
// delete the key
err = rs.Cmd("DEL", utils.ACTION_PLAN_PREFIX+key).Err
cache.RemKey(utils.ACTION_PLAN_PREFIX+key, cCommit, transactionID)
return err
}
if !overwrite {
// get existing action plan to merge the account ids
if existingAts, _ := rs.GetActionPlan(key, true, transactionID); existingAts != nil {
if ats.AccountIDs == nil && len(existingAts.AccountIDs) > 0 {
ats.AccountIDs = make(utils.StringMap)
}
for accID := range existingAts.AccountIDs {
ats.AccountIDs[accID] = true
}
}
// do not keep this in cache (will be obsolete)
cache.RemKey(utils.ACTION_PLAN_PREFIX+key, cCommit, transactionID)
}
result, err := rs.ms.Marshal(ats)
if err != nil {
return err
}
var b bytes.Buffer
w := zlib.NewWriter(&b)
w.Write(result)
w.Close()
err = rs.Cmd("SET", utils.ACTION_PLAN_PREFIX+key, b.Bytes()).Err
cache.RemKey(utils.ACTION_PLAN_PREFIX+key, cCommit, transactionID)
return
}
示例15: save
func save(r redis.AsyncClient, key string, obj interface{}, w http.ResponseWriter) {
var b bytes.Buffer
z := zlib.NewWriter(&b)
defer z.Close()
je := json.NewEncoder(z)
err := je.Encode(obj)
if err != nil {
log.Fatal("Failed to json Encode with error: ", err)
}
z.Flush()
f, rerr := r.Set(key, b.Bytes())
if rerr != nil {
panic(rerr)
}
_, rerr, timeout := f.TryGet(50000000000)
if rerr != nil {
panic(rerr)
}
if timeout {
savetimeout++
log.Println("save timeout! count: ", savetimeout)
fmt.Fprintf(w, "Save failed for %s", key)
}
}