func (bw *BlockWriter) Close() error {
	bw.closed = true
	if bw.conn != nil {
		defer bw.conn.Close()
	}
	if bw.stream != nil {
		err := bw.stream.finish()
		if err != nil {
			return err
		}
		err = bw.finalizeBlock(bw.offset)
		if err != nil {
			return err
		}
	}
	return nil
}
Close implements io.Closer. It flushes any unwritten packets out to the
datanode, and sends a final packet indicating the end of the block.
package rpc
import (
	"errors"
	"fmt"
	"io"
	"net"
	hdfs "github.com/colinmarc/hdfs/protocol/hadoop_hdfs"
	"github.com/golang/protobuf/proto"
)
var ErrEndOfBlock = errors.New("The amount of data to be written is more than is left in the block.")
type BlockWriter struct {
	clientName string
	block      *hdfs.LocatedBlockProto
	blockSize  int64
	namenode *NamenodeConnection
	conn     net.Conn
	stream   *blockWriteStream
	offset   int64
	closed   bool
}
func NewBlockWriter(block *hdfs.LocatedBlockProto, namenode *NamenodeConnection, blockSize int64) *BlockWriter {
	s := &BlockWriter{
		clientName: namenode.ClientName(),
		block:      block,
		blockSize:  blockSize,
		namenode:   namenode,
	}
	return s
}
func (bw *BlockWriter) Write(b []byte) (int, error) {
	var blockFull bool
	if bw.offset >= bw.blockSize {
		return 0, ErrEndOfBlock
	} else if (bw.offset + int64(len(b))) > bw.blockSize {
		blockFull = true
		b = b[:bw.blockSize-bw.offset]
	}
	if bw.stream == nil {
		err := bw.connectNext()
		if err != nil {
			return 0, err
		}
	}
	n, err := bw.stream.Write(b)
	bw.offset += int64(n)
	if err == nil && blockFull {
		err = ErrEndOfBlock
	}
	return n, err
}