|
| 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 | +package org.apache.tika.io; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.InputStream; |
| 21 | +import java.nio.channels.SeekableByteChannel; |
| 22 | +import java.nio.file.Path; |
| 23 | + |
| 24 | +/** |
| 25 | + * Empty stand-in for content that is never extracted: its emptiness describes the |
| 26 | + * placeholder, not the document, so it reports an unknown length rather than zero. |
| 27 | + * |
| 28 | + * @see TikaInputStream#getPlaceholder() |
| 29 | + */ |
| 30 | +class PlaceholderSource extends InputStream implements TikaInputSource { |
| 31 | + |
| 32 | + private final TemporaryResources tmp; |
| 33 | + private Path spilledPath; |
| 34 | + |
| 35 | + PlaceholderSource(TemporaryResources tmp) { |
| 36 | + this.tmp = tmp; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public int read() { |
| 41 | + return -1; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public int read(byte[] b, int off, int len) { |
| 46 | + // InputStream contract: a zero-length read returns 0, even at EOF |
| 47 | + return len == 0 ? 0 : -1; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public long skip(long n) { |
| 52 | + return 0; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public int available() { |
| 57 | + return 0; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void seekTo(long newPosition) throws IOException { |
| 62 | + if (newPosition != 0) { |
| 63 | + throw new IOException("Invalid seek position: " + newPosition + " (empty source)"); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public Path materializedPath() { |
| 69 | + return spilledPath; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public boolean hasPath() { |
| 74 | + return spilledPath != null; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public Path getPath(String suffix) throws IOException { |
| 79 | + if (spilledPath == null) { |
| 80 | + spilledPath = tmp.createTempFile(suffix); |
| 81 | + } |
| 82 | + return spilledPath; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public long getLength() { |
| 87 | + return -1; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public boolean hasReliableLength() { |
| 92 | + return false; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public boolean isPlaceholder() { |
| 97 | + return true; |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public void enableRewind(CacheMemoryBudget budget) { |
| 102 | + // No-op: there is nothing to rewind |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public SeekableByteChannel getSeekableByteChannel() { |
| 107 | + return new MemorySeekableByteChannel(new byte[0], 0); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public synchronized void mark(int readlimit) { |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public synchronized void reset() { |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public boolean markSupported() { |
| 120 | + return true; |
| 121 | + } |
| 122 | +} |
0 commit comments