public class GridFs extends Object
This implementation uses a ObjectId
as the id when writing and stores
the name of the file in the files collection document's "filename" field. To
unlink(String)
or read(String, OutputStream)
a file from
the collection the _id field may contain any value but the filename field
must be present.
Modifier and Type | Field and Description |
---|---|
static String |
CHUNKS_SUFFIX
The suffix for the chunks collection.
|
static int |
DEFAULT_CHUNK_SIZE
The default chunk size.
|
static String |
DEFAULT_ROOT
The suffix for the files collection.
|
static String |
FILES_SUFFIX
The suffix for the files collection.
|
Constructor and Description |
---|
GridFs(MongoDatabase database)
Creates a new GridFs.
|
GridFs(MongoDatabase database,
String rootName)
Creates a new GridFs.
|
GridFs(String mongoDbUri)
Creates a new GridFs.
|
GridFs(String mongoDbUri,
String rootName)
Creates a new GridFs.
|
Modifier and Type | Method and Description |
---|---|
int |
getChunkSize()
Returns the size for a chunk written.
|
void |
read(String name,
OutputStream sink)
Reads a file from the GridFS collections and writes the contents to the
sink
|
void |
setChunkSize(int chunkSize)
Sets the value of size for a chunk written.
|
boolean |
unlink(String name)
Unlinks (deletes) the file from the GridFS collections.
|
void |
write(String name,
InputStream source)
Attempts to write a file into the GridFS collections using the specified
name for the file and deriving the chunks from the data read from the
source.
|
public static final String CHUNKS_SUFFIX
public static final int DEFAULT_CHUNK_SIZE
public static final String DEFAULT_ROOT
public static final String FILES_SUFFIX
public GridFs(MongoDatabase database)
The GridFS objects will be stored in the 'fs' collection.
database
- The database containing the GridFS collections.public GridFs(MongoDatabase database, String rootName)
database
- The database containing the GridFS collections.rootName
- The rootName for the collections. The FILES_SUFFIX
and CHUNKS_SUFFIX
will be appended to create the two
collection names.public GridFs(String mongoDbUri)
mongoDbUri
- The configuration for the connection to MongoDB expressed as a
MongoDB URL.IllegalArgumentException
- If the mongoDbUri is not a properly formated MongoDB
style URL.public GridFs(String mongoDbUri, String rootName)
mongoDbUri
- The configuration for the connection to MongoDB expressed as a
MongoDB URL.rootName
- The rootName for the collections. The FILES_SUFFIX
and CHUNKS_SUFFIX
will be appended to create the two
collection names.IllegalArgumentException
- If the mongoDbUri is not a properly formated MongoDB
style URL.public int getChunkSize()
public void read(String name, OutputStream sink) throws IOException
name
- The name of the file.sink
- The stream to Write the data to.IOException
- On a failure reading the data from MongoDB or writing to the
sink.public void setChunkSize(int chunkSize)
chunkSize
- The new value for the size for a chunk written.public boolean unlink(String name) throws IOException
name
- The name of the file to be deleted.IOException
- On a failure to delete the file.public void write(String name, InputStream source) throws IOException
name
- The name of the file being written.source
- The source of the bits in the file.IOException
- On a failure writing the documents or reading the file
contents. In the case of a failure an attempt is made to
remove the documents written to the collections.Copyright © 2011-2013 Allanbank Consulting, Inc.. All Rights Reserved.