Source for org.jfree.fonts.ByteAccessUtilities

   1: /**
   2:  * ===========================================
   3:  * LibFonts : a free Java font reading library
   4:  * ===========================================
   5:  *
   6:  * Project Info:  http://reporting.pentaho.org/libfonts/
   7:  *
   8:  * (C) Copyright 2006-2007, by Pentaho Corporation and Contributors.
   9:  *
  10:  * This library is free software; you can redistribute it and/or modify it under the terms
  11:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  12:  * either version 2.1 of the License, or (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  15:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16:  * See the GNU Lesser General Public License for more details.
  17:  *
  18:  * You should have received a copy of the GNU Lesser General Public License along with this
  19:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20:  * Boston, MA 02111-1307, USA.
  21:  *
  22:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  23:  * in the United States and other countries.]
  24:  *
  25:  * ------------
  26:  * $Id: ByteAccessUtilities.java 3523 2007-10-16 11:03:09Z tmorgner $
  27:  * ------------
  28:  * (C) Copyright 2006-2007, by Pentaho Corporation.
  29:  */
  30: package org.jfree.fonts;
  31: 
  32: import org.jfree.fonts.encoding.ByteBuffer;
  33: import org.jfree.fonts.encoding.CodePointBuffer;
  34: import org.jfree.fonts.encoding.Encoding;
  35: import org.jfree.fonts.encoding.EncodingException;
  36: import org.jfree.fonts.encoding.EncodingRegistry;
  37: import org.jfree.fonts.encoding.manual.Utf16LE;
  38: 
  39: /**
  40:  * Reads byte-data using a Big-Endian access schema. Big-Endian is used for TrueType fonts.
  41:  *
  42:  * @author Thomas Morgner
  43:  */
  44: public class ByteAccessUtilities
  45: {
  46:   private ByteAccessUtilities()
  47:   {
  48:   }
  49: 
  50:   public static int readUShort (final byte[] data, final int pos)
  51:   {
  52:     return ((data[pos] & 0xff) << 8) | (data[pos + 1] & 0xff);
  53:   }
  54: 
  55:   public static long readULong (final byte[] data, final int pos)
  56:   {
  57:     final int c1 = (data[pos] & 0xff);
  58:     final int c2 = (data[pos + 1] & 0xff);
  59:     final int c3 = (data[pos + 2] & 0xff);
  60:     final int c4 = (data[pos + 3] & 0xff);
  61: 
  62:     long retval = ((long) c1 << 24);
  63:     retval |= (long)c2 << 16;
  64:     retval |= (long)c3 << 8;
  65:     retval |= (long)c4;
  66:     return retval;
  67:   }
  68: 
  69:   public static float readFixed (final byte[] data, final int pos)
  70:   {
  71:     final short mantissa = readShort(data, pos);
  72:     final int fraction = readUShort(data, pos + 2);
  73:     if (fraction == 0 || mantissa == 0)
  74:     {
  75:       return 0;
  76:     }
  77:     return (float) mantissa / (fraction / 16384.0f);
  78:   }
  79: 
  80:   public static long readLongDateTime (final byte[] data, final int pos)
  81:   {
  82:     final int c1 = (data[pos] & 0xff);
  83:     final int c2 = (data[pos + 1] & 0xff);
  84:     final int c3 = (data[pos + 2] & 0xff);
  85:     final int c4 = (data[pos + 3] & 0xff);
  86:     final int c5 = (data[pos + 4] & 0xff);
  87:     final int c6 = (data[pos + 5] & 0xff);
  88:     final int c7 = (data[pos + 6] & 0xff);
  89:     final int c8 = (data[pos + 7] & 0xff);
  90: 
  91:     long retval = ((long) c1 << 56);
  92:     retval |= (long)c2 << 48;
  93:     retval |= (long)c3 << 40;
  94:     retval |= (long)c4 << 32;
  95:     retval |= (long)c5 << 24;
  96:     retval |= (long)c6 << 16;
  97:     retval |= (long)c7 << 8;
  98:     retval |= (long)c8;
  99:     return retval;
 100:   }
 101: 
 102:   public static byte[] readBytes (final byte[] data,
 103:                                   final int pos, final int length)
 104:   {
 105:     final byte[] retval = new byte[length];
 106:     System.arraycopy(data, pos, retval, 0, length);
 107:     return retval;
 108:   }
 109: 
 110:   public static short readShort (final byte[] data, final int pos)
 111:   {
 112:     return (short) ((data[pos] & 0xff) << 8 | (data[pos + 1] & 0xff));
 113:   }
 114: 
 115:   public static int readLong (final byte[] data, final int pos)
 116:   {
 117:     int retval = 0;
 118:     retval |= (long)(data[pos] & 0xff) << 24;
 119:     retval |= (long)(data[pos + 1] & 0xff) << 16;
 120:     retval |= (long)(data[pos + 2] & 0xff) << 8;
 121:     retval |= (long)(data[pos + 3] & 0xff);
 122:     return retval;
 123:   }
 124: 
 125:   public static int readZStringOffset (final byte[] data, final int pos, final int maxLength)
 126:   {
 127:     final int lastPos = Math.min (pos + maxLength, pos + data.length);
 128:     for (int i = pos; i < lastPos; i++)
 129:     {
 130:       if (data[i] == 0)
 131:       {
 132:         return i;
 133:       }
 134:     }
 135: 
 136:     return lastPos;
 137:   }
 138: 
 139:   public static String readZString (final byte[] data, final int pos, final int maxLength, final String encoding)
 140:       throws EncodingException
 141:   {
 142:     final int lastPos = Math.min (pos + maxLength, pos + data.length);
 143:     for (int i = pos; i < lastPos; i++)
 144:     {
 145:       if (data[i] == 0)
 146:       {
 147:         return readString(data, pos, i - pos, encoding);
 148:       }
 149:     }
 150: 
 151:     return readString(data, pos, lastPos, encoding);
 152:   }
 153: 
 154:   public static String readString (final byte[] data, final int pos,
 155:                                    final int length, final String encoding)
 156:           throws EncodingException
 157:   {
 158:     final Encoding enc;
 159:     if ("UTF-16".equals(encoding))
 160:     {
 161:       enc = EncodingRegistry.getInstance().getEncoding("UTF-16LE");
 162:     }
 163:     else
 164:     {
 165:       enc = EncodingRegistry.getInstance().getEncoding(encoding);
 166:     }
 167:     final ByteBuffer byteBuffer = new ByteBuffer(data, pos, length);
 168:     final CodePointBuffer cp = enc.decode(byteBuffer, null);
 169:     return Utf16LE.getInstance().encodeString(cp);
 170:   }
 171: }