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.

LittleEndianBig5Stream.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.util;
  16. import java.io.ByteArrayInputStream;
  17. /**
  18. * Stream that converts MSOffice's way of storing Big5, with
  19. * zero-byte padding for ASCII and in LittleEndianOrder.
  20. */
  21. @Internal
  22. public class LittleEndianBig5Stream extends ByteArrayInputStream {
  23. private static final int EOF = -1;
  24. private static final int INVALID_PAIR = -2;
  25. private static final int EMPTY_TRAILING = -3;
  26. //the char that is logically trailing in Big5 encoding
  27. //however in LittleEndian order, this is the first encountered.
  28. int trailing = EMPTY_TRAILING;
  29. public LittleEndianBig5Stream(byte[] buf) {
  30. super(buf);
  31. }
  32. public LittleEndianBig5Stream(byte[] buf, int offset, int length) {
  33. super(buf, offset, length);
  34. }
  35. @Override
  36. public int read() {
  37. if (trailing != EMPTY_TRAILING) {
  38. int tmp = trailing;
  39. trailing = EMPTY_TRAILING;
  40. return tmp;
  41. }
  42. int leading = readNext();
  43. while (leading == INVALID_PAIR) {
  44. leading = readNext();
  45. }
  46. if (leading == EOF) {
  47. return EOF;
  48. }
  49. return leading;
  50. }
  51. //returns leading, sets trailing appropriately
  52. //returns -1 if it hits the end of the stream
  53. //returns -2 for an invalid big5 code pair
  54. private final int readNext() {
  55. trailing = super.read();
  56. if (trailing == -1) {
  57. return EOF;
  58. }
  59. int leading = super.read();
  60. if (leading == EOF) {
  61. return EOF;
  62. }
  63. int lead = leading&0xff;
  64. if (lead > 0x80) {
  65. return leading;
  66. } else if (lead == 0) {
  67. int ret = trailing;
  68. trailing = EMPTY_TRAILING;
  69. return ret;
  70. } else {
  71. int ret = trailing;
  72. trailing = EMPTY_TRAILING;
  73. return ret;
  74. //return INVALID_PAIR;
  75. }
  76. }
  77. @Override
  78. public int read(byte[] buff, int off, int len) {
  79. int bytesRead = 0;
  80. for (int i = off; i < off+len; i++) {
  81. int b = read();
  82. if (b == -1) {
  83. if (bytesRead == 0) {
  84. return -1;
  85. } else {
  86. return bytesRead;
  87. }
  88. }
  89. bytesRead++;
  90. buff[i] = (byte)b;
  91. }
  92. return bytesRead;
  93. }
  94. }