@@ -37,24 +37,84 @@ Array.prototype.clean = function(deleteValue) {
3737 return this ;
3838} ;
3939
40- /* Convert Unsigned Int 8 Array Buffer to String */
41- function u8as ( d ) {
40+ /* Start of JPAK Class Stuff */
41+ var JPAK = function ( ) { } ;
42+
43+ // Auxiliary functions
44+ // Convert Unsigned Int 8 Array Buffer to String
45+ JPAK . Uint8ArrayToString = function ( uintArray ) {
4246 var o = "" ;
43- for ( var i = 0 ; i < d . byteLength ; i ++ )
44- o += String . fromCharCode ( d [ i ] ) ;
47+ for ( var i = 0 ; i < uintArray . byteLength ; i ++ )
48+ o += String . fromCharCode ( uintArray [ i ] ) ;
4549 return o ;
4650}
4751
48- /* Start of JPAK Class Stuff */
49- var JPAK = function ( ) { } ;
52+ var u8as = JPAK . Uint8ArrayToString ; // Provided for compatibility
53+
54+ JPAK . Base64_Encoding = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' ;
55+
56+ // Modified version from https://gist.github.com/jonleighton/958841
57+ JPAK . ArrayBufferToBase64 = function ( arrayBuffer ) {
58+ var base64 = ''
59+
60+ var bytes = new Uint8Array ( arrayBuffer )
61+ var byteLength = bytes . byteLength
62+ var byteRemainder = byteLength % 3
63+ var mainLength = byteLength - byteRemainder
64+
65+ var a , b , c , d
66+ var chunk
67+
68+ // Main loop deals with bytes in chunks of 3
69+ for ( var i = 0 ; i < mainLength ; i = i + 3 ) {
70+ // Combine the three bytes into a single integer
71+ chunk = ( bytes [ i ] << 16 ) | ( bytes [ i + 1 ] << 8 ) | bytes [ i + 2 ]
72+
73+ // Use bitmasks to extract 6-bit segments from the triplet
74+ a = ( chunk & 16515072 ) >> 18 // 16515072 = (2^6 - 1) << 18
75+ b = ( chunk & 258048 ) >> 12 // 258048 = (2^6 - 1) << 12
76+ c = ( chunk & 4032 ) >> 6 // 4032 = (2^6 - 1) << 6
77+ d = chunk & 63 // 63 = 2^6 - 1
78+
79+ // Convert the raw binary segments to the appropriate ASCII encoding
80+ base64 += JPAK . Base64_Encoding [ a ] + JPAK . Base64_Encoding [ b ] + JPAK . Base64_Encoding [ c ] + JPAK . Base64_Encoding [ d ]
81+ }
82+
83+ // Deal with the remaining bytes and padding
84+ if ( byteRemainder == 1 ) {
85+ chunk = bytes [ mainLength ]
86+
87+ a = ( chunk & 252 ) >> 2 // 252 = (2^6 - 1) << 2
88+
89+ // Set the 4 least significant bits to zero
90+ b = ( chunk & 3 ) << 4 // 3 = 2^2 - 1
91+
92+ base64 += JPAK . Base64_Encoding [ a ] + JPAK . Base64_Encoding [ b ] + '=='
93+ } else if ( byteRemainder == 2 ) {
94+ chunk = ( bytes [ mainLength ] << 8 ) | bytes [ mainLength + 1 ]
95+
96+ a = ( chunk & 64512 ) >> 10 // 64512 = (2^6 - 1) << 10
97+ b = ( chunk & 1008 ) >> 4 // 1008 = (2^6 - 1) << 4
98+
99+ // Set the 2 least significant bits to zero
100+ c = ( chunk & 15 ) << 2 // 15 = 2^4 - 1
101+
102+ base64 += JPAK . Base64_Encoding [ a ] + JPAK . Base64_Encoding [ b ] + JPAK . Base64_Encoding [ c ] + '='
103+ }
104+
105+ return base64
106+ } ;
50107
108+
109+ // JPAKLoader
51110JPAK . jpakloader = function ( parameters ) {
52111 if ( parameters !== undefined ) {
53112 this . jpakfile = parameters . file ;
54113 this . loadall = parameters . loadall || false ; // TODO: Implement the fetch-on-need feature
55114 }
56115} ;
57116
117+ // Variables
58118JPAK . jpakloader . prototype . dataloaded = false ; // Set to true, when file is loaded
59119JPAK . jpakloader . prototype . filecache = [ ] ; // The cached files that we loaded
60120
@@ -207,6 +267,23 @@ JPAK.jpakloader.prototype.GetFileURL = function(path, type) {
207267 return cache . url ;
208268} ;
209269
270+ // Returns an arraybuffer with file content. It looks in the cache for already loaded files.
271+ JPAK . jpakloader . prototype . GetFileArrayBuffer = function ( path , type ) {
272+ var file = this . FindFileEntry ( path ) ;
273+ type = type || 'application/octet-binary' ;
274+ var cache = this . CacheLoad ( path ) ;
275+
276+ if ( file != undefined && cache == undefined ) {
277+ // Add it to file cache
278+ var blob = new Blob ( [ new Uint8Array ( this . jpakdata . slice ( file . offset , file . offset + file . size ) ) . buffer ] , { "type" :type } ) ;
279+ var arraybuffer = this . jpakdata . slice ( file . offset , file . offset + file . size ) ;
280+ this . filecache . push ( { "path" :path , "type" :type , "blob" :blob , "url" :URL . createObjectURL ( blob ) , "arraybuffer" : arraybuffer } ) ;
281+ return arraybuffer ;
282+ } else if ( cache != undefined )
283+ return cache . arraybuffer ;
284+
285+ return undefined ;
286+ } ;
210287// Returns an arraybuffer with file content. It looks in the cache for already loaded files.
211288JPAK . jpakloader . prototype . GetFileArrayBuffer = function ( path , type ) {
212289 var file = this . FindFileEntry ( path ) ;
@@ -225,4 +302,26 @@ JPAK.jpakloader.prototype.GetFileArrayBuffer = function(path, type) {
225302 return undefined ;
226303} ;
227304
305+ // Returns an Base64 Encoded File Content. It looks in the cache for already loaded files.
306+ JPAK . jpakloader . prototype . GetBase64File = function ( path , type ) {
307+ var filedata = this . GetFileArrayBuffer ( path , type ) ;
308+ if ( filedata == undefined )
309+ return undefined ;
310+
311+ return JPAK . ArrayBufferToBase64 ( filedata ) ;
312+ } ;
228313
314+ // Returns an HTML Data URI with File Content. It looks in the cache for already loaded files.
315+ // Using HTML Data URI for Images, you can hide the load process from chrome Network Inspector
316+ // I didnt find any place that you can find DataURI File
317+ JPAK . jpakloader . prototype . GetHTMLDataURIFile = function ( path , type , encoding ) {
318+ var b64 = this . GetBase64File ( path , type ) ;
319+ // HTML Data URI Format: data:[<MIME-type>][;charset=<encoding>][;base64],<data>
320+ if ( b64 === undefined )
321+ return undefined
322+
323+ if ( encoding !== undefined )
324+ return "data:" + type + ";charset=" + encoding + ";base64," + b64 ;
325+ else
326+ return "data:" + type + ";base64," + b64 ;
327+ } ;
0 commit comments