@Override public void write(final byte[] b, final int off, final int len) { checkClosed(); notNull("b", b); if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) { throw new IndexOutOfBoundsException(); } else if (len == 0) { return; } int currentOffset = off; int lengthToWrite = len; int amountToCopy = 0; while (lengthToWrite > 0) { amountToCopy = lengthToWrite; if (amountToCopy > chunkSizeBytes - bufferOffset) { amountToCopy = chunkSizeBytes - bufferOffset; } System.arraycopy(b, currentOffset, buffer, bufferOffset, amountToCopy); bufferOffset += amountToCopy; currentOffset += amountToCopy; lengthToWrite -= amountToCopy; lengthInBytes += amountToCopy; if (bufferOffset == chunkSizeBytes) { writeChunk(); } } }
private void writeChunk() { if (bufferOffset > 0) { if (clientSession != null) { chunksCollection.insertOne(clientSession, new Document("files_id", fileId).append("n", chunkIndex) .append("data", getData())); } else { chunksCollection.insertOne(new Document("files_id", fileId).append("n", chunkIndex).append("data", getData())); } updateMD5(); chunkIndex++; bufferOffset = 0; } }
@Override public void close() { synchronized (closeLock) { if (closed) { return; } closed = true; } writeChunk(); GridFSFile gridFSFile = new GridFSFile(fileId, filename, lengthInBytes, chunkSizeBytes, new Date(), getMD5Digest(), metadata); if (clientSession != null) { filesCollection.insertOne(clientSession, gridFSFile); } else { filesCollection.insertOne(gridFSFile); } buffer = null; }
@Override public void write(final int b) { byte[] byteArray = new byte[1]; byteArray[0] = (byte) (0xFF & b); write(byteArray, 0, 1); }
private GridFSUploadStream createGridFSUploadStream(@Nullable final ClientSession clientSession, final BsonValue id, final String filename, final GridFSUploadOptions options) { notNull("options", options); Integer chunkSizeBytes = options.getChunkSizeBytes(); int chunkSize = chunkSizeBytes == null ? this.chunkSizeBytes : chunkSizeBytes; checkCreateIndex(clientSession); return new GridFSUploadStreamImpl(clientSession, filesCollection, chunksCollection, id, filename, chunkSize, disableMD5, options.getMetadata()); }
@Override @SuppressWarnings("deprecation") public ObjectId getFileId() { return getObjectId(); }
@Override public void abort() { synchronized (closeLock) { checkClosed(); closed = true; } if (clientSession != null) { chunksCollection.deleteMany(clientSession, new Document("files_id", fileId)); } else { chunksCollection.deleteMany(new Document("files_id", fileId)); } }
GridFSUploadStreamImpl(@Nullable final ClientSession clientSession, final MongoCollection<GridFSFile> filesCollection, final MongoCollection<Document> chunksCollection, final BsonValue fileId, final String filename, final int chunkSizeBytes, final boolean disableMD5, @Nullable final Document metadata) { this.clientSession = clientSession; this.filesCollection = notNull("files collection", filesCollection); this.chunksCollection = notNull("chunks collection", chunksCollection); this.fileId = notNull("File Id", fileId); this.filename = notNull("filename", filename); this.chunkSizeBytes = chunkSizeBytes; this.md5 = createMD5Digest(disableMD5); this.metadata = metadata; chunkIndex = 0; bufferOffset = 0; buffer = new byte[chunkSizeBytes]; }
@Override public void write(final byte[] b) { write(b, 0, b.length); }
private GridFSUploadStream createGridFSUploadStream(@Nullable final ClientSession clientSession, final BsonValue id, final String filename, final GridFSUploadOptions options) { notNull("options", options); Integer chunkSizeBytes = options.getChunkSizeBytes(); int chunkSize = chunkSizeBytes == null ? this.chunkSizeBytes : chunkSizeBytes; checkCreateIndex(clientSession); return new GridFSUploadStreamImpl(clientSession, filesCollection, chunksCollection, id, filename, chunkSize, disableMD5, options.getMetadata()); }
@Override @SuppressWarnings("deprecation") public ObjectId getFileId() { return getObjectId(); }
@Override public void abort() { synchronized (closeLock) { checkClosed(); closed = true; } if (clientSession != null) { chunksCollection.deleteMany(clientSession, new Document("files_id", fileId)); } else { chunksCollection.deleteMany(new Document("files_id", fileId)); } }
GridFSUploadStreamImpl(@Nullable final ClientSession clientSession, final MongoCollection<GridFSFile> filesCollection, final MongoCollection<Document> chunksCollection, final BsonValue fileId, final String filename, final int chunkSizeBytes, final boolean disableMD5, @Nullable final Document metadata) { this.clientSession = clientSession; this.filesCollection = notNull("files collection", filesCollection); this.chunksCollection = notNull("chunks collection", chunksCollection); this.fileId = notNull("File Id", fileId); this.filename = notNull("filename", filename); this.chunkSizeBytes = chunkSizeBytes; this.md5 = createMD5Digest(disableMD5); this.metadata = metadata; chunkIndex = 0; bufferOffset = 0; buffer = new byte[chunkSizeBytes]; }
private void writeChunk() { if (bufferOffset > 0) { if (clientSession != null) { chunksCollection.insertOne(clientSession, new Document("files_id", fileId).append("n", chunkIndex) .append("data", getData())); } else { chunksCollection.insertOne(new Document("files_id", fileId).append("n", chunkIndex).append("data", getData())); } updateMD5(); chunkIndex++; bufferOffset = 0; } }
@Override public void close() { synchronized (closeLock) { if (closed) { return; } closed = true; } writeChunk(); GridFSFile gridFSFile = new GridFSFile(fileId, filename, lengthInBytes, chunkSizeBytes, new Date(), getMD5Digest(), metadata); if (clientSession != null) { filesCollection.insertOne(clientSession, gridFSFile); } else { filesCollection.insertOne(gridFSFile); } buffer = null; }
@Override public void write(final byte[] b, final int off, final int len) { checkClosed(); notNull("b", b); if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) { throw new IndexOutOfBoundsException(); } else if (len == 0) { return; } int currentOffset = off; int lengthToWrite = len; int amountToCopy = 0; while (lengthToWrite > 0) { amountToCopy = lengthToWrite; if (amountToCopy > chunkSizeBytes - bufferOffset) { amountToCopy = chunkSizeBytes - bufferOffset; } System.arraycopy(b, currentOffset, buffer, bufferOffset, amountToCopy); bufferOffset += amountToCopy; currentOffset += amountToCopy; lengthToWrite -= amountToCopy; lengthInBytes += amountToCopy; if (bufferOffset == chunkSizeBytes) { writeChunk(); } } }
@Override public void write(final int b) { byte[] byteArray = new byte[1]; byteArray[0] = (byte) (0xFF & b); write(byteArray, 0, 1); }
private GridFSUploadStream createGridFSUploadStream(@Nullable final ClientSession clientSession, final BsonValue id, final String filename, final GridFSUploadOptions options) { notNull("options", options); Integer chunkSizeBytes = options.getChunkSizeBytes(); int chunkSize = chunkSizeBytes == null ? this.chunkSizeBytes : chunkSizeBytes; checkCreateIndex(clientSession); return new GridFSUploadStreamImpl(clientSession, filesCollection, chunksCollection, id, filename, chunkSize, disableMD5, options.getMetadata()); }
@Override @SuppressWarnings("deprecation") public ObjectId getFileId() { return getObjectId(); }
@Override public void abort() { synchronized (closeLock) { checkClosed(); closed = true; } if (clientSession != null) { chunksCollection.deleteMany(clientSession, new Document("files_id", fileId)); } else { chunksCollection.deleteMany(new Document("files_id", fileId)); } }