Changeset 2526


Ignore:
Timestamp:
04/02/07 09:37:59 (13 years ago)
Author:
curtis
Message:

Eliminate embedded small byte array allocations.

Location:
trunk/loci/formats
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/loci/formats/DataTools.java

    r2438 r2526  
    3636public final class DataTools { 
    3737 
     38  // -- Static fields -- 
     39 
     40  /** 
     41   * Persistent byte array for calling 
     42   * {@link java.io.DataInput#readFully(byte[], int, int)} efficiently. 
     43   */ 
     44  private static ThreadLocal eightBytes = new ThreadLocal() { 
     45    protected synchronized Object initialValue() { 
     46      return new byte[8]; 
     47    } 
     48  }; 
     49 
    3850  // -- Constructor -- 
    3951 
     
    4456  /** Reads 1 signed byte [-128, 127]. */ 
    4557  public static byte readSignedByte(DataInput in) throws IOException { 
    46     byte[] b = new byte[1]; 
    47     in.readFully(b); 
     58    byte[] b = (byte[]) eightBytes.get(); 
     59    in.readFully(b, 0, 1); 
    4860    return b[0]; 
    4961  } 
     
    6274    throws IOException 
    6375  { 
    64     byte[] bytes = new byte[2]; 
    65     in.readFully(bytes); 
    66     return bytesToShort(bytes, little); 
     76    byte[] b = (byte[]) eightBytes.get(); 
     77    in.readFully(b, 0, 2); 
     78    return bytesToShort(b, little); 
    6779  } 
    6880 
     
    8092    throws IOException 
    8193  { 
    82     byte[] bytes = new byte[4]; 
    83     in.readFully(bytes); 
    84     return bytesToInt(bytes, little); 
     94    byte[] b = (byte[]) eightBytes.get(); 
     95    in.readFully(b, 0, 4); 
     96    return bytesToInt(b, little); 
    8597  } 
    8698 
     
    98110    throws IOException 
    99111  { 
    100     byte[] bytes = new byte[8]; 
    101     in.readFully(bytes); 
    102     return bytesToLong(bytes, little); 
     112    byte[] b = (byte[]) eightBytes.get(); 
     113    in.readFully(b, 0, 8); 
     114    return bytesToLong(b, little); 
    103115  } 
    104116 
     
    123135    throws IOException 
    124136  { 
    125     byte[] bytes =  s.getBytes("UTF-8"); 
    126     out.write(bytes); 
     137    byte[] b =  s.getBytes("UTF-8"); 
     138    out.write(b); 
    127139  } 
    128140 
     
    159171  } 
    160172 
    161   /** Get a pair of bytes representing a short value. */ 
    162   public static byte[] shortToBytes(short v, boolean little) { 
    163     byte[] rtn = new byte[2]; 
    164     if (little) { 
    165       rtn[0] = (byte) (v & 0xff); 
    166       rtn[1] = (byte) ((v >>> 8) & 0xff); 
    167     } 
    168     else { 
    169       rtn[0] = (byte) ((v >>> 8) & 0xff); 
    170       rtn[1] = (byte) (v & 0xff); 
    171     } 
    172     return rtn; 
    173   } 
    174  
    175   /** Get four bytes representing an int value. */ 
    176   public static byte[] intToBytes(int v, boolean little) { 
    177     byte[] rtn = new byte[4]; 
    178     if (little) { 
    179       rtn[0] = (byte) (v & 0xff); 
    180       rtn[1] = (byte) ((v >> 8) & 0xff); 
    181       rtn[2] = (byte) ((v >> 16) & 0xff); 
    182       rtn[3] = (byte) ((v >> 24) & 0xff); 
    183     } 
    184     else { 
    185       rtn[0] = (byte) ((v >> 24) & 0xff); 
    186       rtn[1] = (byte) ((v >> 16) & 0xff); 
    187       rtn[2] = (byte) ((v >> 8) & 0xff); 
    188       rtn[3] = (byte) (v & 0xff); 
    189     } 
    190     return rtn; 
    191   } 
    192  
    193   /** Get four bytes representing a float value. */ 
    194   public static byte[] floatToBytes(float v, boolean little) { 
    195     int i = Float.floatToIntBits(v); 
    196     return intToBytes(i, little); 
    197   } 
    198  
    199   /** Get eight bytes representing a double value. */ 
    200   public static byte[] doubleToBytes(double v, boolean little) { 
    201     byte[] rtn = new byte[8]; 
    202     long l = Double.doubleToLongBits(v); 
    203  
    204     if (little) { 
    205       rtn[0] = (byte) (l & 0xff); 
    206       rtn[1] = (byte) ((l >> 8) & 0xff); 
    207       rtn[2] = (byte) ((l >> 16) & 0xff); 
    208       rtn[3] = (byte) ((l >> 24) & 0xff); 
    209       rtn[4] = (byte) ((l >> 32) & 0xff); 
    210       rtn[5] = (byte) ((l >> 40) & 0xff); 
    211       rtn[6] = (byte) ((l >> 48) & 0xff); 
    212       rtn[7] = (byte) ((l >> 56) & 0xff); 
    213     } 
    214     else { 
    215       rtn[0] = (byte) ((l >> 56) & 0xff); 
    216       rtn[1] = (byte) ((l >> 48) & 0xff); 
    217       rtn[2] = (byte) ((l >> 40) & 0xff); 
    218       rtn[3] = (byte) ((l >> 32) & 0xff); 
    219       rtn[4] = (byte) ((l >> 24) & 0xff); 
    220       rtn[5] = (byte) ((l >> 16) & 0xff); 
    221       rtn[6] = (byte) ((l >> 8) & 0xff); 
    222       rtn[7] = (byte) (l & 0xff); 
    223     } 
    224     return rtn; 
    225   } 
    226  
    227173  // -- Word decoding -- 
    228174 
     
    432378  public static long bytesToLong(short[] bytes, boolean little) { 
    433379    return bytesToLong(bytes, 0, 8, little); 
    434   } 
    435  
    436   /** Translates bytes from the given array into a string. */ 
    437   public static String bytesToString(short[] bytes, int off, int len) { 
    438     if (bytes.length - off < len) len = bytes.length - off; 
    439     for (int i=0; i<len; i++) { 
    440       if (bytes[off + i] == 0) { 
    441         len = i; 
    442         break; 
    443       } 
    444     } 
    445     byte[] b = new byte[len]; 
    446     for (int i=0; i<b.length; i++) b[i] = (byte) bytes[off + i]; 
    447     return new String(b); 
    448   } 
    449  
    450   /** Translates bytes from the given array into a string. */ 
    451   public static String bytesToString(short[] bytes, int off) { 
    452     return bytesToString(bytes, off, bytes.length - off); 
    453   } 
    454  
    455   /** Translates bytes from the given array into a string. */ 
    456   public static String bytesToString(short[] bytes) { 
    457     return bytesToString(bytes, 0, bytes.length); 
    458380  } 
    459381 
  • trunk/loci/formats/ImageTools.java

    r2430 r2526  
    789789      for (int i=0; i<b.length; i++) { 
    790790        for (int j=0; j<s[0].length; j++) { 
    791           byte[] v = DataTools.shortToBytes(s[i][j], little); 
    792           b[i][j*2] = v[0]; 
    793           b[i][j*2 + 1] = v[1]; 
     791          short v = s[i][j]; 
     792          if (little) { 
     793            b[i][j*2] = (byte) (v & 0xff); 
     794            b[i][j*2+1] = (byte) ((v >>> 8) & 0xff); 
     795          } 
     796          else { 
     797            b[i][j*2] = (byte) ((v >>> 8) & 0xff); 
     798            b[i][j*2+1] = (byte) (v & 0xff); 
     799          } 
    794800        } 
    795801      } 
     
    801807      for (int i=0; i<b.length; i++) { 
    802808        for (int j=0; j<in[0].length; j++) { 
    803           byte[] v = DataTools.intToBytes(in[i][j], little); 
    804           b[i][j*4] = v[0]; 
    805           b[i][j*4 + 1] = v[1]; 
    806           b[i][j*4 + 2] = v[2]; 
    807           b[i][j*4 + 3] = v[3]; 
     809          int v = in[i][j]; 
     810          if (little) { 
     811            b[i][j*4] = (byte) (v & 0xff); 
     812            b[i][j*4+1] = (byte) ((v >> 8) & 0xff); 
     813            b[i][j*4+2] = (byte) ((v >> 16) & 0xff); 
     814            b[i][j*4+3] = (byte) ((v >> 24) & 0xff); 
     815          } 
     816          else { 
     817            b[i][j*4] = (byte) ((v >> 24) & 0xff); 
     818            b[i][j*4+1] = (byte) ((v >> 16) & 0xff); 
     819            b[i][j*4+2] = (byte) ((v >> 8) & 0xff); 
     820            b[i][j*4+3] = (byte) (v & 0xff); 
     821          } 
    808822        } 
    809823      } 
     
    815829      for (int i=0; i<b.length; i++) { 
    816830        for (int j=0; j<in[0].length; j++) { 
    817           byte[] v = DataTools.floatToBytes(in[i][j], little); 
    818           b[i][j*4] = v[0]; 
    819           b[i][j*4 + 1] = v[1]; 
    820           b[i][j*4 + 2] = v[2]; 
    821           b[i][j*4 + 3] = v[3]; 
     831          int v = Float.floatToIntBits(in[i][j]); 
     832          if (little) { 
     833            b[i][j*4] = (byte) (v & 0xff); 
     834            b[i][j*4+1] = (byte) ((v >> 8) & 0xff); 
     835            b[i][j*4+2] = (byte) ((v >> 16) & 0xff); 
     836            b[i][j*4+3] = (byte) ((v >> 24) & 0xff); 
     837          } 
     838          else { 
     839            b[i][j*4] = (byte) ((v >> 24) & 0xff); 
     840            b[i][j*4+1] = (byte) ((v >> 16) & 0xff); 
     841            b[i][j*4+2] = (byte) ((v >> 8) & 0xff); 
     842            b[i][j*4+3] = (byte) (v & 0xff); 
     843          } 
    822844        } 
    823845      } 
Note: See TracChangeset for help on using the changeset viewer.