Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions toolbox/io/import_label.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
%
% Authors: Francois Tadel, 2012-2022

import sun.misc.BASE64Decoder;


%% ===== GET FILES =====
sAllAtlas = repmat(db_template('Atlas'), 0);
Expand Down
5 changes: 1 addition & 4 deletions toolbox/io/in_gii.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
%
% Authors: Francois Tadel, 2017

import sun.misc.BASE64Decoder;

% Read XML file
sXml = in_xml(GiiFile);

Expand Down Expand Up @@ -56,8 +54,7 @@

case {'Base64Binary', 'GZipBase64Binary'}
% Base64 decoding
decoder = BASE64Decoder();
Values{iArray} = decoder.decodeBuffer(sXml.GIFTI.DataArray(iArray).Data.text);
Values{iArray} = bst_base64('decode', sXml.GIFTI.DataArray(iArray).Data.text);
% Unpack gzipped stream
if strcmpi(sXml.GIFTI.DataArray(iArray).Encoding, 'GZipBase64Binary')
Values{iArray} = dunzip(Values{iArray});
Expand Down
2 changes: 0 additions & 2 deletions toolbox/io/in_tess_gii.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
%
% Authors: Francois Tadel, 2012-2017

import sun.misc.BASE64Decoder;

% Read GII file
[sXml, Values] = in_gii(TessFile);
% Initialize matrices
Expand Down
9 changes: 2 additions & 7 deletions toolbox/io/out_tess_gii.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,14 @@ function out_tess_gii( TessMat, OutputFile, isSPM )
end

% ===== ENCODE DATA =====
import sun.misc.BASE64Encoder;
% Initialize Base64 encoder
encoder = BASE64Encoder();
% Encode vertices: millimeters
Vertices = single(TessMat.Vertices' .* 1000);
Vertices = typecast(Vertices(:), 'uint8');
Vertices = char(encoder.encodeBuffer(Vertices));
Vertices((Vertices == 10) | (Vertices == 13)) = [];
Vertices = bst_base64('encode', Vertices);
% Encode faces: 0-based array
Faces = int32(TessMat.Faces' - 1);
Faces = typecast(Faces(:), 'uint8');
Faces = char(encoder.encodeBuffer(Faces));
Faces((Faces == 10) | (Faces == 13)) = [];
Faces = bst_base64('encode', Faces);

% ===== CREATE XML STRING =====
% Create XML file from a template
Expand Down
84 changes: 84 additions & 0 deletions toolbox/misc/bst_base64.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
function y = bst_base64(action, x)
% BST_BASE64: Encode or decode Base64 data, independently from the Java version.
%
% USAGE: bytes = bst_base64('decode', str) % str : char array (line breaks allowed)
% % bytes: int8 vector
% str = bst_base64('encode', bytes) % bytes: int8/uint8 vector (or Java byte[])
% % str : char array (no line breaks)
%
% DESCRIPTION:
% The classes sun.misc.BASE64Decoder and sun.misc.BASE64Encoder used previously were
% internal JDK classes, removed in Java 9. Recent versions of Matlab ship with a newer
% JDK, in which they are not available anymore ("Unable to find or import
% 'sun.misc.BASE64Decoder'"). This function uses java.util.Base64 instead (Java >= 8),
% with fallbacks for older environments.

% @=============================================================================
% This function is part of the Brainstorm software:
% https://neuroimage.usc.edu/brainstorm
%
% Copyright (c) University of Southern California & McGill University
% This software is distributed under the terms of the GNU General Public License
% as published by the Free Software Foundation. Further details on the GPLv3
% license can be found at http://www.gnu.org/copyleft/gpl.html.
%
% FOR RESEARCH PURPOSES ONLY. THE SOFTWARE IS PROVIDED "AS IS," AND THE
% UNIVERSITY OF SOUTHERN CALIFORNIA AND ITS COLLABORATORS DO NOT MAKE ANY
% WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF
% MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, NOR DO THEY ASSUME ANY
% LIABILITY OR RESPONSIBILITY FOR THE USE OF THIS SOFTWARE.
%
% For more information type "brainstorm license" at command prompt.
% =============================================================================@
%
% Authors: Bhushan Thombre, 2026

switch lower(action)
% ===== DECODE =====
case 'decode'
% Convert to a simple char row, remove all the white spaces and line breaks
x = char(x);
x = x(:)';
x(x <= 32) = [];
if isempty(x)
y = zeros(0, 1, 'int8');
return;
end
try
% Java >= 8
y = java.util.Base64.getMimeDecoder().decode(java.lang.String(x));
catch
try
% Matlab >= R2016b, no Java needed
y = typecast(matlab.net.base64decode(x), 'int8');
catch
% Java <= 8
decoder = sun.misc.BASE64Decoder();
y = decoder.decodeBuffer(x);
end
end
y = y(:);

% ===== ENCODE =====
case 'encode'
x = typecast(x(:), 'int8');
try
% Java >= 8
y = char(java.util.Base64.getEncoder().encodeToString(x));
catch
try
% Matlab >= R2016b, no Java needed
y = char(matlab.net.base64encode(typecast(x, 'uint8')));
catch
% Java <= 8
encoder = sun.misc.BASE64Encoder();
y = char(encoder.encode(x));
end
end
% Remove the line breaks possibly added by the encoder
y = y(:)';
y((y == 10) | (y == 13)) = [];

otherwise
error(['Unsupported action: ' action]);
end
6 changes: 2 additions & 4 deletions toolbox/process/bst_report.m
Original file line number Diff line number Diff line change
Expand Up @@ -1040,8 +1040,6 @@ function Info(sProcess, sInputs, strMsg)
% ===== IMAGES =====
if ~isempty(iImages)
html = [html '<H2>Snapshots</H2>' 10];
% Create Base64 encoder
encoder = sun.misc.BASE64Encoder();
% Loop on all the images
for i = 1:length(iImages)
iMsg = iImages(i);
Expand All @@ -1063,15 +1061,15 @@ function Info(sProcess, sInputs, strMsg)
jByteStream = java.io.ByteArrayOutputStream();
javax.imageio.ImageIO.write(jImage, 'png', jByteStream);
% Encode PNG image in Base64
jStringImage = encoder.encode(jByteStream.toByteArray());
strImage = bst_base64('encode', jByteStream.toByteArray());
% Display image in HTML
html = [html, Comment];
if ~isempty(FileNames)
html = [html, ' -- ' 10, PrintFilesList(FileNames, ProtocolInfo, 1)];
else
html = [html, '<BR>'];
end
html = [html, '<IMG src="data:image/png;base64,' char(jStringImage) '" /><BR><BR>'];
html = [html, '<IMG src="data:image/png;base64,' strImage '" /><BR><BR>'];
end
end

Expand Down