You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PFMInputStream.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.fonts.type1;
  19. import java.io.EOFException;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.io.DataInputStream;
  23. import java.io.InputStreamReader;
  24. /**
  25. * This is a helper class for reading PFM files. It defines functions for
  26. * extracting specific values out of the stream.
  27. */
  28. public class PFMInputStream extends java.io.FilterInputStream {
  29. private final DataInputStream datain;
  30. /**
  31. * Constructs a PFMInputStream based on an InputStream representing the
  32. * PFM file.
  33. *
  34. * @param in The stream from which to read the PFM file
  35. */
  36. public PFMInputStream(InputStream in) {
  37. super(in);
  38. datain = new DataInputStream(in);
  39. }
  40. /**
  41. * Parses a one byte value out of the stream.
  42. *
  43. * @return The value extracted
  44. * @throws IOException In case of an I/O problem
  45. */
  46. public short readByte() throws IOException {
  47. short s = datain.readByte();
  48. // Now, we've got to trick Java into forgetting the sign
  49. int s1 = (((s & 0xF0) >>> 4) << 4) + (s & 0x0F);
  50. return (short)s1;
  51. }
  52. /**
  53. * Parses a two byte value out of the stream.
  54. *
  55. * @return The value extracted
  56. * @throws IOException In case of an I/O problem
  57. */
  58. public int readShort() throws IOException {
  59. int i = datain.readShort();
  60. // Change byte order
  61. int high = (i & 0xFF00) >>> 8;
  62. int low = (i & 0x00FF) << 8;
  63. return low + high;
  64. }
  65. /**
  66. * Parses a four byte value out of the stream.
  67. *
  68. * @return The value extracted
  69. * @throws IOException In case of an I/O problem
  70. */
  71. public long readInt() throws IOException {
  72. int i = datain.readInt();
  73. // Change byte order
  74. int i1 = (i & 0xFF000000) >>> 24;
  75. int i2 = (i & 0x00FF0000) >>> 8;
  76. int i3 = (i & 0x0000FF00) << 8;
  77. int i4 = (i & 0x000000FF) << 24;
  78. return i1 + i2 + i3 + i4;
  79. }
  80. /**
  81. * Parses a zero-terminated string out of the stream.
  82. *
  83. * @return The value extracted
  84. * @throws IOException In case of an I/O problem
  85. */
  86. public String readString() throws IOException {
  87. InputStreamReader reader = new InputStreamReader(in, "ISO-8859-1");
  88. StringBuffer buf = new StringBuffer();
  89. int ch = reader.read();
  90. while (ch > 0) {
  91. buf.append((char)ch);
  92. ch = reader.read();
  93. }
  94. if (ch == -1) {
  95. throw new EOFException("Unexpected end of stream reached");
  96. }
  97. return buf.toString();
  98. }
  99. }