日期:2014-05-16 浏览次数:20530 次
Object _id; // 文件的唯一标识
String _filename; // 文件的名称
String _contentType; // 文件内容类型
long _length; // 文件长度
long _chunkSize; // 文件分块大小
Date _uploadDate; // 更新时间
List<String> _aliases; // 别名
DBObject _extradata = new BasicDBObject(); //额外信息
String _md5; // md5 值
// 保存文件
public void save(){
if ( _fs == null )
throw new MongoException( "need _fs" );
_fs._filesCollection.save( this );
}
// 将文件写入输出流
public long writeTo( OutputStream out )
throws IOException {
final int nc = numChunks();
// 遍历文件块,一次写入到输出流中
for ( int i=0; i<nc; i++ ){
out.write( getChunk( i ) );
}
return _length;
}
// 获取特定的文件块
byte[] getChunk( int i ){
if ( _fs == null )
throw new RuntimeException( "no gridfs!" );
// 调用 GridFS 的 _chunkCollection 的 findOne 方法
// _chunkCollection 中存放了文件数据
// 查询时指定了参数 files_id (文件的唯一标识) 和 n (序号)
DBObject chunk = _fs._chunkCollection.findOne( BasicDBObjectBuilder.start( "files_id" , _id ).add( "n" , i ).get() );
if ( chunk == null )
throw new MongoException( "can't find a chunk! file id: " + _id + " chunk: " + i );
// 获取文件块数据
return (byte[])chunk.get( "data" );
}
// 获取输入流,用于读取数据
public InputStream getInputStream(){
return new MyInputStream();
}
// 读取数据
// b 存放数据的字节数组
// off 偏移量
// len 长度
public int read(byte[] b, int off, int len){
// 偏移量超过文件大小
if ( _data == null || _offset >= _data.length ){
// 已经读完
if ( _nextChunk >= _numChunks )
return -1;
// 读取一块数据,以备使用
_data = getChunk( _nextChunk );
_offset = 0;
_nextChunk++;
}
// r:将要读取的数据的长度
int r = Math.min( len , _data.length - _offset );
// 将 _data 中指定偏移量和长度的数据复制到 b 中
System.arraycopy( _data , _offset , b , off , r );
// 增加偏移量
// 在 MyInputStream 内部记录当前的数据块读取到哪里了
// 下次再从这里开始读取
_offset += r;
// 返回读取的长度
return r;
}
// 从 GridFS 的 _filesCollection 和 _chunkCollection 中删除文件
// 与 GridFS.remove 方法的实现相似, 实际上相当于调用 _fs.remove(_id)
void remove(){
_fs._filesCollection.remove( new BasicDBObject( "_id" , _id ) );
_fs._chunkCollection.remove( new BasicDBObject( "files_id" , _id ) );
}
// 获取输出流
public OutputStream getOutputStream() {
if ( _outputStream == null ) {
_outputStream