Godeps/_workspace/src/gopkg.in/mgo.v2/gridfs.go
func (file *GridFile) Write(data []byte) (n int, err error) {
	file.assertMode(gfsWriting)
	file.m.Lock()
	debugf("GridFile %p: writing %d bytes", file, len(data))
	defer file.m.Unlock()

	if file.err != nil {
		return 0, file.err
	}

	n = len(data)
	file.doc.Length += int64(n)
	chunkSize := file.doc.ChunkSize

	if len(file.wbuf)+len(data) < chunkSize {
		file.wbuf = append(file.wbuf, data...)
		return
	}

	// First, flush file.wbuf complementing with data.
	if len(file.wbuf) > 0 {
		missing := chunkSize - len(file.wbuf)
		if missing > len(data) {
			missing = len(data)
		}
		file.wbuf = append(file.wbuf, data[:missing]...)
		data = data[missing:]
		file.insertChunk(file.wbuf)
		file.wbuf = file.wbuf[0:0]
	}

	// Then, flush all chunks from data without copying.
	for len(data) > chunkSize {
		size := chunkSize
		if size > len(data) {
			size = len(data)
		}
		file.insertChunk(data[:size])
		data = data[size:]
	}

	// And append the rest for a future call.
	file.wbuf = append(file.wbuf, data...)

	return n, file.err
}
func (file *GridFile) Write(data []byte) (n int, err error) {
	file.assertMode(gfsWriting)
	file.m.Lock()
	debugf("GridFile %p: writing %d bytes", file, len(data))
	defer file.m.Unlock()

	if file.err != nil {
		return 0, file.err
	}

	n = len(data)
	file.doc.Length += int64(n)
	chunkSize := file.doc.ChunkSize

	if len(file.wbuf)+len(data) < chunkSize {
		file.wbuf = append(file.wbuf, data...)
		return
	}

	// First, flush file.wbuf complementing with data.
	if len(file.wbuf) > 0 {
		missing := chunkSize - len(file.wbuf)
		if missing > len(data) {
			missing = len(data)
		}
		file.wbuf = append(file.wbuf, data[:missing]...)
		data = data[missing:]
		file.insertChunk(file.wbuf)
		file.wbuf = file.wbuf[0:0]
	}

	// Then, flush all chunks from data without copying.
	for len(data) > chunkSize {
		size := chunkSize
		if size > len(data) {
			size = len(data)
		}
		file.insertChunk(data[:size])
		data = data[size:]
	}

	// And append the rest for a future call.
	file.wbuf = append(file.wbuf, data...)

	return n, file.err
}
func ( file * GridFile ) Write ( data [ ] byte ) ( n int , err error ) { file . assertMode ( gfsWriting ) file . m . Lock ( ) debugf ( " " , file , len ( data ) ) defer file . m . Unlock ( ) if file . err != nil { return 0 , file . err } n = len ( data ) file . doc . Length += int64 ( n ) chunkSize := file . doc . ChunkSize if len ( file . wbuf ) + len ( data ) < chunkSize { file . wbuf = append ( file . wbuf , data ... ) return } // First, flush file.wbuf complementing with data. if len ( file . wbuf ) > 0 { missing := chunkSize - len ( file . wbuf ) if missing > len ( data ) { missing = len ( data ) } file . wbuf = append ( file . wbuf , data [ : missing ] ... ) data = data [ missing : ] file . insertChunk ( file . wbuf ) file . wbuf = file . wbuf [ 0 : 0 ] } // Then, flush all chunks from data without copying. for len ( data ) > chunkSize { size := chunkSize if size > len ( data ) { size = len ( data ) } file . insertChunk ( data [ : size ] ) data = data [ size : ] } // And append the rest for a future call. file . wbuf = append ( file . wbuf , data ... ) return n , file . err }
// Write writes the provided data to the file and returns the
// number of bytes written and an error in case something
// wrong happened.
//
// The file will internally cache the data so that all but the last
// chunk sent to the database have the size defined by SetChunkSize.
// This also means that errors may be deferred until a future call
// to Write or Close.
//
// The parameters and behavior of this function turn the file
// into an io.Writer.
Write writes the provided data to the file and returns the number of bytes written and an error in case something wrong happened . The file will internally cache the data so that all but the last chunk sent to the database have the size defined by SetChunkSize . This also means that errors may be deferred until a future call to Write or Close . The parameters and behavior of this function turn the file into an io . Writer .