/*
Writes the current skip data to the buffers. The current document
frequency determines the max level is skip ddata is to be written to.
*/
769459
func (w *MultiLevelSkipListWriter) BufferSkip(df int) error {
	assert(df%w.skipInterval == 0)
	numLevels := 1
	df /= w.skipInterval

	// determine max level
	for (df%w.skipMultiplier) == 0 && numLevels < w.numberOfSkipLevels {
		numLevels++
		df /= w.skipMultiplier
	}

	childPointer := int64(0)

	var err error
	for level := 0; level < numLevels; level++ {
		if err = w.spi.WriteSkipData(level, w.skipBuffer[level]); err != nil {
			return err
		}

		newChildPointer := w.skipBuffer[level].FilePointer()

		if level != 0 {
			// store child pointers for all levels except the lowest
			if err = w.skipBuffer[level].WriteVLong(childPointer); err != nil {
				return err
			}
		}

		// remember the childPointer for the next level
		childPointer = newChildPointer
	}
	return nil
}
276279
func (w *syncFileWriter) Write(buf []byte) (n int, err error) {
	written := 0

	// If buf > 64k we'll have to send multiple chunks.
	// TODO Refactor this into something that can coalesce smaller writes into a single chukn.
	for len(buf) > 0 {
		// Writes < 64k have a one-to-one mapping to chunks.
		// If buffer is larger than the max, we'll return the max size and leave it up to the
		// caller to handle correctly.
		partialBuf := buf
		if len(partialBuf) > wire.SyncMaxChunkSize {
			partialBuf = partialBuf[:wire.SyncMaxChunkSize]
		}

		if err := w.sender.SendOctetString(wire.StatusSyncData); err != nil {
			return written, err
		}
		if err := w.sender.SendBytes(partialBuf); err != nil {
			return written, err
		}

		written += len(partialBuf)
		buf = buf[len(partialBuf):]
	}

	return written, nil
}
1030308
func SetBuffers(apdbg *Pdbg) {
	if apdbg == nil {
		apdbg = pdbg
	}
	apdbg.bout = bytes.NewBuffer(nil)
	apdbg.sout = bufio.NewWriter(apdbg.bout)
	apdbg.berr = bytes.NewBuffer(nil)
	apdbg.serr = bufio.NewWriter(apdbg.berr)
}
411018
func (in *DataInputImpl) SkipBytes(numBytes int64) (err error) {
	assert2(numBytes >= 0, "numBytes must be >= 0, got %v", numBytes)
	if in.skipBuffer == nil {
		in.skipBuffer = make([]byte, SKIP_BUFFER_SIZE)
	}
	assert(len(in.skipBuffer) == SKIP_BUFFER_SIZE)
	var step int
	for skipped := int64(0); skipped < numBytes; {
		step = int(numBytes - skipped)
		if SKIP_BUFFER_SIZE < step {
			step = SKIP_BUFFER_SIZE
		}
		if err = in.ReadBytesBuffered(in.skipBuffer[:step], false); err != nil {
			return
		}
		skipped += int64(step)
	}
	return nil
}
720555
func (f *Buffer) Write(p []byte) (n int, err error) {
	if f.isClosed {
		return 0, io.EOF
	}
	if f.Index < 0 {
		return 0, io.EOF
	}
	// we might have rewinded, let's reset the buffer before appending to it
	idx := int(f.Index)
	buffLen := f.Buff.Len()
	if idx != buffLen && idx <= buffLen {
		f.Buff = bytes.NewBuffer(f.Bytes()[:f.Index])
	}
	n, err = f.Buff.Write(p)

	f.Index += int64(n)
	return n, err
}
1360698
func (w *Writer) Write(data []byte) (n int, err error) {
	src := data
	if w.n > 0 {
		// Append bytes from data to the last remainder.
		// TODO: limit the amount copied on first try.
		n = copy(w.src[w.n:], data)
		w.n += n
		src = w.src[:w.n]
	}
	for {
		nDst, nSrc, err := w.t.Transform(w.dst, src, false)
		if _, werr := w.w.Write(w.dst[:nDst]); werr != nil {
			return n, werr
		}
		src = src[nSrc:]
		if w.n > 0 && len(src) <= n {
			// Enough bytes from w.src have been consumed. We make src point
			// to data instead to reduce the copying.
			w.n = 0
			n -= len(src)
			src = data[n:]
			if n < len(data) && (err == nil || err == ErrShortSrc) {
				continue
			}
		} else {
			n += nSrc
		}
		switch {
		case err == ErrShortDst && (nDst > 0 || nSrc > 0):
		case err == ErrShortSrc && len(src) < len(w.src):
			m := copy(w.src, src)
			// If w.n > 0, bytes from data were already copied to w.src and n
			// was already set to the number of bytes consumed.
			if w.n == 0 {
				n += m
			}
			w.n = m
			return n, nil
		case err == nil && w.n > 0:
			return n, errInconsistentByteCount
		default:
			return n, err
		}
	}
}
595291
func (lx *lexer) skip(pred func(rune) bool) {
	for {
		r := lx.next()
		if pred(r) {
			continue
		}
		lx.backup()
		lx.ignore()
		return
	}
}
957678
func (adp *BufferAdapter) Write(loc *Location, data []byte) {
	adp.bufs = append(adp.bufs, bytes.NewBuffer(data))
}
940418
func (w *MultiLevelSkipListWriter) WriteSkip(output IndexOutput) (int64, error) {
	skipPointer := output.FilePointer()
	if len(w.skipBuffer) == 0 {
		return skipPointer, nil
	}

	for level := w.numberOfSkipLevels - 1; level > 0; level-- {
		if length := w.skipBuffer[level].FilePointer(); length > 0 {
			err := output.WriteVLong(length)
			if err != nil {
				return 0, err
			}
			err = w.skipBuffer[level].WriteTo(output)
			if err != nil {
				return 0, err
			}
		}
	}
	return skipPointer, w.skipBuffer[0].WriteTo(output)
}
652739
func writeProtoStringLines(w *bytes.Buffer, skip int, lines []string) {
	// equivalent to C's isprint.
	isprint := func(c byte) bool {
		return c >= 0x20 && c < 0x7f
	}

	w.WriteByte('"')
	for lIdx, line := range lines {
		if lIdx != 0 {
			// to get a "\\n".join(lines) effect: newlines between lines, but not
			// trailing.
			w.WriteString(`\n`)
		}
		// Loop over the bytes, not the runes.
		for i := skip; i < len(line); i++ {
			// Divergence from C++: we don't escape apostrophes.
			// There's no need to escape them, and the C++ parser
			// copes with a naked apostrophe.
			switch c := line[i]; c {
			case '\n':
				w.WriteString(`\n`)
			case '\r':
				w.WriteString(`\r`)
			case '\t':
				w.WriteString(`\t`)
			case '"':
				w.WriteString(`\"`)
			case '\\':
				w.WriteString(`\\`)
			default:
				if isprint(c) {
					w.WriteByte(c)
				} else {
					fmt.Fprintf(w, "\\%03o", c)
				}
			}
		}
	}
	w.WriteByte('"')
}
1248129
func (f *decompressor) copyData() {
	buf := f.dict.writeSlice()
	if len(buf) > f.copyLen {
		buf = buf[:f.copyLen]
	}

	cnt, err := io.ReadFull(f.r, buf)
	f.roffset += int64(cnt)
	f.copyLen -= cnt
	f.dict.writeMark(cnt)
	if err != nil {
		f.err = noEOF(err)
		return
	}

	if f.dict.availWrite() == 0 || f.copyLen > 0 {
		f.toRead = f.dict.readFlush()
		f.step = (*decompressor).copyData
		return
	}
	f.finishBlock()
}
892209
func (c *C2) maxWritten() int {
	if c.totalWritten < c.blocksize {
		return c.totalWritten
	}

	return c.blocksize
}
857564
func (mBatch *goLevelDBBatch) Write() {
	err := mBatch.db.db.Write(mBatch.batch, &opt.WriteOptions{Sync: false})
	if err != nil {
		panic(err)
	}
}
1067108
func (rm *responseModifier) Write(b []byte) (int, error) {
	if rm.hijacked {
		return rm.rw.Write(b)
	}

	if len(rm.body)+len(b) > maxBufferSize {
		rm.Flush()
	}
	rm.body = append(rm.body, b...)
	return len(b), nil
}
506550
func (d *Data) Write(p []byte) (n int, err error) {
	if d.exifLoader == nil {
		d.exifLoader = C.exif_loader_new()
		runtime.SetFinalizer(d, (*Data).cleanup)
	}

	res := C.exif_loader_write(d.exifLoader, (*C.uchar)(unsafe.Pointer(&p[0])), C.uint(len(p)))

	if res == 1 {
		return len(p), nil
	}
	return len(p), ErrFoundExifInData
}
942233
func (s *UDPSession) WriteBuffers(v [][]byte) (n int, err error) {
	for {
		s.mu.Lock()
		if s.isClosed {
			s.mu.Unlock()
			return 0, errors.New(errBrokenPipe)
		}

		if s.kcp.WaitSnd() < int(s.kcp.snd_wnd) {
			for _, b := range v {
				n += len(b)
				for {
					if len(b) <= int(s.kcp.mss) {
						s.kcp.Send(b)
						break
					} else {
						s.kcp.Send(b[:s.kcp.mss])
						b = b[s.kcp.mss:]
					}
				}
			}

			if s.kcp.WaitSnd() >= int(s.kcp.snd_wnd) || !s.writeDelay {
				s.kcp.flush(false)
			}
			s.mu.Unlock()
			atomic.AddUint64(&DefaultSnmp.BytesSent, uint64(n))
			return n, nil
		}

		var timeout *time.Timer
		var c <-chan time.Time
		if !s.wd.IsZero() {
			if time.Now().After(s.wd) {
				s.mu.Unlock()
				return 0, errTimeout{}
			}
			delay := s.wd.Sub(time.Now())
			timeout = time.NewTimer(delay)
			c = timeout.C
		}
		s.mu.Unlock()

		select {
		case <-s.chWriteEvent:
		case <-c:
		case <-s.die:
		case err = <-s.chWriteError:
			if timeout != nil {
				timeout.Stop()
			}
			return n, err
		}

		if timeout != nil {
			timeout.Stop()
		}
	}
}
574905
func (c *Context) BufferSubData(target int, offset int, data interface{}) {
	c.Call("bufferSubData", target, offset, data)
}