Skip to content

Commit

Permalink
md5 nicities
Browse files Browse the repository at this point in the history
  • Loading branch information
erh committed Sep 4, 2009
1 parent 7cc51f8 commit 3c9a414
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/main/com/mongodb/gridfs/CLI.java
Expand Up @@ -120,9 +120,10 @@ public static void main(String[] args) throws Exception {
String fn = args[i+1];
GridFSInputFile f = fs.createFile( new File( fn ) );
f.save();
f.validate();
return;
}


if ( s.equals( "md5" ) ){
GridFS fs = getGridFS();
Expand Down
22 changes: 20 additions & 2 deletions src/main/com/mongodb/gridfs/GridFSFile.java
Expand Up @@ -37,6 +37,20 @@ public void save(){
_fs._filesCollection.save( this );
}

public void validate(){
if ( _fs == null )
throw new MongoException( "no _fs" );
if ( _md5 == null )
throw new MongoException( "no _md5 stored" );

DBObject res = _fs._mongo.command( new BasicDBObject( "filemd5" , _id ) );
String m = res.get( "md5" ).toString();
if ( m.equals( _md5 ) )
return;

throw new MongoException( "md5 differ. mine [" + _md5 + "] theirs [" + m + "]" );
}

public int numChunks(){
double d = _length;
d = d / _chunkSize;
Expand Down Expand Up @@ -89,7 +103,9 @@ public String getMD5(){
// ------------------------------

public Object put( String key , Object v ){
if ( key.equals( "_id" ) )
if ( key == null )
throw new RuntimeException( "key should never be null" );
else if ( key.equals( "_id" ) )
_id = v;
else if ( key.equals( "_ns" ) );
else if ( key.equals( "filename" ) )
Expand Down Expand Up @@ -117,7 +133,9 @@ else if ( key.equals( "aliases" ) ){
}

public Object get( String key ){
if ( key.equals( "_id" ) )
if ( key == null )
throw new RuntimeException( "key should never be null" );
else if ( key.equals( "_id" ) )
return _id;
else if ( key.equals( "filename" ) )
return _filename;
Expand Down
22 changes: 20 additions & 2 deletions src/main/com/mongodb/gridfs/GridFSInputFile.java
Expand Up @@ -23,6 +23,7 @@

import java.io.*;
import java.util.*;
import java.security.*;

public class GridFSInputFile extends GridFSFile {

Expand Down Expand Up @@ -73,11 +74,15 @@ public int saveChunks()
long total = 0;
int cn = 0;

MessageDigest md = _md5Pool.get();
md.reset();
DigestInputStream in = new DigestInputStream( _in , md );

while ( true ){
int start =0;

while ( start < b.length ){
int r = _in.read( b , start , b.length - start );
int r = in.read( b , start , b.length - start );
if ( r == 0 )
throw new RuntimeException( "i'm doing something wrong" );
if ( r < 0 )
Expand Down Expand Up @@ -105,13 +110,26 @@ public int saveChunks()
if ( start < b.length )
break;
}

_md5 = Util.toHex( md.digest() );
_md5Pool.done( md );

_length = total;
System.out.println( "length: " + _length );
_saved = true;
return cn;
}

final InputStream _in;
boolean _saved = false;

static SimplePool<MessageDigest> _md5Pool = new SimplePool( "md5" , 10 , -1 , false , false ){
protected MessageDigest createNew(){
try {
return MessageDigest.getInstance("MD5");
}
catch ( java.security.NoSuchAlgorithmException e ){
throw new RuntimeException( "your system doesn't have md5!" );
}
}
};
}

0 comments on commit 3c9a414

Please sign in to comment.