Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
BigDataViewer_Server_Extension
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
BioinformaticDataCompression
BigDataViewer_Server_Extension
Commits
f009a29f
Commit
f009a29f
authored
4 years ago
by
Vojtech Moravec
Browse files
Options
Downloads
Patches
Plain Diff
Add responseWithShortArray utility.
parent
82c6a2c7
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/bdv/server/CellHandler.java
+58
-26
58 additions, 26 deletions
src/main/java/bdv/server/CellHandler.java
with
58 additions
and
26 deletions
src/main/java/bdv/server/CellHandler.java
+
58
−
26
View file @
f009a29f
...
@@ -49,11 +49,11 @@ import java.nio.file.Paths;
...
@@ -49,11 +49,11 @@ import java.nio.file.Paths;
import
java.util.Stack
;
import
java.util.Stack
;
public
class
CellHandler
extends
ContextHandler
{
public
class
CellHandler
extends
ContextHandler
{
private
long
transferedDataSize
=
0
;
private
final
long
transferedDataSize
=
0
;
private
static
final
org
.
eclipse
.
jetty
.
util
.
log
.
Logger
LOG
=
Log
.
getLogger
(
CellHandler
.
class
);
private
static
final
org
.
eclipse
.
jetty
.
util
.
log
.
Logger
LOG
=
Log
.
getLogger
(
CellHandler
.
class
);
private
int
counter
=
0
;
private
final
int
counter
=
0
;
private
final
VolatileGlobalCellCache
cache
;
private
final
VolatileGlobalCellCache
cache
;
private
final
Hdf5VolatileShortArrayLoader
loader
;
private
final
Hdf5VolatileShortArrayLoader
loader
;
...
@@ -102,8 +102,15 @@ public class CellHandler extends ContextHandler {
...
@@ -102,8 +102,15 @@ public class CellHandler extends ContextHandler {
private
final
CompressionOptions
compressionParams
;
private
final
CompressionOptions
compressionParams
;
private
ImageCompressor
compressor
=
null
;
private
ImageCompressor
compressor
=
null
;
private
Stack
<
MemoryOutputStream
>
cachedBuffers
=
null
;
private
Stack
<
MemoryOutputStream
>
cachedBuffers
=
null
;
private
ICacheFile
c
ompressionCacheFile
=
null
;
private
ICacheFile
c
achedCodebook
=
null
;
private
final
int
INITIAL_BUFFER_SIZE
=
2048
;
private
final
int
INITIAL_BUFFER_SIZE
=
2048
;
private
long
accumulation
=
0
;
private
synchronized
long
addToAccumulation
(
final
int
value
)
{
accumulation
+=
value
;
return
accumulation
;
}
public
CellHandler
(
final
String
baseUrl
,
final
String
xmlFilename
,
final
String
datasetName
,
final
String
thumbnailsDirectory
,
public
CellHandler
(
final
String
baseUrl
,
final
String
xmlFilename
,
final
String
datasetName
,
final
String
thumbnailsDirectory
,
final
CompressionOptions
compressionParams
)
throws
SpimDataException
,
IOException
{
final
CompressionOptions
compressionParams
)
throws
SpimDataException
,
IOException
{
...
@@ -137,17 +144,17 @@ public class CellHandler extends ContextHandler {
...
@@ -137,17 +144,17 @@ public class CellHandler extends ContextHandler {
if
(
compressionParams
==
null
)
if
(
compressionParams
==
null
)
return
;
return
;
this
.
compressionParams
.
setInputDataInfo
(
new
FileInputData
(
this
.
baseFilename
));
this
.
compressionParams
.
setInputDataInfo
(
new
FileInputData
(
this
.
baseFilename
));
QuantizationCacheManager
qcm
=
new
QuantizationCacheManager
(
compressionParams
.
getCodebookCacheFolder
());
final
QuantizationCacheManager
qcm
=
new
QuantizationCacheManager
(
compressionParams
.
getCodebookCacheFolder
());
this
.
c
ompressionCacheFile
=
qcm
.
loadCacheFile
(
compressionParams
);
this
.
c
achedCodebook
=
qcm
.
loadCacheFile
(
compressionParams
);
if
(
c
ompressionCacheFile
==
null
)
{
if
(
c
achedCodebook
==
null
)
{
LOG
.
warn
(
"CellHandler: Didn't find cached codebook for "
+
this
.
baseFilename
);
LOG
.
warn
(
"CellHandler: Didn't find cached codebook for "
+
this
.
baseFilename
);
return
;
return
;
}
}
LOG
.
info
(
String
.
format
(
"CellHandler: Loaded cached codebook file. '%s' for %s"
,
c
ompressionCacheFile
,
this
.
baseFilename
));
LOG
.
info
(
String
.
format
(
"CellHandler: Loaded cached codebook file. '%s' for %s"
,
c
achedCodebook
,
this
.
baseFilename
));
final
int
initialCompressionCacheSize
=
10
;
final
int
initialCompressionCacheSize
=
10
;
compressor
=
new
ImageCompressor
(
compressionParams
,
c
ompressionCacheFile
);
compressor
=
new
ImageCompressor
(
compressionParams
,
c
achedCodebook
);
cachedBuffers
=
new
Stack
<>();
cachedBuffers
=
new
Stack
<>();
for
(
int
i
=
0
;
i
<
initialCompressionCacheSize
;
i
++)
{
for
(
int
i
=
0
;
i
<
initialCompressionCacheSize
;
i
++)
{
cachedBuffers
.
push
(
new
MemoryOutputStream
(
INITIAL_BUFFER_SIZE
));
cachedBuffers
.
push
(
new
MemoryOutputStream
(
INITIAL_BUFFER_SIZE
));
...
@@ -163,7 +170,7 @@ public class CellHandler extends ContextHandler {
...
@@ -163,7 +170,7 @@ public class CellHandler extends ContextHandler {
}
}
}
}
private
synchronized
void
returnBufferForReuse
(
MemoryOutputStream
buffer
)
{
private
synchronized
void
returnBufferForReuse
(
final
MemoryOutputStream
buffer
)
{
buffer
.
reset
();
buffer
.
reset
();
cachedBuffers
.
push
(
buffer
);
cachedBuffers
.
push
(
buffer
);
}
}
...
@@ -193,6 +200,20 @@ public class CellHandler extends ContextHandler {
...
@@ -193,6 +200,20 @@ public class CellHandler extends ContextHandler {
return
new
FlatBufferInputData
(
data
,
new
V3i
(
cellDims
[
0
],
cellDims
[
1
],
cellDims
[
2
]),
InputData
.
PixelType
.
Gray16
,
this
.
baseFilename
);
return
new
FlatBufferInputData
(
data
,
new
V3i
(
cellDims
[
0
],
cellDims
[
1
],
cellDims
[
2
]),
InputData
.
PixelType
.
Gray16
,
this
.
baseFilename
);
}
}
private
void
responseWithShortArray
(
final
HttpServletResponse
response
,
final
short
[]
data
)
throws
IOException
{
final
OutputStream
responseStream
=
response
.
getOutputStream
();
final
byte
[]
buf
=
new
byte
[
2
*
data
.
length
];
for
(
int
i
=
0
,
j
=
0
;
i
<
data
.
length
;
i
++)
{
final
short
s
=
data
[
i
];
buf
[
j
++]
=
(
byte
)
((
s
>>
8
)
&
0xff
);
buf
[
j
++]
=
(
byte
)
(
s
&
0xff
);
}
response
.
setContentLength
(
buf
.
length
);
responseStream
.
write
(
buf
);
responseStream
.
close
();
}
@Override
@Override
public
void
doHandle
(
final
String
target
,
public
void
doHandle
(
final
String
target
,
final
Request
baseRequest
,
final
Request
baseRequest
,
...
@@ -225,18 +246,7 @@ public class CellHandler extends ContextHandler {
...
@@ -225,18 +246,7 @@ public class CellHandler extends ContextHandler {
final
short
[]
data
=
getCachedVolatileCellData
(
parts
,
cellDims
);
final
short
[]
data
=
getCachedVolatileCellData
(
parts
,
cellDims
);
final
OutputStream
responseStream
=
response
.
getOutputStream
();
responseWithShortArray
(
response
,
data
);
final
byte
[]
buf
=
new
byte
[
2
*
data
.
length
];
for
(
int
i
=
0
,
j
=
0
;
i
<
data
.
length
;
i
++)
{
final
short
s
=
data
[
i
];
buf
[
j
++]
=
(
byte
)
((
s
>>
8
)
&
0xff
);
buf
[
j
++]
=
(
byte
)
(
s
&
0xff
);
}
response
.
setContentLength
(
buf
.
length
);
responseStream
.
write
(
buf
);
responseStream
.
close
();
response
.
setContentType
(
"application/octet-stream"
);
response
.
setContentType
(
"application/octet-stream"
);
response
.
setStatus
(
HttpServletResponse
.
SC_OK
);
response
.
setStatus
(
HttpServletResponse
.
SC_OK
);
...
@@ -251,14 +261,36 @@ public class CellHandler extends ContextHandler {
...
@@ -251,14 +261,36 @@ public class CellHandler extends ContextHandler {
final
FlatBufferInputData
inputData
=
createInputDataObject
(
data
,
cellDims
);
final
FlatBufferInputData
inputData
=
createInputDataObject
(
data
,
cellDims
);
MemoryOutputStream
cellCompressionStream
=
getCachedCompressionBuffer
();
final
MemoryOutputStream
cellCompressionStream
=
getCachedCompressionBuffer
();
final
int
compressedContentLength
=
compressor
.
streamCompressChunk
(
cellCompressionStream
,
inputData
);
final
int
compressedContentLength
=
compressor
.
streamCompressChunk
(
cellCompressionStream
,
inputData
);
// // DEBUG decompress in place.
// if (true) {
// final byte[] buffer = cellCompressionStream.getBuffer();
// final int bufferLength = cellCompressionStream.getCurrentBufferLength();
// ImageDecompressor decompressor = new ImageDecompressor(cachedCodebook);
// short[] decompressedData = null;
// try (InputStream is = new BufferedInputStream(new ByteArrayInputStream(buffer, 0, bufferLength))) {
// decompressedData = decompressor.decompressStream(is, bufferLength);
// } catch (ImageDecompressionException e) {
// e.printStackTrace();
// }
// assert (decompressedData != null);
// responseWithShortArray(response, decompressedData);
// return;
// }
response
.
setContentLength
(
compressedContentLength
);
response
.
setContentLength
(
compressedContentLength
);
try
(
OutputStream
responseStream
=
response
.
getOutputStream
())
{
try
(
final
OutputStream
responseStream
=
response
.
getOutputStream
())
{
responseStream
.
write
(
cellCompressionStream
.
getBuffer
(),
0
,
cellCompressionStream
.
getCurrentBufferLength
());
responseStream
.
write
(
cellCompressionStream
.
getBuffer
(),
0
,
cellCompressionStream
.
getCurrentBufferLength
());
}
}
final
long
currentlySent
=
addToAccumulation
(
compressedContentLength
);
LOG
.
info
(
String
.
format
(
"Sending %dB instead of %dB. Currently sent %dB"
,
compressedContentLength
,
(
data
.
length
*
2
),
currentlySent
));
assert
(
cellCompressionStream
.
getCurrentBufferLength
()
==
compressedContentLength
)
:
assert
(
cellCompressionStream
.
getCurrentBufferLength
()
==
compressedContentLength
)
:
"compressor.streamCompressChunk() is not equal to cachedCompressionStream.getCurrentBufferLength()"
;
"compressor.streamCompressChunk() is not equal to cachedCompressionStream.getCurrentBufferLength()"
;
...
@@ -285,8 +317,8 @@ public class CellHandler extends ContextHandler {
...
@@ -285,8 +317,8 @@ public class CellHandler extends ContextHandler {
return
;
return
;
}
}
try
(
DataOutputStream
dos
=
new
DataOutputStream
(
response
.
getOutputStream
()))
{
try
(
final
DataOutputStream
dos
=
new
DataOutputStream
(
response
.
getOutputStream
()))
{
c
ompressionCacheFile
.
writeToStream
(
dos
);
c
achedCodebook
.
writeToStream
(
dos
);
}
}
response
.
getOutputStream
().
close
();
response
.
getOutputStream
().
close
();
...
@@ -373,7 +405,7 @@ public class CellHandler extends ContextHandler {
...
@@ -373,7 +405,7 @@ public class CellHandler extends ContextHandler {
final
StringWriter
sw
=
new
StringWriter
();
final
StringWriter
sw
=
new
StringWriter
();
xout
.
output
(
doc
,
sw
);
xout
.
output
(
doc
,
sw
);
return
sw
.
toString
();
return
sw
.
toString
();
}
catch
(
JDOMException
|
IOException
e
)
{
}
catch
(
final
JDOMException
|
IOException
e
)
{
LOG
.
warn
(
"Could not read settings file \""
+
settings
+
"\""
);
LOG
.
warn
(
"Could not read settings file \""
+
settings
+
"\""
);
LOG
.
warn
(
e
.
getMessage
());
LOG
.
warn
(
e
.
getMessage
());
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment