-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update encoding and decoding function help info
- Loading branch information
Showing
17 changed files
with
590 additions
and
183 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,38 +1,49 @@ | ||
function output = base64decode(input) | ||
%BASE64DECODE Decode Base64 string to a byte array. | ||
function output = base64decode(varargin) | ||
% | ||
% output = base64decode(input) | ||
% output = base64decode(input) | ||
% | ||
% The function takes a Base64 string INPUT and returns a uint8 array | ||
% OUTPUT. JAVA must be running to use this function. The result is always | ||
% given as a 1-by-N array, and doesn't retrieve the original dimensions. | ||
% | ||
% See also base64encode | ||
% Decoding a Base64-encoded byte-stream to recover the original data | ||
% This function depends on JVM in MATLAB or, can optionally use the ZMat | ||
% toolbox (https://github.com/fangq/zmat) | ||
% | ||
% Copyright (c) 2012, Kota Yamaguchi | ||
% URL: https://www.mathworks.com/matlabcentral/fileexchange/39526-byte-encoding-utilities | ||
% License : BSD, see LICENSE_*.txt | ||
% | ||
% Modified by: Qianqian Fang (q.fang <at> neu.edu) | ||
% | ||
% input: | ||
% input: a base64-encoded string | ||
% | ||
% output: | ||
% output: the decoded binary byte-stream as a uint8 vector | ||
% | ||
% examples: | ||
% bytes=base64encode('Test JSONLab'); | ||
% orig=char(base64decode(bytes)) | ||
% | ||
% license: | ||
% BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details | ||
% | ||
% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) | ||
% | ||
|
||
if(nargin==0) | ||
error('you must provide at least 1 input'); | ||
end | ||
if(exist('zmat','file')==2 || exist('zmat','file')==3) | ||
output=zmat(uint8(input),0,'base64'); | ||
output=zmat(varargin{1},0,'base64'); | ||
return; | ||
elseif(isoctavemesh) | ||
error('You must install the ZMat toolbox (https://github.com/fangq/zmat) to use this function in Octave'); | ||
end | ||
if(exist('OCTAVE_VERSION','builtin')) | ||
len=rem(numel(input),8) | ||
if(len) | ||
input=[input(:)', repmat(sprintf('\0'),1,(8-len))]; | ||
end | ||
output = base64_decode(input); | ||
return; | ||
end | ||
error(javachk('jvm')); | ||
if ischar(input), input = uint8(input); end | ||
|
||
output = typecast(org.apache.commons.codec.binary.Base64.decodeBase64(input), 'uint8')'; | ||
error(javachk('jvm')); | ||
|
||
if(ischar(varargin{1})) | ||
varargin{1}=uint8(varargin{1}); | ||
end | ||
|
||
input=typecast(varargin{1}(:)','uint8'); | ||
|
||
output = typecast(org.apache.commons.codec.binary.Base64.decodeBase64(input), 'uint8')'; | ||
|
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 |
---|---|---|
@@ -1,33 +1,57 @@ | ||
function output = base64encode(input) | ||
%BASE64ENCODE Encode a byte array using Base64 codec. | ||
function varargout = base64encode(varargin) | ||
% | ||
% output = base64encode(input) | ||
% output = base64encode(input) | ||
% | ||
% The function takes a char, int8, or uint8 array INPUT and returns Base64 | ||
% encoded string OUTPUT. JAVA must be running to use this function. Note | ||
% that encoding doesn't preserve input dimensions. | ||
% Encoding a binary vector or array using Base64 | ||
% | ||
% See also base64decode | ||
% This function depends on JVM in MATLAB or, can optionally use the ZMat | ||
% toolbox (https://github.com/fangq/zmat) | ||
% | ||
% Copyright (c) 2012, Kota Yamaguchi | ||
% URL: https://www.mathworks.com/matlabcentral/fileexchange/39526-byte-encoding-utilities | ||
% License : BSD, see LICENSE_*.txt | ||
% | ||
% Modified by: Qianqian Fang (q.fang <at> neu.edu) | ||
% | ||
% input: | ||
% input: a base64-encoded string | ||
% | ||
% output: | ||
% output: the decoded binary byte-stream as a uint8 vector | ||
% | ||
% examples: | ||
% bytes=base64encode('Test JSONLab'); | ||
% orig=char(base64decode(bytes)) | ||
% | ||
% license: | ||
% BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details | ||
% | ||
% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) | ||
% | ||
|
||
if(nargin==0) | ||
error('you must provide at least 1 input'); | ||
end | ||
|
||
if(exist('zmat','file')==2 || exist('zmat','file')==3) | ||
output=zmat(uint8(input),1,'base64'); | ||
[varargout{1:nargout}]=zmat(varargin{1}, 1,'base64',varargin{2:end}); | ||
return; | ||
end | ||
if(exist('OCTAVE_VERSION','builtin')) | ||
output = base64_encode(uint8(input)); | ||
|
||
if(ischar(varargin{1})) | ||
varargin{1}=uint8(varargin{1}); | ||
end | ||
|
||
input=typecast(varargin{1}(:)','uint8'); | ||
|
||
if(isoctavemesh) | ||
varargout{1} = base64_encode(uint8(input)); | ||
return; | ||
end | ||
error(javachk('jvm')); | ||
if ischar(input), input = uint8(input); end | ||
|
||
output = char(org.apache.commons.codec.binary.Base64.encodeBase64Chunked(input))'; | ||
output = regexprep(output,'\r',''); | ||
error(javachk('jvm')); | ||
if ischar(input) | ||
input = uint8(input); | ||
end | ||
|
||
varargout{1} = char(org.apache.commons.codec.binary.Base64.encodeBase64Chunked(input))'; | ||
varargout{1} = regexprep(varargout{1} ,'\r',''); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,69 @@ | ||
function output = gzipdecode(input) | ||
%GZIPDECODE Decompress input bytes using GZIP. | ||
function varargout = gzipdecode(varargin) | ||
% | ||
% output = gzipdecode(input) | ||
% output = gzipdecode(input) | ||
% or | ||
% output = gzipdecode(input,info) | ||
% | ||
% The function takes a compressed byte array INPUT and returns inflated | ||
% bytes OUTPUT. The INPUT is a result of GZIPENCODE function. The OUTPUT | ||
% is always an 1-by-N uint8 array. JAVA must be enabled to use the function. | ||
% | ||
% See also gzipencode typecast | ||
% Decompressing a GZIP-compressed byte-stream to recover the original data | ||
% This function depends on JVM in MATLAB or, can optionally use the ZMat | ||
% toolbox (https://github.com/fangq/zmat) | ||
% | ||
% Copyright (c) 2012, Kota Yamaguchi | ||
% URL: https://www.mathworks.com/matlabcentral/fileexchange/39526-byte-encoding-utilities | ||
% License : BSD, see LICENSE_*.txt | ||
% | ||
% Modified by: Qianqian Fang (q.fang <at> neu.edu) | ||
% | ||
% input: | ||
% input: a string, int8/uint8 vector or numerical array to store the GZIP-compressed data | ||
% info (optional): a struct produced by the zmat/lz4hcencode function during | ||
% compression; if not given, the inputs/outputs will be treated as a | ||
% 1-D vector | ||
% | ||
% output: | ||
% output: the decompressed byte stream stored in a uint8 vector; if info is | ||
% given, output will restore the original data's type and dimensions | ||
% | ||
% examples: | ||
% [bytes, info]=gzipencode(eye(10)); | ||
% orig=gzipdecode(bytes,info); | ||
% | ||
% license: | ||
% BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details | ||
% | ||
% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) | ||
% | ||
|
||
if(nargin==0) | ||
error('you must provide at least 1 input'); | ||
end | ||
if(exist('zmat','file')==2 || exist('zmat','file')==3) | ||
output=zmat(uint8(input),0,'gzip'); | ||
if(nargin>1) | ||
[varargout{1:nargout}]=zmat(varargin{1},varargin{2:end}); | ||
else | ||
[varargout{1:nargout}]=zmat(varargin{1},0,'gzip',varargin{2:end}); | ||
end | ||
return; | ||
elseif(isoctavemesh) | ||
error('You must install the ZMat toolbox (https://github.com/fangq/zmat) to use this function in Octave'); | ||
end | ||
error(javachk('jvm')); | ||
if ischar(input) | ||
warning('gzipdecode:inputTypeMismatch', ... | ||
'Input is char, but treated as uint8.'); | ||
input = uint8(input); | ||
end | ||
if ~isa(input, 'int8') && ~isa(input, 'uint8') | ||
error('Input must be either int8 or uint8.'); | ||
|
||
if(ischar(varargin{1})) | ||
varargin{1}=uint8(varargin{1}); | ||
end | ||
|
||
input=typecast(varargin{1}(:)','uint8'); | ||
|
||
gzip = java.util.zip.GZIPInputStream(java.io.ByteArrayInputStream(input)); | ||
buffer = java.io.ByteArrayOutputStream(); | ||
org.apache.commons.io.IOUtils.copy(gzip, buffer); | ||
gzip.close(); | ||
output = typecast(buffer.toByteArray(), 'uint8')'; | ||
|
||
end | ||
if(nargout>0) | ||
varargout{1} = typecast(buffer.toByteArray(), 'uint8')'; | ||
if(nargin>1 && isstruct(varargin{2}) && isfield(varargin{2},'type')) | ||
inputinfo=varargin{2}; | ||
varargout{1}=typecast(varargout{1},inputinfo.type); | ||
varargout{1}=reshape(varargout{1},inputinfo.size); | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,63 @@ | ||
function output = gzipencode(input) | ||
%GZIPENCODE Compress input bytes with GZIP. | ||
function varargout = gzipencode(varargin) | ||
% | ||
% output = gzipencode(input) | ||
% output = gzipencode(input) | ||
% or | ||
% [output, info] = gzipencode(input) | ||
% | ||
% The function takes a char, int8, or uint8 array INPUT and returns | ||
% compressed bytes OUTPUT as a uint8 array. Note that the compression | ||
% doesn't preserve input dimensions. JAVA must be enabled to use the | ||
% function. | ||
% Compress a string or numerical array using the GZIP-compression | ||
% | ||
% See also gzipdecode typecast | ||
% This function depends on JVM in MATLAB or, can optionally use the ZMat | ||
% toolbox (https://github.com/fangq/zmat) | ||
% | ||
% Copyright (c) 2012, Kota Yamaguchi | ||
% URL: https://www.mathworks.com/matlabcentral/fileexchange/39526-byte-encoding-utilities | ||
% License : BSD, see LICENSE_*.txt | ||
% | ||
% Modified by: Qianqian Fang (q.fang <at> neu.edu) | ||
% | ||
% input: | ||
% input: the original data, can be a string, a numerical vector or array | ||
% | ||
% output: | ||
% output: the decompressed byte stream stored in a uint8 vector; if info is | ||
% given, output will restore the original data's type and dimensions | ||
% | ||
% examples: | ||
% [bytes, info]=gzipencode(eye(10)); | ||
% orig=gzipdecode(bytes,info); | ||
% | ||
% license: | ||
% BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details | ||
% | ||
% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) | ||
% | ||
|
||
|
||
if(nargin==0) | ||
error('you must provide at least 1 input'); | ||
end | ||
|
||
if(exist('zmat','file')==2 || exist('zmat','file')==3) | ||
output=zmat(uint8(input),1,'gzip'); | ||
[varargout{1:nargout}]=zmat(varargin{1},1,'gzip'); | ||
return; | ||
elseif(isoctavemesh) | ||
error('You must install the ZMat toolbox (https://github.com/fangq/zmat) to use this function in Octave'); | ||
end | ||
|
||
error(javachk('jvm')); | ||
if ischar(input), input = uint8(input); end | ||
if ~isa(input, 'int8') && ~isa(input, 'uint8') | ||
error('Input must be either char, int8 or uint8.'); | ||
|
||
if(ischar(varargin{1})) | ||
varargin{1}=uint8(varargin{1}); | ||
end | ||
|
||
input=typecast(varargin{1}(:)','uint8'); | ||
|
||
buffer = java.io.ByteArrayOutputStream(); | ||
gzip = java.util.zip.GZIPOutputStream(buffer); | ||
gzip.write(input, 0, numel(input)); | ||
gzip.close(); | ||
output = typecast(buffer.toByteArray(), 'uint8')'; | ||
|
||
end | ||
varargout{1} = typecast(buffer.toByteArray(), 'uint8')'; | ||
|
||
if(nargout>1) | ||
varargout{2}=struct('type',class(varargin{1}),'size',size(varargin{1}),'method','gzip','status',0); | ||
end |
Oops, something went wrong.