XZIOException.
7 | */
8 | public class XZIOException extends java.io.IOException {
9 | private static final long serialVersionUID = 3L;
10 |
11 | public XZIOException() {
12 | super();
13 | }
14 |
15 | public XZIOException(String s) {
16 | super(s);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/org/xbib/io/archive/cpio/CpioSession.java:
--------------------------------------------------------------------------------
1 | package org.xbib.io.archive.cpio;
2 |
3 | import org.xbib.io.BytesProgressWatcher;
4 | import org.xbib.io.archive.ArchiveSession;
5 |
6 | /**
7 | * Cpio Session
8 | */
9 | public class CpioSession extends ArchiveSession
15 | * The finish method of FinishableOutputStream
16 | * does nothing. Subclasses should override it if they need finishing
17 | * support, which is the case, for example, with compressors.
18 | *
19 | * @throws java.io.IOException
20 | */
21 | public void finish() throws IOException {
22 | }
23 |
24 | ;
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/org/xbib/io/compress/xz/PowerPCOptions.java:
--------------------------------------------------------------------------------
1 | package org.xbib.io.compress.xz;
2 |
3 | import org.xbib.io.compress.xz.simple.PowerPC;
4 |
5 | import java.io.InputStream;
6 |
7 | /**
8 | * BCJ filter for big endian PowerPC instructions.
9 | */
10 | public class PowerPCOptions extends BCJOptions {
11 | private static final int ALIGNMENT = 4;
12 |
13 | public PowerPCOptions() {
14 | super(ALIGNMENT);
15 | }
16 |
17 | public FinishableOutputStream getOutputStream(FinishableOutputStream out) {
18 | return new SimpleOutputStream(out, new PowerPC(true, startOffset));
19 | }
20 |
21 | public InputStream getInputStream(InputStream in) {
22 | return new SimpleInputStream(in, new PowerPC(false, startOffset));
23 | }
24 |
25 | FilterEncoder getFilterEncoder() {
26 | return new BCJEncoder(this, BCJCoder.POWERPC_FILTER_ID);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/main/assemblies/plugin.xml:
--------------------------------------------------------------------------------
1 |
2 | {
12 |
13 | /**
14 | * Set URI of this connection
15 | *
16 | * @param uri
17 | */
18 | Connection setURI(URI uri);
19 |
20 | /**
21 | * Get URI of this connection
22 | */
23 | URI getURI();
24 |
25 | /**
26 | * Close connection and close all sessions
27 | */
28 | void close() throws IOException;
29 |
30 | /**
31 | * Create a new session on this connection
32 | *
33 | * @return the session
34 | * @throws IOException if the session can not be created
35 | */
36 | S createSession() throws IOException;
37 |
38 | }
39 |
--------------------------------------------------------------------------------
/src/main/java/org/xbib/io/compress/xz/package-info.java:
--------------------------------------------------------------------------------
1 | /**
2 | * XZ data compression
3 | *
4 | * This aims to be a complete implementation of XZ
5 | * data compression in pure Java. Features:
10 | * Threading is planned but it is unknown when it will be implemented.
11 | *
12 | * Start by reading the documentation of
13 | * {@link org.xbib.io.compress.xz.XZOutputStream} and
14 | * {@link org.xbib.io.compress.xz.XZInputStream}. If you use XZ inside another file
15 | * format or protocol, see also {@link org.xbib.io.compress.xz.SingleXZInputStream}.
16 |
17 | */
18 | package org.xbib.io.compress.xz;
19 |
--------------------------------------------------------------------------------
/src/site/site.xml:
--------------------------------------------------------------------------------
1 |
2 |
{
24 |
25 | Map meta(String key, Object value);
28 |
29 | P payload();
30 |
31 | Packet payload(P packet);
32 | }
33 |
--------------------------------------------------------------------------------
/src/main/java/org/xbib/io/compress/xz/RawCoder.java:
--------------------------------------------------------------------------------
1 | package org.xbib.io.compress.xz;
2 |
3 | class RawCoder {
4 | static void validate(FilterCoder[] filters)
5 | throws UnsupportedOptionsException {
6 | for (int i = 0; i < filters.length - 1; ++i) {
7 | if (!filters[i].nonLastOK()) {
8 | throw new UnsupportedOptionsException(
9 | "Unsupported XZ filter chain");
10 | }
11 | }
12 |
13 | if (!filters[filters.length - 1].lastOK()) {
14 | throw new UnsupportedOptionsException(
15 | "Unsupported XZ filter chain");
16 | }
17 |
18 | int changesSizeCount = 0;
19 | for (int i = 0; i < filters.length; ++i) {
20 | if (filters[i].changesSize()) {
21 | ++changesSizeCount;
22 | }
23 | }
24 |
25 | if (changesSizeCount > 3) {
26 | throw new UnsupportedOptionsException(
27 | "Unsupported XZ filter chain");
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/org/xbib/elasticsearch/knapsack/KnapsackModule.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2014 Jörg Prante
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.xbib.elasticsearch.knapsack;
17 |
18 | import org.elasticsearch.common.inject.Binder;
19 | import org.elasticsearch.common.inject.Module;
20 |
21 | public class KnapsackModule implements Module {
22 |
23 | @Override
24 | public void configure(Binder binder) {
25 | binder.bind(KnapsackService.class).asEagerSingleton();
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/main/java/org/xbib/io/archive/zip/ZipSession.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2014 Jörg Prante
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.xbib.io.archive.zip;
17 |
18 | import org.xbib.io.BytesProgressWatcher;
19 | import org.xbib.io.archive.ArchiveSession;
20 |
21 | public class ZipSession extends ArchiveSession {
22 |
23 | protected ZipSession(BytesProgressWatcher watcher) {
24 | super(watcher);
25 | }
26 |
27 | @Override
28 | public String getName() {
29 | return "zip";
30 | }
31 |
32 | }
--------------------------------------------------------------------------------
/src/main/java/org/xbib/elasticsearch/knapsack/KnapsackRequest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2014 Jörg Prante
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.xbib.elasticsearch.knapsack;
17 |
18 | import org.elasticsearch.common.unit.TimeValue;
19 |
20 | import java.util.Map;
21 |
22 | public interface KnapsackRequest {
23 |
24 | String getCluster();
25 |
26 | String getHost();
27 |
28 | int getPort();
29 |
30 | boolean getSniff();
31 |
32 | TimeValue getTimeout();
33 |
34 | Map getIndexTypeNames();
35 | }
36 |
--------------------------------------------------------------------------------
/src/test/java/org/xbib/suites/CompressionTestSuite.java:
--------------------------------------------------------------------------------
1 | package org.xbib.suites;
2 |
3 | import org.junit.runner.RunWith;
4 | import org.junit.runners.Suite;
5 | import org.xbib.io.compress.bzip2.BZip2BitInputStreamTests;
6 | import org.xbib.io.compress.bzip2.BZip2BitOutputStreamTests;
7 | import org.xbib.io.compress.bzip2.BZip2BlockDecompressorTests;
8 | import org.xbib.io.compress.bzip2.BZip2DivSufSortTests;
9 | import org.xbib.io.compress.bzip2.BZip2HuffmanStageDecoderTests;
10 | import org.xbib.io.compress.bzip2.BZip2OutputStreamTests;
11 | import org.xbib.io.compress.bzip2.SimpleBZip2Tests;
12 | import org.xbib.io.compress.bzip2.HuffmanAllocatorTests;
13 |
14 |
15 | @RunWith(Suite.class)
16 | @Suite.SuiteClasses({
17 | BZip2BitInputStreamTests.class,
18 | BZip2BitOutputStreamTests.class,
19 | BZip2BlockDecompressorTests.class,
20 | BZip2DivSufSortTests.class,
21 | BZip2HuffmanStageDecoderTests.class,
22 | BZip2OutputStreamTests.class,
23 | HuffmanAllocatorTests.class,
24 | SimpleBZip2Tests.class
25 | })
26 | public class CompressionTestSuite {
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/src/main/java/org/xbib/io/compress/bzip2/BZip2CompressCodec.java:
--------------------------------------------------------------------------------
1 | package org.xbib.io.compress.bzip2;
2 |
3 | import org.xbib.io.compress.CompressCodec;
4 |
5 | import java.io.IOException;
6 | import java.io.InputStream;
7 | import java.io.OutputStream;
8 |
9 | public class BZip2CompressCodec implements CompressCodec {
14 |
15 | enum Mode {
16 |
17 | READ, WRITE, READ_WRITE, APPEND, DEFERRED_WRITE, OVERWRITE, URI_ENCODED, NONE;
18 | }
19 |
20 | /**
21 | * Open valve with a given input/output mode
22 | *
23 | * @throws java.io.IOException if valve can not be opened
24 | */
25 | void open(EnumSet
22 | * Omitting the integrity check is strongly discouraged except when
23 | * the integrity of the data will be verified by other means anyway,
24 | * and calculating the check twice would be useless.
25 | */
26 | public static final int CHECK_NONE = 0;
27 |
28 | /**
29 | * Integrity check ID for CRC32.
30 | */
31 | public static final int CHECK_CRC32 = 1;
32 |
33 | /**
34 | * Integrity check ID for CRC64.
35 | */
36 | public static final int CHECK_CRC64 = 4;
37 |
38 | /**
39 | * Integrity check ID for SHA-256.
40 | */
41 | public static final int CHECK_SHA256 = 10;
42 |
43 | private XZ() {
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/main/java/org/xbib/io/compress/xz/CountingOutputStream.java:
--------------------------------------------------------------------------------
1 | package org.xbib.io.compress.xz;
2 |
3 | import java.io.IOException;
4 | import java.io.OutputStream;
5 |
6 | /**
7 | * Counts the number of bytes written to an output stream.
8 | *
9 | * The
7 | * The amount of memory required and the memory usage limit are
8 | * included in the error detail message in human readable format.
9 | */
10 | public class MemoryLimitException extends XZIOException {
11 | private static final long serialVersionUID = 3L;
12 |
13 | private final int memoryNeeded;
14 | private final int memoryLimit;
15 |
16 | /**
17 | * Creates a new MemoryLimitException.
18 | *
19 | * The amount of memory needed and the memory usage limit are
20 | * included in the error detail message.
21 | *
22 | * @param memoryNeeded amount of memory needed as kibibytes (KiB)
23 | * @param memoryLimit specified memory usage limit as kibibytes (KiB)
24 | */
25 | public MemoryLimitException(int memoryNeeded, int memoryLimit) {
26 | super("" + memoryNeeded + " KiB of memory would be needed; limit was "
27 | + memoryLimit + " KiB");
28 |
29 | this.memoryNeeded = memoryNeeded;
30 | this.memoryLimit = memoryLimit;
31 | }
32 |
33 | /**
34 | * Gets how much memory is required to decompress the data.
35 | *
36 | * @return amount of memory needed as kibibytes (KiB)
37 | */
38 | public int getMemoryNeeded() {
39 | return memoryNeeded;
40 | }
41 |
42 | /**
43 | * Gets what the memory usage limit was at the time the exception
44 | * was created.
45 | *
46 | * @return memory usage limit as kibibytes (KiB)
47 | */
48 | public int getMemoryLimit() {
49 | return memoryLimit;
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/main/java/org/xbib/io/archive/esbulk/EsBulkArchiveCodec.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2014 Jörg Prante
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.xbib.io.archive.esbulk;
17 |
18 | import org.xbib.io.BytesProgressWatcher;
19 | import org.xbib.io.archive.ArchiveCodec;
20 |
21 | import java.io.IOException;
22 | import java.io.InputStream;
23 | import java.io.OutputStream;
24 |
25 | public class EsBulkArchiveCodec implements ArchiveCodec
57 | * Simply calls the {@link #read(byte[], int, int)} method.
58 | *
59 | * MUST be overridden if the {@link #read(byte[], int, int)} method
60 | * is not overridden; may be overridden otherwise.
61 | *
62 | * @return the byte read, or -1 if end of input is reached
63 | * @throws java.io.IOException if an I/O error has occurred
64 | */
65 | @Override
66 | public int read() throws IOException {
67 | byte[] b = new byte[1];
68 | int num = read(b, 0, 1);
69 | return num == -1 ? -1 : b[0] & 0xFF;
70 | }
71 |
72 | }
73 |
--------------------------------------------------------------------------------
/src/main/java/org/xbib/io/compress/lzf/ChunkDecoderFactory.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2014 Jörg Prante
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.xbib.io.compress.lzf;
17 |
18 | /**
19 | * Simple helper class used for loading {@link ChunkDecoder} implementations,
20 | * based on criteria such as "fastest available". Yes, it looks butt-ugly,
21 | * but does the job. Nonetheless, if anyone has lipstick for this pig, let me
22 | * know.
23 | */
24 | public class ChunkDecoderFactory {
25 |
26 | private final static ChunkDecoderFactory _instance;
27 |
28 | static {
29 | Class> impl = VanillaChunkDecoder.class;
30 | _instance = new ChunkDecoderFactory(impl);
31 | }
32 |
33 | private final Class extends ChunkDecoder> _implClass;
34 |
35 | @SuppressWarnings("unchecked")
36 | private ChunkDecoderFactory(Class> imp) {
37 | _implClass = (Class extends ChunkDecoder>) imp;
38 | }
39 |
40 | /**
41 | * Method to use for getting decompressor instance that uses the most
42 | * optimal available methods for underlying data access. It should be safe
43 | * to call this method as implementations are dynamically loaded; however,
44 | * on some non-standard platforms it may be necessary to either directly
45 | * load instances, or use {@link #safeInstance()}.
46 | */
47 | public static ChunkDecoder optimalInstance() {
48 | try {
49 | return _instance._implClass.newInstance();
50 | } catch (Exception e) {
51 | throw new IllegalStateException("Failed to load a ChunkDecoder instance (" + e.getClass().getName() + "): "
52 | + e.getMessage(), e);
53 | }
54 | }
55 |
56 | /**
57 | * Method that can be used to ensure that a "safe" decompressor instance is
58 | * loaded. Safe here means that it should work on any and all Java
59 | * platforms.
60 | */
61 | public static ChunkDecoder safeInstance() {
62 | // this will always succeed loading; no need to use dynamic class loading or instantiation
63 | return new VanillaChunkDecoder();
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/src/main/java/org/xbib/io/compress/xz/SeekableInputStream.java:
--------------------------------------------------------------------------------
1 | package org.xbib.io.compress.xz;
2 |
3 | import java.io.IOException;
4 | import java.io.InputStream;
5 |
6 | /**
7 | * Input stream with random access support.
8 | */
9 | public abstract class SeekableInputStream extends InputStream {
10 | /**
11 | * Seeks
13 | * This will not seek past the end of the file. If the current position
14 | * is already at or past the end of the file, this doesn't seek at all
15 | * and returns
20 | * If
63 | * Seeking past the end of the file should be supported by the subclasses
64 | * unless there is a good reason to do otherwise. If one has seeked
65 | * past the end of the stream, This implementation is not suitable for encodings other than
25 | * utf-8, because java.io encodes unmappable character as question
26 | * marks leading to unreadable ZIP entries on some operating
27 | * systems. Furthermore this implementation is unable to tell whether a
30 | * given name can be safely encoded or not. This implementation acts as a last resort implementation, when
33 | * neither {@link Simple8BitArchiveEntryEncoding} nor {@link NioArchiveEntryEncoding} is
34 | * available.
9 | * Currently only simple byte-wise delta is supported. The only option
10 | * is the delta distance, which you should set to match your data.
11 | * It's not possible to provide a generic default value for it.
12 | *
13 | * For example, with distance = 2 and eight-byte input
14 | * A1 B1 A2 B3 A3 B5 A4 B7, the output will be A1 B1 01 02 01 02 01 02.
15 | *
16 | * The Delta filter can be good with uncompressed bitmap images. It can
17 | * also help with PCM audio, although special-purpose compressors like
18 | * FLAC will give much smaller result at much better compression speed.
19 | */
20 | public class DeltaOptions extends FilterOptions {
21 | /**
22 | * Smallest supported delta calculation distance.
23 | */
24 | public static final int DISTANCE_MIN = 1;
25 |
26 | /**
27 | * Largest supported delta calculation distance.
28 | */
29 | public static final int DISTANCE_MAX = 256;
30 |
31 | private int distance = DISTANCE_MIN;
32 |
33 | /**
34 | * Creates new Delta options and sets the delta distance to 1 byte.
35 | */
36 | public DeltaOptions() {
37 | }
38 |
39 | /**
40 | * Creates new Delta options and sets the distance to the given value.
41 | */
42 | public DeltaOptions(int distance) throws UnsupportedOptionsException {
43 | setDistance(distance);
44 | }
45 |
46 | /**
47 | * Sets the delta distance in bytes. The new distance must be in
48 | * the range [DISTANCE_MIN, DISTANCE_MAX].
49 | */
50 | public void setDistance(int distance) throws UnsupportedOptionsException {
51 | if (distance < DISTANCE_MIN || distance > DISTANCE_MAX) {
52 | throw new UnsupportedOptionsException(
53 | "Delta distance must be in the range [" + DISTANCE_MIN
54 | + ", " + DISTANCE_MAX + "]: " + distance);
55 | }
56 |
57 | this.distance = distance;
58 | }
59 |
60 | /**
61 | * Gets the delta distance.
62 | */
63 | public int getDistance() {
64 | return distance;
65 | }
66 |
67 | public int getEncoderMemoryUsage() {
68 | return DeltaOutputStream.getMemoryUsage();
69 | }
70 |
71 | public FinishableOutputStream getOutputStream(FinishableOutputStream out) {
72 | return new DeltaOutputStream(out, this);
73 | }
74 |
75 | public int getDecoderMemoryUsage() {
76 | return 1;
77 | }
78 |
79 | public InputStream getInputStream(InputStream in) {
80 | return new DeltaInputStream(in, distance);
81 | }
82 |
83 | FilterEncoder getFilterEncoder() {
84 | return new DeltaEncoder(this);
85 | }
86 |
87 | public Object clone() {
88 | try {
89 | return super.clone();
90 | } catch (CloneNotSupportedException e) {
91 | assert false;
92 | throw new RuntimeException();
93 | }
94 | }
95 | }
96 |
--------------------------------------------------------------------------------
/src/main/java/org/xbib/elasticsearch/action/knapsack/state/TransportKnapsackStateAction.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2014 Jörg Prante
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.xbib.elasticsearch.action.knapsack.state;
17 |
18 | import org.elasticsearch.action.ActionListener;
19 | import org.elasticsearch.action.support.ActionFilters;
20 | import org.elasticsearch.action.support.TransportAction;
21 | import org.elasticsearch.client.Client;
22 | import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
23 | import org.elasticsearch.common.inject.Inject;
24 | import org.elasticsearch.common.logging.ESLogger;
25 | import org.elasticsearch.common.logging.ESLoggerFactory;
26 | import org.elasticsearch.common.settings.Settings;
27 | import org.elasticsearch.threadpool.ThreadPool;
28 | import org.elasticsearch.transport.TransportService;
29 | import org.xbib.elasticsearch.knapsack.KnapsackService;
30 | import org.xbib.elasticsearch.knapsack.KnapsackState;
31 |
32 | public class TransportKnapsackStateAction extends TransportAction There are mostly two implementations, one that uses java.nio
26 | * {@link java.nio.charset.Charset Charset} and one implementation,
27 | * which copes with simple 8 bit charsets, because java-1.4 did not
28 | * support Cp437 in java.nio. The main reason for defining an own encoding layer comes from
31 | * the problems with {@link String#getBytes(String)
32 | * String.getBytes}, which encodes unknown characters as ASCII
33 | * quotation marks ('?'). Quotation marks are per definition an
34 | * invalid filename on some operating systems like Windows, which
35 | * leads to ignored ZIP entries. All implementations should implement this interface in a
38 | * reentrant way. Examples for CP 437 (in pseudo-notation, right hand side is
55 | * C-style notation):finish method does nothing.
10 | * This is FinishableOutputStream instead
11 | * of OutputStream solely because it allows
12 | * using this as the output stream for a chain of raw filters.
13 | */
14 | class CountingOutputStream extends FinishableOutputStream {
15 | private final OutputStream out;
16 | private long size = 0;
17 |
18 | public CountingOutputStream(OutputStream out) {
19 | this.out = out;
20 | }
21 |
22 | public void write(int b) throws IOException {
23 | out.write(b);
24 | if (size >= 0) {
25 | ++size;
26 | }
27 | }
28 |
29 | public void write(byte[] b, int off, int len) throws IOException {
30 | out.write(b, off, len);
31 | if (size >= 0) {
32 | size += len;
33 | }
34 | }
35 |
36 | public void flush() throws IOException {
37 | out.flush();
38 | }
39 |
40 | public void close() throws IOException {
41 | out.close();
42 | }
43 |
44 | public long getSize() {
45 | return size;
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/src/main/java/org/xbib/elasticsearch/action/knapsack/abort/KnapsackAbortRequestBuilder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2014 Jörg Prante
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.xbib.elasticsearch.action.knapsack.abort;
17 |
18 | import org.elasticsearch.action.ActionRequestBuilder;
19 | import org.elasticsearch.client.ElasticsearchClient;
20 |
21 | public class KnapsackAbortRequestBuilder extends ActionRequestBuilder0.
15 | */
16 | public void setStartOffset(int startOffset)
17 | throws UnsupportedOptionsException {
18 | if ((startOffset & (alignment - 1)) != 0) {
19 | throw new UnsupportedOptionsException(
20 | "Start offset must be a multiple of " + alignment);
21 | }
22 |
23 | this.startOffset = startOffset;
24 | }
25 |
26 | /**
27 | * Gets the start offset.
28 | */
29 | public int getStartOffset() {
30 | return startOffset;
31 | }
32 |
33 | public int getEncoderMemoryUsage() {
34 | return SimpleOutputStream.getMemoryUsage();
35 | }
36 |
37 | public int getDecoderMemoryUsage() {
38 | return SimpleInputStream.getMemoryUsage();
39 | }
40 |
41 | public Object clone() {
42 | try {
43 | return super.clone();
44 | } catch (CloneNotSupportedException e) {
45 | assert false;
46 | throw new RuntimeException();
47 | }
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/CREDITS.txt:
--------------------------------------------------------------------------------
1 |
2 | Knapsack contains derived work of Apache Common Compress http://commons.apache.org/proper/commons-compress/
3 |
4 | The code in this component has many origins:
5 | The bzip2, tar and zip support came from Avalon's Excalibur, but originally
6 | from Ant, as far as life in Apache goes. The tar package is originally Tim Endres'
7 | public domain package. The bzip2 package is based on the work done by Keiron Liddle as well
8 | as Julian Seward's libbzip2. It has migrated via:
9 | Ant -> Avalon-Excalibur -> Commons-IO -> Commons-Compress.
10 |
11 | The cpio package has been contributed by Michael Kuss and the jRPM project.
12 |
13 | Thanks to `nicktgr15 finish() method will do nothing.
21 | */
22 | public FinishableWrapperOutputStream(OutputStream out) {
23 | this.out = out;
24 | }
25 |
26 | /**
27 | * Calls {@link java.io.OutputStream#write(int) out.write(b)}.
28 | */
29 | public void write(int b) throws IOException {
30 | out.write(b);
31 | }
32 |
33 | /**
34 | * Calls {@link java.io.OutputStream#write(byte[]) out.write(buf)}.
35 | */
36 | public void write(byte[] buf) throws IOException {
37 | out.write(buf);
38 | }
39 |
40 | /**
41 | * Calls {@link java.io.OutputStream#write(byte[], int, int)
42 | * out.write(buf, off, len)}.
43 | */
44 | public void write(byte[] buf, int off, int len) throws IOException {
45 | out.write(buf, off, len);
46 | }
47 |
48 | /**
49 | * Calls {@link java.io.OutputStream#flush() out.flush()}.
50 | */
51 | public void flush() throws IOException {
52 | out.flush();
53 | }
54 |
55 | /**
56 | * Calls {@link java.io.OutputStream#close() out.close()}.
57 | */
58 | public void close() throws IOException {
59 | out.close();
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/src/main/java/org/xbib/io/archive/ArchiveEntry.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2014 Jörg Prante
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.xbib.io.archive;
17 |
18 | import java.util.Date;
19 |
20 | /**
21 | * Represents an entry of an archive.
22 | */
23 | public interface ArchiveEntry {
24 |
25 | /**
26 | * Special value indicating that the size is unknown
27 | */
28 | long SIZE_UNKNOWN = -1;
29 |
30 | ArchiveEntry setName(String name);
31 |
32 | /**
33 | * The name of the entry in the archive. May refer to a file or directory or other item
34 | */
35 | String getName();
36 |
37 | /**
38 | * Set the (uncompressed) entry size in bytes
39 | */
40 | ArchiveEntry setEntrySize(long size);
41 |
42 | /**
43 | * The (uncompressed) size of the entry. May be -1 (SIZE_UNKNOWN) if the size is unknown
44 | */
45 | long getEntrySize();
46 |
47 | /**
48 | * Sets the date of the last modification of this entry.
49 | *
50 | * @param date the date
51 | * @return this archive entry
52 | */
53 | ArchiveEntry setLastModified(Date date);
54 |
55 | /**
56 | * The last modified date of the entry.
57 | */
58 | Date getLastModified();
59 |
60 | /**
61 | * True if the entry refers to a directory
62 | */
63 | boolean isDirectory();
64 |
65 | }
66 |
--------------------------------------------------------------------------------
/src/main/java/org/xbib/io/compress/xz/index/IndexEncoder.java:
--------------------------------------------------------------------------------
1 | package org.xbib.io.compress.xz.index;
2 |
3 | import org.xbib.io.compress.xz.XZIOException;
4 | import org.xbib.io.compress.xz.common.EncoderUtil;
5 |
6 | import java.io.IOException;
7 | import java.io.OutputStream;
8 | import java.util.ArrayList;
9 | import java.util.Iterator;
10 | import java.util.zip.CheckedOutputStream;
11 |
12 | public class IndexEncoder extends IndexBase {
13 |
14 | private final ArrayList the archive session type
29 | * @param the archive input stream type
30 | * @param {
33 |
34 | /**
35 | * Returns the name of this archive codec ("cpio", "tar", "zip")
36 | *
37 | * @return the name
38 | */
39 | String getName();
40 |
41 | /**
42 | * Creates a new archive session with a progress watcher
43 | *
44 | * @param watcher the progress watcher
45 | * @return the new archive session
46 | */
47 | S newSession(BytesProgressWatcher watcher);
48 |
49 | /**
50 | * Creates a new archive input stream
51 | *
52 | * @param in the input stream for the archive input stream
53 | * @return the archive input stream
54 | * @throws IOException if archive input stream can not be created
55 | */
56 | I createArchiveInputStream(InputStream in) throws IOException;
57 |
58 | /**
59 | * Creates a new archive output stream
60 | *
61 | * @param out the output stream for the archive output stream
62 | * @return the archive output stream
63 | * @throws IOException if archive output stream can not be created
64 | */
65 | O createArchiveOutputStream(OutputStream out) throws IOException;
66 |
67 | }
--------------------------------------------------------------------------------
/src/main/java/org/xbib/io/compress/xz/lz/Hash234.java:
--------------------------------------------------------------------------------
1 | package org.xbib.io.compress.xz.lz;
2 |
3 | final class Hash234 extends CRC32Hash {
4 | private static final int HASH_2_SIZE = 1 << 10;
5 | private static final int HASH_2_MASK = HASH_2_SIZE - 1;
6 |
7 | private static final int HASH_3_SIZE = 1 << 16;
8 | private static final int HASH_3_MASK = HASH_3_SIZE - 1;
9 |
10 | private final int hash4Mask;
11 |
12 | private final int[] hash2Table = new int[HASH_2_SIZE];
13 | private final int[] hash3Table = new int[HASH_3_SIZE];
14 | private final int[] hash4Table;
15 |
16 | private int hash2Value = 0;
17 | private int hash3Value = 0;
18 | private int hash4Value = 0;
19 |
20 | static int getHash4Size(int dictSize) {
21 | int h = dictSize - 1;
22 | h |= h >>> 1;
23 | h |= h >>> 2;
24 | h |= h >>> 4;
25 | h |= h >>> 8;
26 | h >>>= 1;
27 | h |= 0xFFFF;
28 | if (h > (1 << 24)) {
29 | h >>>= 1;
30 | }
31 |
32 | return h + 1;
33 | }
34 |
35 | static int getMemoryUsage(int dictSize) {
36 | // Sizes of the hash arrays + a little extra
37 | return (HASH_2_SIZE + HASH_3_SIZE + getHash4Size(dictSize))
38 | / (1024 / 4) + 4;
39 | }
40 |
41 | Hash234(int dictSize) {
42 | hash4Table = new int[getHash4Size(dictSize)];
43 | hash4Mask = hash4Table.length - 1;
44 | }
45 |
46 | void calcHashes(byte[] buf, int off) {
47 | int temp = crcTable[buf[off] & 0xFF] ^ (buf[off + 1] & 0xFF);
48 | hash2Value = temp & HASH_2_MASK;
49 |
50 | temp ^= (buf[off + 2] & 0xFF) << 8;
51 | hash3Value = temp & HASH_3_MASK;
52 |
53 | temp ^= crcTable[buf[off + 3] & 0xFF] << 5;
54 | hash4Value = temp & hash4Mask;
55 | }
56 |
57 | int getHash2Pos() {
58 | return hash2Table[hash2Value];
59 | }
60 |
61 | int getHash3Pos() {
62 | return hash3Table[hash3Value];
63 | }
64 |
65 | int getHash4Pos() {
66 | return hash4Table[hash4Value];
67 | }
68 |
69 | void updateTables(int pos) {
70 | hash2Table[hash2Value] = pos;
71 | hash3Table[hash3Value] = pos;
72 | hash4Table[hash4Value] = pos;
73 | }
74 |
75 | void normalize(int normalizeOffset) {
76 | LZEncoder.normalize(hash2Table, normalizeOffset);
77 | LZEncoder.normalize(hash3Table, normalizeOffset);
78 | LZEncoder.normalize(hash4Table, normalizeOffset);
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/src/main/java/org/xbib/io/compress/xz/FilterOptions.java:
--------------------------------------------------------------------------------
1 | package org.xbib.io.compress.xz;
2 |
3 | import java.io.IOException;
4 | import java.io.InputStream;
5 |
6 | /**
7 | * Base class for filter-specific options classes.
8 | */
9 | public abstract class FilterOptions implements Cloneable {
10 | /**
11 | * Gets how much memory the encoder will need with
12 | * the given filter chain. This function simply calls
13 | * getEncoderMemoryUsage() for every filter
14 | * in the array and returns the sum of the returned values.
15 | */
16 | public static int getEncoderMemoryUsage(FilterOptions[] options) {
17 | int m = 0;
18 |
19 | for (int i = 0; i < options.length; ++i) {
20 | m += options[i].getEncoderMemoryUsage();
21 | }
22 |
23 | return m;
24 | }
25 |
26 | /**
27 | * Gets how much memory the decoder will need with
28 | * the given filter chain. This function simply calls
29 | * getDecoderMemoryUsage() for every filter
30 | * in the array and returns the sum of the returned values.
31 | */
32 | public static int getDecoderMemoryUsage(FilterOptions[] options) {
33 | int m = 0;
34 |
35 | for (int i = 0; i < options.length; ++i) {
36 | m += options[i].getDecoderMemoryUsage();
37 | }
38 |
39 | return m;
40 | }
41 |
42 | /**
43 | * Gets how much memory the encoder will need with these options.
44 | */
45 | public abstract int getEncoderMemoryUsage();
46 |
47 | /**
48 | * Gets a raw (no XZ headers) encoder output stream using these options.
49 | * Raw streams are an advanced feature. In most cases you want to store
50 | * the compressed data in the .xz container format instead of using
51 | * a raw stream. To use this filter in a .xz file, pass this object
52 | * to XZOutputStream.
53 | */
54 | public abstract FinishableOutputStream getOutputStream(
55 | FinishableOutputStream out);
56 |
57 | /**
58 | * Gets how much memory the decoder will need to decompress the data
59 | * that was encoded with these options.
60 | */
61 | public abstract int getDecoderMemoryUsage();
62 |
63 | /**
64 | * Gets a raw (no XZ headers) decoder input stream using these options.
65 | */
66 | public abstract InputStream getInputStream(InputStream in)
67 | throws IOException;
68 |
69 | abstract FilterEncoder getFilterEncoder();
70 |
71 | FilterOptions() {
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/src/main/java/org/xbib/io/compress/xz/simple/IA64.java:
--------------------------------------------------------------------------------
1 | package org.xbib.io.compress.xz.simple;
2 |
3 | public final class IA64 implements SimpleFilter {
4 | private static final int[] BRANCH_TABLE = {
5 | 0, 0, 0, 0, 0, 0, 0, 0,
6 | 0, 0, 0, 0, 0, 0, 0, 0,
7 | 4, 4, 6, 6, 0, 0, 7, 7,
8 | 4, 4, 0, 0, 4, 4, 0, 0};
9 |
10 | private final boolean isEncoder;
11 | private int pos;
12 |
13 | public IA64(boolean isEncoder, int startPos) {
14 | this.isEncoder = isEncoder;
15 | pos = startPos;
16 | }
17 |
18 | public int code(byte[] buf, int off, int len) {
19 | int end = off + len - 16;
20 | int i;
21 |
22 | for (i = off; i <= end; i += 16) {
23 | int instrTemplate = buf[i] & 0x1F;
24 | int mask = BRANCH_TABLE[instrTemplate];
25 |
26 | for (int slot = 0, bitPos = 5; slot < 3; ++slot, bitPos += 41) {
27 | if (((mask >>> slot) & 1) == 0) {
28 | continue;
29 | }
30 |
31 | int bytePos = bitPos >>> 3;
32 | int bitRes = bitPos & 7;
33 |
34 | long instr = 0;
35 | for (int j = 0; j < 6; ++j) {
36 | instr |= (buf[i + bytePos + j] & 0xFFL) << (8 * j);
37 | }
38 |
39 | long instrNorm = instr >>> bitRes;
40 |
41 | if (((instrNorm >>> 37) & 0x0F) != 0x05
42 | || ((instrNorm >>> 9) & 0x07) != 0x00) {
43 | continue;
44 | }
45 |
46 | int src = (int) ((instrNorm >>> 13) & 0x0FFFFF);
47 | src |= ((int) (instrNorm >>> 36) & 1) << 20;
48 | src <<= 4;
49 |
50 | int dest;
51 | if (isEncoder) {
52 | dest = src + (pos + i - off);
53 | } else {
54 | dest = src - (pos + i - off);
55 | }
56 |
57 | dest >>>= 4;
58 |
59 | instrNorm &= ~(0x8FFFFFL << 13);
60 | instrNorm |= (dest & 0x0FFFFFL) << 13;
61 | instrNorm |= (dest & 0x100000L) << (36 - 20);
62 |
63 | instr &= (1 << bitRes) - 1;
64 | instr |= instrNorm << bitRes;
65 |
66 | for (int j = 0; j < 6; ++j) {
67 | buf[i + bytePos + j] = (byte) (instr >>> (8 * j));
68 | }
69 | }
70 | }
71 |
72 | i -= off;
73 | pos += i;
74 | return i;
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/src/main/java/org/xbib/io/archive/ArchiveService.java:
--------------------------------------------------------------------------------
1 |
2 | package org.xbib.io.archive;
3 |
4 | import org.xbib.io.BytesProgressWatcher;
5 | import org.xbib.io.compress.CompressCodecService;
6 |
7 | import java.nio.file.Path;
8 | import java.util.Map;
9 | import java.util.ServiceLoader;
10 | import java.util.Set;
11 | import java.util.WeakHashMap;
12 |
13 | public class ArchiveService {
14 |
15 | private final static Mapn bytes forward in this stream.
12 | * 0. Otherwise, if skipping n bytes
16 | * would cause the position to exceed the stream size, this will do
17 | * equivalent of seek(length()) and the return value will
18 | * be adjusted accordingly.
19 | * n is negative, the position isn't changed and
21 | * the return value is 0. It doesn't seek backward
22 | * because it would conflict with the specification of
23 | * {@link java.io.InputStream#skip(long) InputStream.skip}.
24 | *
25 | * @return 0 if n is negative,
26 | * less than n if skipping n
27 | * bytes would seek past the end of the file,
28 | * n otherwise
29 | * @throws java.io.IOException might be thrown by {@link #seek(long)}
30 | */
31 | public long skip(long n) throws IOException {
32 | if (n <= 0) {
33 | return 0;
34 | }
35 |
36 | long size = length();
37 | long pos = position();
38 | if (pos >= size) {
39 | return 0;
40 | }
41 |
42 | if (size - pos < n) {
43 | n = size - pos;
44 | }
45 |
46 | seek(pos + n);
47 | return n;
48 | }
49 |
50 | /**
51 | * Gets the size of the stream.
52 | */
53 | public abstract long length() throws IOException;
54 |
55 | /**
56 | * Gets the current position in the stream.
57 | */
58 | public abstract long position() throws IOException;
59 |
60 | /**
61 | * Seeks to the specified absolute position in the stream.
62 | * read will return
66 | * -1 to indicate end of stream.
67 | *
68 | * @param pos new read position in the stream
69 | * @throws java.io.IOException if pos is negative or if
70 | * a stream-specific I/O error occurs
71 | */
72 | public abstract void seek(long pos) throws IOException;
73 | }
74 |
--------------------------------------------------------------------------------
/src/main/java/org/xbib/elasticsearch/action/knapsack/abort/TransportKnapsackAbortAction.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2014 Jörg Prante
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.xbib.elasticsearch.action.knapsack.abort;
17 |
18 | import org.elasticsearch.action.ActionListener;
19 | import org.elasticsearch.action.support.ActionFilters;
20 | import org.elasticsearch.action.support.TransportAction;
21 | import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
22 | import org.elasticsearch.common.inject.Inject;
23 | import org.elasticsearch.common.logging.ESLogger;
24 | import org.elasticsearch.common.logging.ESLoggerFactory;
25 | import org.elasticsearch.common.settings.Settings;
26 | import org.elasticsearch.threadpool.ThreadPool;
27 | import org.elasticsearch.transport.TransportService;
28 | import org.xbib.elasticsearch.knapsack.KnapsackService;
29 |
30 | public class TransportKnapsackAbortAction extends TransportActionRandomAccessFile object.
37 | */
38 | public SeekableFileInputStream(RandomAccessFile randomAccessFile) {
39 | this.randomAccessFile = randomAccessFile;
40 | }
41 |
42 | /**
43 | * Calls {@link java.io.RandomAccessFile#read() randomAccessFile.read()}.
44 | */
45 | public int read() throws IOException {
46 | return randomAccessFile.read();
47 | }
48 |
49 | /**
50 | * Calls {@link java.io.RandomAccessFile#read(byte[]) randomAccessFile.read(buf)}.
51 | */
52 | public int read(byte[] buf) throws IOException {
53 | return randomAccessFile.read(buf);
54 | }
55 |
56 | /**
57 | * Calls
58 | * {@link java.io.RandomAccessFile#read(byte[], int, int)
59 | * randomAccessFile.read(buf, off, len)}.
60 | */
61 | public int read(byte[] buf, int off, int len) throws IOException {
62 | return randomAccessFile.read(buf, off, len);
63 | }
64 |
65 | /**
66 | * Calls {@link java.io.RandomAccessFile#close() randomAccessFile.close()}.
67 | */
68 | public void close() throws IOException {
69 | randomAccessFile.close();
70 | }
71 |
72 | /**
73 | * Calls {@link java.io.RandomAccessFile#length() randomAccessFile.length()}.
74 | */
75 | public long length() throws IOException {
76 | return randomAccessFile.length();
77 | }
78 |
79 | /**
80 | * Calls {@link java.io.RandomAccessFile#getFilePointer()
81 | * randomAccessFile.getFilePointer()}.
82 | */
83 | public long position() throws IOException {
84 | return randomAccessFile.getFilePointer();
85 | }
86 |
87 | /**
88 | * Calls {@link java.io.RandomAccessFile#seek(long) randomAccessFile.seek(long)}.
89 | */
90 | public void seek(long pos) throws IOException {
91 | randomAccessFile.seek(pos);
92 | }
93 | }
94 |
--------------------------------------------------------------------------------
/src/main/java/org/xbib/elasticsearch/action/knapsack/pull/KnapsackPullResponse.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2014 Jörg Prante
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.xbib.elasticsearch.action.knapsack.pull;
17 |
18 | import org.elasticsearch.action.ActionResponse;
19 | import org.elasticsearch.common.io.stream.StreamInput;
20 | import org.elasticsearch.common.io.stream.StreamOutput;
21 | import org.elasticsearch.common.xcontent.ToXContent;
22 | import org.elasticsearch.common.xcontent.XContentBuilder;
23 | import org.xbib.elasticsearch.knapsack.KnapsackState;
24 |
25 | import java.io.IOException;
26 |
27 | import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
28 |
29 | public class KnapsackPullResponse extends ActionResponse implements ToXContent {
30 |
31 | private KnapsackState state;
32 |
33 | private boolean running;
34 |
35 | private String archive;
36 |
37 | public KnapsackPullResponse setState(KnapsackState state) {
38 | this.state = state;
39 | return this;
40 | }
41 |
42 | public KnapsackState getState() {
43 | return state;
44 | }
45 |
46 | public KnapsackPullResponse setRunning(boolean running) {
47 | this.running = running;
48 | return this;
49 | }
50 |
51 | public boolean isRunning() {
52 | return running;
53 | }
54 |
55 | @Override
56 | public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
57 | builder.field("running", running).field("state");
58 | state.toXContent(builder, params);
59 | return builder;
60 | }
61 |
62 |
63 | @Override
64 | public void readFrom(StreamInput in) throws IOException {
65 | super.readFrom(in);
66 | state = new KnapsackState();
67 | state.readFrom(in);
68 | running = in.readBoolean();
69 | archive = in.readString();
70 | }
71 |
72 | @Override
73 | public void writeTo(StreamOutput out) throws IOException {
74 | super.writeTo(out);
75 | state.writeTo(out);
76 | out.writeBoolean(running);
77 | out.writeString(archive);
78 | }
79 |
80 | public String toString() {
81 | try {
82 | XContentBuilder builder = jsonBuilder();
83 | builder.startObject();
84 | builder = toXContent(builder, EMPTY_PARAMS);
85 | builder.endObject();
86 | return builder.string();
87 | } catch (IOException e) {
88 | return "";
89 | }
90 | }
91 |
92 | }
93 |
--------------------------------------------------------------------------------
/src/main/java/org/xbib/elasticsearch/action/knapsack/push/KnapsackPushResponse.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2014 Jörg Prante
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.xbib.elasticsearch.action.knapsack.push;
17 |
18 | import org.elasticsearch.action.ActionResponse;
19 | import org.elasticsearch.common.io.stream.StreamInput;
20 | import org.elasticsearch.common.io.stream.StreamOutput;
21 | import org.elasticsearch.common.xcontent.ToXContent;
22 | import org.elasticsearch.common.xcontent.XContentBuilder;
23 | import org.xbib.elasticsearch.knapsack.KnapsackState;
24 |
25 | import java.io.IOException;
26 |
27 | import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
28 |
29 | public class KnapsackPushResponse extends ActionResponse implements ToXContent {
30 |
31 | private KnapsackState state;
32 |
33 | private boolean running;
34 |
35 | private String archive;
36 |
37 | public KnapsackPushResponse setState(KnapsackState state) {
38 | this.state = state;
39 | return this;
40 | }
41 |
42 | public KnapsackState getState() {
43 | return state;
44 | }
45 |
46 | public KnapsackPushResponse setRunning(boolean running) {
47 | this.running = running;
48 | return this;
49 | }
50 |
51 | public boolean isRunning() {
52 | return running;
53 | }
54 |
55 | @Override
56 | public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
57 | builder.field("running", running).field("state");
58 | state.toXContent(builder, params);
59 | return builder;
60 | }
61 |
62 | @Override
63 | public void readFrom(StreamInput in) throws IOException {
64 | super.readFrom(in);
65 | state = new KnapsackState();
66 | state.readFrom(in);
67 | running = in.readBoolean();
68 | archive = in.readString();
69 | }
70 |
71 | @Override
72 | public void writeTo(StreamOutput out) throws IOException {
73 | super.writeTo(out);
74 | state.writeTo(out);
75 | out.writeBoolean(running);
76 | out.writeString(archive);
77 | }
78 |
79 | @Override
80 | public String toString() {
81 | try {
82 | XContentBuilder builder = jsonBuilder();
83 | builder.startObject();
84 | builder = toXContent(builder, EMPTY_PARAMS);
85 | builder.endObject();
86 | return builder.string();
87 | } catch (IOException e) {
88 | return "";
89 | }
90 | }
91 | }
92 |
--------------------------------------------------------------------------------
/src/main/java/org/xbib/elasticsearch/action/knapsack/exp/KnapsackExportRequestBuilder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2014 Jörg Prante
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.xbib.elasticsearch.action.knapsack.exp;
17 |
18 | import org.elasticsearch.action.ActionRequestBuilder;
19 | import org.elasticsearch.action.search.SearchRequest;
20 | import org.elasticsearch.client.ElasticsearchClient;
21 | import org.elasticsearch.common.unit.ByteSizeValue;
22 |
23 | import java.nio.file.Path;
24 | import java.util.Map;
25 |
26 | /**
27 | * Build request for knapsack export action
28 | */
29 | public class KnapsackExportRequestBuilder extends ActionRequestBuilder
57 | * encode("\u20AC_for_Dollar.txt") = "%U20AC_for_Dollar.txt"
58 | * encode("\u00D6lf\u00E4sser.txt") = "\231lf\204sser.txt"
59 | *
60 | *
61 | * @param name A filename or ZIP comment.
62 | * @return A byte buffer with a backing array containing the
63 | * encoded name. Unmappable characters or malformed
64 | * character sequences are mapped to a sequence of utf-16
65 | * words encoded in the format %Uxxxx. It is
66 | * assumed, that the byte buffer is positioned at the
67 | * beginning of the encoded result, the byte buffer has a
68 | * backing array and the limit of the byte buffer points
69 | * to the end of the encoded result.
70 | * @throws java.io.IOException
71 | */
72 | ByteBuffer encode(String name) throws IOException;
73 |
74 | /**
75 | * @param data The byte values to decode.
76 | * @return The decoded string.
77 | * @throws java.io.IOException
78 | */
79 | String decode(byte[] data) throws IOException;
80 | }
81 |
--------------------------------------------------------------------------------
/src/main/java/org/xbib/io/compress/xz/DeltaOutputStream.java:
--------------------------------------------------------------------------------
1 | package org.xbib.io.compress.xz;
2 |
3 | import org.xbib.io.compress.xz.delta.DeltaEncoder;
4 |
5 | import java.io.IOException;
6 |
7 |
8 | class DeltaOutputStream extends FinishableOutputStream {
9 | private static final int TMPBUF_SIZE = 4096;
10 |
11 | private FinishableOutputStream out;
12 | private final DeltaEncoder delta;
13 | private final byte[] tmpbuf = new byte[TMPBUF_SIZE];
14 |
15 | private boolean finished = false;
16 | private IOException exception = null;
17 |
18 | static int getMemoryUsage() {
19 | return 1 + TMPBUF_SIZE / 1024;
20 | }
21 |
22 | DeltaOutputStream(FinishableOutputStream out, DeltaOptions options) {
23 | this.out = out;
24 | delta = new DeltaEncoder(options.getDistance());
25 | }
26 |
27 | public void write(int b) throws IOException {
28 | byte[] buf = new byte[1];
29 | buf[0] = (byte) b;
30 | write(buf, 0, 1);
31 | }
32 |
33 | public void write(byte[] buf, int off, int len) throws IOException {
34 | if (off < 0 || len < 0 || off + len < 0 || off + len > buf.length) {
35 | throw new IndexOutOfBoundsException();
36 | }
37 |
38 | if (exception != null) {
39 | throw exception;
40 | }
41 |
42 | if (finished) {
43 | throw new XZIOException("Stream finished");
44 | }
45 |
46 | try {
47 | while (len > TMPBUF_SIZE) {
48 | delta.encode(buf, off, TMPBUF_SIZE, tmpbuf);
49 | out.write(tmpbuf);
50 | off += TMPBUF_SIZE;
51 | len -= TMPBUF_SIZE;
52 | }
53 |
54 | delta.encode(buf, off, len, tmpbuf);
55 | out.write(tmpbuf, 0, len);
56 | } catch (IOException e) {
57 | exception = e;
58 | throw e;
59 | }
60 | }
61 |
62 | public void flush() throws IOException {
63 | if (exception != null) {
64 | throw exception;
65 | }
66 |
67 | if (finished) {
68 | throw new XZIOException("Stream finished or closed");
69 | }
70 |
71 | try {
72 | out.flush();
73 | } catch (IOException e) {
74 | exception = e;
75 | throw e;
76 | }
77 | }
78 |
79 | public void finish() throws IOException {
80 | if (!finished) {
81 | if (exception != null) {
82 | throw exception;
83 | }
84 |
85 | try {
86 | out.finish();
87 | } catch (IOException e) {
88 | exception = e;
89 | throw e;
90 | }
91 |
92 | finished = true;
93 | }
94 | }
95 |
96 | public void close() throws IOException {
97 | if (out != null) {
98 | try {
99 | out.close();
100 | } catch (IOException e) {
101 | if (exception == null) {
102 | exception = e;
103 | }
104 | }
105 |
106 | out = null;
107 | }
108 |
109 | if (exception != null) {
110 | throw exception;
111 | }
112 | }
113 | }
114 |
--------------------------------------------------------------------------------