pixelmed
Utilities.java
Go to the documentation of this file.
1 /* Copyright (c) 2014, David A. Clunie DBA Pixelmed Publishing. All rights reserved. */
2 
3 package com.pixelmed.codec.jpeg;
4 
5 import java.io.InputStream;
6 import java.io.IOException;
7 
13 public class Utilities {
14 
15  private static final String identString = "@(#) $Header: /userland/cvs/codec/com/pixelmed/codec/jpeg/Utilities.java,v 1.1 2014/03/21 15:28:07 dclunie Exp $";
16 
17  // modified from com.pixelmed.utils.HexDump.toPaddedHexString()
18  public static String toPaddedHexString(int i,int length) {
19  StringBuffer sb = new StringBuffer();
20  sb.append("0x");
21  String s=Integer.toHexString(i);
22  int ls=s.length();
23  while(ls++ < length) {
24  sb.append("0");
25  }
26  sb.append(s); // even if it is longer than length wanted
27  return sb.toString();
28  }
29 
30  public static String toPaddedHexString(long i,int length) {
31  StringBuffer sb = new StringBuffer();
32  sb.append("0x");
33  String s=Long.toHexString(i);
34  int ls=s.length();
35  while(ls++ < length) {
36  sb.append("0");
37  }
38  sb.append(s); // even if it is longer than length wanted
39  return sb.toString();
40  }
41 
42  public static final int extract8(byte[] b,int offset) {
43  return b[offset]&0xff;
44  }
45 
46  public static final int extract16be(byte[] b,int offset) {
47  return ((b[offset]&0xff)<<8) + (b[offset+1]&0xff);
48  }
49 
50  public static final long extract24be(byte[] b,int offset) {
51  return ((b[offset]&0xff)<<16) + ((b[offset+1]&0xff)<<8) + (b[offset+2]&0xff);
52  }
53 
54  public static final long extract32be(byte[] b,int offset) {
55  return ((b[offset]&0xff)<<24) + ((b[offset+1]&0xff)<<16) + ((b[offset+2]&0xff)<<8) + (b[offset+3]&0xff);
56  }
57 
58  public static final int read16be(InputStream in) throws IOException {
59  // big-endian
60  int u;
61  byte b[] = new byte[2];
62  int count = in.read(b,0,2);
63  if (count == 2) {
64  u = extract16be(b,0);
65  }
66  else {
67  u = -1; // OK as EOF/failure value since int is 32 bits and valid can only be 16
68  }
69  return u;
70  }
71 
72 }
73 
com.pixelmed.codec.jpeg.Utilities.extract16be
static final int extract16be(byte[] b, int offset)
Definition: Utilities.java:46
com.pixelmed.codec.jpeg.Utilities.toPaddedHexString
static String toPaddedHexString(int i, int length)
Definition: Utilities.java:18
com.pixelmed.codec.jpeg.Utilities.extract8
static final int extract8(byte[] b, int offset)
Definition: Utilities.java:42
com.pixelmed.codec.jpeg.Utilities
Definition: Utilities.java:13
com.pixelmed.codec.jpeg.Utilities.toPaddedHexString
static String toPaddedHexString(long i, int length)
Definition: Utilities.java:30
com.pixelmed.codec.jpeg.Utilities.extract24be
static final long extract24be(byte[] b, int offset)
Definition: Utilities.java:50
com.pixelmed.codec.jpeg.Utilities.extract32be
static final long extract32be(byte[] b, int offset)
Definition: Utilities.java:54
com.pixelmed.codec.jpeg.Utilities.read16be
static final int read16be(InputStream in)
Definition: Utilities.java:58