Skip to content

Commit

Permalink
TIKA-2909 -- trivial formatting updates and add entry to CHANGES.txt …
Browse files Browse the repository at this point in the history
…file
  • Loading branch information
tballison committed Jul 22, 2019
1 parent c21ba30 commit bc16d2e
Show file tree
Hide file tree
Showing 5 changed files with 682 additions and 707 deletions.
2 changes: 2 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Release 1.22 - ???
* NOTE: Known regression: PDFBOX-4587 -- PDF passwords with codepoints
between 0xF000 and 0XF0000 will cause an exception.

* Add parser for HWP v5 files via SooMyung Lee (soomyung) (TIKA-2909).

* Fix order of closing streams to avoid "Failed to close temporary resource"
exception (TIKA-2908).

Expand Down
Original file line number Diff line number Diff line change
@@ -1,131 +1,134 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.tika.parser.hwp;

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.util.IOUtils;
import org.apache.poi.util.LittleEndian;

public class HwpStreamReader {
private InputStream input;
private byte[] buf;

public HwpStreamReader(InputStream inputStream) {
this.input = inputStream;
buf = new byte[4];
}

/**
* More data to read ?
*
* @return
* @throws IOException
*/
public boolean available() throws IOException {
return input.available() > 0;
}

/**
* unsigned 1 byte
*
* @return
* @throws IOException
*/
public short uint8() throws IOException {
int read = IOUtils.readFully(input, buf, 0, 1);

if (read == -1)
return -1;

return LittleEndian.getUByte(buf);
}

/**
* unsigned 2 byte
*
* @return
* @throws IOException
*/
public int uint16() throws IOException {
int read = IOUtils.readFully(input, buf, 0, 2);

if (read == -1)
return -1;

if (read < 2)
throw new EOFException();

return LittleEndian.getUShort(buf);
}

/**
* unsigned 2 byte array
*
* @param i
* @return
* @throws IOException
*/
public int[] uint16(int i) throws IOException {
if (i <= 0)
throw new IllegalArgumentException();

byte[] buf = new byte[i * 2];
int read = IOUtils.readFully(input, buf, 0, i * 2);

if (read != i * 2)
throw new EOFException();

int[] uints = new int[i];
for (int ii = 0; ii < i; ii++) {
uints[ii] = LittleEndian.getUShort(buf, ii * 2);
}

return uints;
}

/**
* unsigned 4 byte
*
* @return
* @throws IOException
*/
public long uint32() throws IOException {
int read = IOUtils.readFully(input, buf, 0, 4);

if (read == -1)
return -1;

if (read < 4)
throw new EOFException();

return LittleEndian.getUInt(buf);
}

/**
* ensure skip of n byte
*
* @param n
* @throws IOException
*/
public void ensureSkip(long n) throws IOException {
IOUtils.skipFully(input, n);
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.tika.parser.hwp;

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.util.IOUtils;
import org.apache.poi.util.LittleEndian;

public class HwpStreamReader {
private InputStream input;
private byte[] buf;

public HwpStreamReader(InputStream inputStream) {
this.input = inputStream;
buf = new byte[4];
}

/**
* More data to read ?
*
* @return
* @throws IOException
*/
public boolean available() throws IOException {
return input.available() > 0;
}

/**
* unsigned 1 byte
*
* @return
* @throws IOException
*/
public short uint8() throws IOException {
int read = IOUtils.readFully(input, buf, 0, 1);

if (read == -1) {
return -1;
}

return LittleEndian.getUByte(buf);
}

/**
* unsigned 2 byte
*
* @return
* @throws IOException
*/
public int uint16() throws IOException {
int read = IOUtils.readFully(input, buf, 0, 2);

if (read == -1) {
return -1;
}

if (read < 2) {
throw new EOFException();
}
return LittleEndian.getUShort(buf);
}

/**
* unsigned 2 byte array
*
* @param i
* @return
* @throws IOException
*/
public int[] uint16(int i) throws IOException {
if (i <= 0) {
throw new IllegalArgumentException();
}
byte[] buf = new byte[i * 2];
int read = IOUtils.readFully(input, buf, 0, i * 2);

if (read != i * 2) {
throw new EOFException();
}
int[] uints = new int[i];
for (int ii = 0; ii < i; ii++) {
uints[ii] = LittleEndian.getUShort(buf, ii * 2);
}

return uints;
}

/**
* unsigned 4 byte
*
* @return
* @throws IOException
*/
public long uint32() throws IOException {
int read = IOUtils.readFully(input, buf, 0, 4);

if (read == -1) {
return -1;
}

if (read < 4) {
throw new EOFException();
}

return LittleEndian.getUInt(buf);
}

/**
* ensure skip of n byte
*
* @param n
* @throws IOException
*/
public void ensureSkip(long n) throws IOException {
IOUtils.skipFully(input, n);
}
}
Loading

0 comments on commit bc16d2e

Please sign in to comment.