Reads data form byte buffer. Searches for either miCOMPRESSED data or miMATRIX data. Compressed data are inflated and the product is recursively passed back to this same method. Modifies buf position. @param buf - input byte buffer @throws IOExcep
(ByteBuffer buf)
| 756 | * @throws IOException when error occurs while reading the buffer. |
| 757 | */ |
| 758 | void readData(ByteBuffer buf) throws IOException { |
| 759 | //read data |
| 760 | ISMatTag tag = new ISMatTag(buf); |
| 761 | switch (tag.type) { |
| 762 | case MatDataTypes.miCOMPRESSED: |
| 763 | int numOfBytes = tag.size; |
| 764 | //inflate and recur |
| 765 | if (buf.remaining() < numOfBytes) { |
| 766 | throw new MatlabIOException("Compressed buffer length miscalculated!"); |
| 767 | } |
| 768 | //instead of standard Inlater class instance I use an inflater input |
| 769 | //stream... gives a great boost to the performance |
| 770 | InflaterInputStream iis = new InflaterInputStream(new ByteBufferInputStream(buf, numOfBytes)); |
| 771 | |
| 772 | //process data decompression |
| 773 | byte[] result = new byte[1024]; |
| 774 | |
| 775 | HeapBufferDataOutputStream dos = new HeapBufferDataOutputStream(); |
| 776 | int i; |
| 777 | try { |
| 778 | do { |
| 779 | i = iis.read(result, 0, result.length); |
| 780 | int len = Math.max(0, i); |
| 781 | dos.write(result, 0, len); |
| 782 | } while (i > 0); |
| 783 | } catch (EOFException eofe) { |
| 784 | System.out.println("EOFException detected!"); |
| 785 | } catch (IOException e) { |
| 786 | throw new MatlabIOException("Could not decompress data: " + e); |
| 787 | } finally { |
| 788 | iis.close(); |
| 789 | dos.flush(); |
| 790 | } |
| 791 | //create a ByteBuffer from the deflated data |
| 792 | ByteBuffer out = dos.getByteBuffer(); |
| 793 | |
| 794 | //with proper byte ordering |
| 795 | out.order(matFileHeader.getByteOrder()); |
| 796 | |
| 797 | try { |
| 798 | readData(out); |
| 799 | } catch (IOException e) { |
| 800 | throw e; |
| 801 | } finally { |
| 802 | dos.close(); |
| 803 | } |
| 804 | break; |
| 805 | case MatDataTypes.miMATRIX: |
| 806 | //read in the matrix |
| 807 | int pos = buf.position(); |
| 808 | |
| 809 | MLArray element = readMatrix(buf, true); |
| 810 | if (element != null) { |
| 811 | // Sometimes a MAT file will contain more than one unnamed |
| 812 | // element. This ensures that all of them will be accessible |
| 813 | // in the end result. |
| 814 | if (!data.containsKey(element.getName())) { |
| 815 | data.put(element.getName(), element); |
no test coverage detected