-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5dce0f5
commit a779d46
Showing
5 changed files
with
168 additions
and
16 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
src/main/java/com/databasepreservation/common/api/common/ConsumesSkipableOutputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* The contents of this file are subject to the license and copyright | ||
* detailed in the LICENSE file at the root of the source | ||
* tree and available online at | ||
* | ||
* https://github.com/keeps/roda | ||
*/ | ||
package com.databasepreservation.common.api.common; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.nio.file.Path; | ||
|
||
import org.roda.core.data.exceptions.AuthorizationDeniedException; | ||
import org.roda.core.data.exceptions.GenericException; | ||
import org.roda.core.data.exceptions.NotFoundException; | ||
import org.roda.core.data.exceptions.RequestNotValidException; | ||
|
||
public interface ConsumesSkipableOutputStream extends ConsumesOutputStream { | ||
|
||
void consumeOutputStream(OutputStream out, long from, long end); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/main/java/com/databasepreservation/common/server/storage/BinaryConsumesOutputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* The contents of this file are subject to the license and copyright | ||
* detailed in the LICENSE file at the root of the source | ||
* tree and available online at | ||
* | ||
* https://github.com/keeps/roda | ||
*/ | ||
package com.databasepreservation.common.server.storage; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.io.RandomAccessFile; | ||
import java.nio.file.Path; | ||
import java.util.Date; | ||
|
||
import com.databasepreservation.common.api.common.ConsumesSkipableOutputStream; | ||
|
||
public class BinaryConsumesOutputStream implements ConsumesSkipableOutputStream { | ||
private final Path path; | ||
private final long size; | ||
private final String filename; | ||
private final String mediaType; | ||
|
||
public BinaryConsumesOutputStream(Path path, long size, String filename, String mediaType) { | ||
this.path = path; | ||
this.size = size; | ||
this.filename = filename; | ||
this.mediaType = mediaType; | ||
} | ||
|
||
@Override | ||
public void consumeOutputStream(OutputStream output) throws IOException { | ||
// TODO document why this method is empty | ||
} | ||
|
||
@Override | ||
public Date getLastModified() { | ||
return null; | ||
} | ||
|
||
|
||
@Override | ||
public long getSize() { | ||
return this.size; | ||
} | ||
|
||
@Override | ||
public void consumeOutputStream(OutputStream out, long from, long end) { | ||
try { | ||
File file = path.toFile(); | ||
byte[] buffer = new byte[1024]; | ||
try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r")) { | ||
long pos = from; | ||
randomAccessFile.seek(pos); | ||
while (pos < end) { | ||
randomAccessFile.read(buffer); | ||
out.write(buffer); | ||
pos += buffer.length; | ||
} | ||
out.flush(); | ||
} | ||
} catch (IOException e) { | ||
// ignore | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public String getFileName() { | ||
return this.filename; | ||
} | ||
|
||
@Override | ||
public String getMediaType() { | ||
return this.mediaType; | ||
} | ||
} |