| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package com.xpn.xwiki.internal.file; |
| 21 |
|
|
| 22 |
|
import java.io.ByteArrayInputStream; |
| 23 |
|
import java.io.File; |
| 24 |
|
import java.io.FileInputStream; |
| 25 |
|
import java.io.IOException; |
| 26 |
|
import java.io.InputStream; |
| 27 |
|
import java.io.InputStreamReader; |
| 28 |
|
import java.io.OutputStream; |
| 29 |
|
import java.io.OutputStreamWriter; |
| 30 |
|
import java.io.Reader; |
| 31 |
|
import java.io.StringWriter; |
| 32 |
|
import java.io.Writer; |
| 33 |
|
import java.nio.charset.Charset; |
| 34 |
|
import java.util.UUID; |
| 35 |
|
import java.util.concurrent.atomic.AtomicLong; |
| 36 |
|
|
| 37 |
|
import javax.inject.Inject; |
| 38 |
|
import javax.inject.Singleton; |
| 39 |
|
|
| 40 |
|
import org.apache.commons.io.FileUtils; |
| 41 |
|
import org.apache.commons.io.IOUtils; |
| 42 |
|
import org.apache.commons.io.output.DeferredFileOutputStream; |
| 43 |
|
import org.apache.commons.lang3.ArrayUtils; |
| 44 |
|
import org.xwiki.component.annotation.Component; |
| 45 |
|
import org.xwiki.environment.Environment; |
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
@version |
| 51 |
|
@since |
| 52 |
|
|
| 53 |
|
@Component(roles = TemporaryDeferredFileRepository.class) |
| 54 |
|
@Singleton |
| |
|
| 76.5% |
Uncovered Elements: 4 (17) |
Complexity: 6 |
Complexity Density: 0.55 |
|
| 55 |
|
public class TemporaryDeferredFileRepository |
| 56 |
|
{ |
| 57 |
|
private static final int THRESHOLD = 10000; |
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
@version |
| 64 |
|
|
| |
|
| 38.9% |
Uncovered Elements: 22 (36) |
Complexity: 11 |
Complexity Density: 0.58 |
|
| 65 |
|
public class TemporaryDeferredFile |
| 66 |
|
{ |
| 67 |
|
private final String repositoryPath; |
| 68 |
|
|
| 69 |
|
private DeferredFileOutputStream currentOutputStream; |
| 70 |
|
|
| 71 |
|
|
| 72 |
|
@param |
| 73 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 74 |
545 |
TemporaryDeferredFile(String repositoryPath)... |
| 75 |
|
{ |
| 76 |
545 |
this.repositoryPath = repositoryPath; |
| 77 |
|
} |
| 78 |
|
|
| 79 |
|
|
| 80 |
|
@return |
| 81 |
|
@throws@link |
| 82 |
|
|
| |
|
| 77.8% |
Uncovered Elements: 2 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
| 83 |
729 |
public InputStream getInputStream() throws IOException... |
| 84 |
|
{ |
| 85 |
729 |
if (this.currentOutputStream != null) { |
| 86 |
729 |
if (this.currentOutputStream.isInMemory()) { |
| 87 |
541 |
return new ByteArrayInputStream(this.currentOutputStream.getData()); |
| 88 |
|
} else { |
| 89 |
188 |
return new FileInputStream(this.currentOutputStream.getFile()); |
| 90 |
|
} |
| 91 |
|
} else { |
| 92 |
0 |
return new ByteArrayInputStream(ArrayUtils.EMPTY_BYTE_ARRAY); |
| 93 |
|
} |
| 94 |
|
} |
| 95 |
|
|
| 96 |
|
|
| 97 |
|
@return |
| 98 |
|
@throws@link |
| 99 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 100 |
545 |
public OutputStream getOutputStream() throws IOException... |
| 101 |
|
{ |
| 102 |
545 |
File file = createTemporaryFile(this.repositoryPath); |
| 103 |
545 |
this.currentOutputStream = new DeferredFileOutputStream(THRESHOLD, file); |
| 104 |
545 |
return this.currentOutputStream; |
| 105 |
|
} |
| 106 |
|
|
| 107 |
|
|
| 108 |
|
@return |
| 109 |
|
|
| |
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
| 110 |
0 |
public long length()... |
| 111 |
|
{ |
| 112 |
0 |
if (this.currentOutputStream != null) { |
| 113 |
0 |
if (this.currentOutputStream.isInMemory()) { |
| 114 |
0 |
return this.currentOutputStream.getData().length; |
| 115 |
|
} else { |
| 116 |
0 |
return this.currentOutputStream.getFile().length(); |
| 117 |
|
} |
| 118 |
|
} |
| 119 |
|
|
| 120 |
0 |
return 0; |
| 121 |
|
} |
| 122 |
|
|
| 123 |
|
|
| 124 |
|
@return |
| 125 |
|
@throws |
| 126 |
|
|
| |
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
| 127 |
0 |
public byte[] getBytes() throws IOException... |
| 128 |
|
{ |
| 129 |
0 |
if (this.currentOutputStream != null) { |
| 130 |
0 |
if (this.currentOutputStream.isInMemory()) { |
| 131 |
0 |
return this.currentOutputStream.getData(); |
| 132 |
|
} else { |
| 133 |
0 |
return FileUtils.readFileToByteArray(this.currentOutputStream.getFile()); |
| 134 |
|
} |
| 135 |
|
} else { |
| 136 |
0 |
return ArrayUtils.EMPTY_BYTE_ARRAY; |
| 137 |
|
} |
| 138 |
|
} |
| 139 |
|
} |
| 140 |
|
|
| 141 |
|
|
| 142 |
|
@link@link |
| 143 |
|
|
| 144 |
|
@version |
| 145 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (18) |
Complexity: 9 |
Complexity Density: 0.69 |
|
| 146 |
|
public class TemporaryDeferredStringFile extends TemporaryDeferredFile |
| 147 |
|
{ |
| 148 |
|
private final Charset charset; |
| 149 |
|
|
| 150 |
|
|
| 151 |
|
@param |
| 152 |
|
@param |
| 153 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 154 |
545 |
TemporaryDeferredStringFile(String repositoryPath, Charset charset)... |
| 155 |
|
{ |
| 156 |
545 |
super(repositoryPath); |
| 157 |
545 |
this.charset = charset; |
| 158 |
|
} |
| 159 |
|
|
| 160 |
|
|
| 161 |
|
@return |
| 162 |
|
@throws@link |
| 163 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 164 |
729 |
public Reader getReader() throws IOException... |
| 165 |
|
{ |
| 166 |
729 |
return new InputStreamReader(getInputStream(), this.charset); |
| 167 |
|
} |
| 168 |
|
|
| 169 |
|
|
| 170 |
|
@return |
| 171 |
|
@throws@link |
| 172 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 173 |
545 |
public Writer getWriter() throws IOException... |
| 174 |
|
{ |
| 175 |
545 |
OutputStream stream = getOutputStream(); |
| 176 |
|
|
| 177 |
545 |
return new OutputStreamWriter(stream, this.charset); |
| 178 |
|
} |
| 179 |
|
|
| 180 |
|
|
| 181 |
|
@return@link |
| 182 |
|
@throws |
| 183 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 3 |
Complexity Density: 0.6 |
|
| 184 |
620 |
public String getString() throws IOException... |
| 185 |
|
{ |
| 186 |
620 |
try (Reader reader = getReader()) { |
| 187 |
620 |
StringWriter writer = new StringWriter(); |
| 188 |
|
|
| 189 |
620 |
IOUtils.copyLarge(reader, writer); |
| 190 |
|
|
| 191 |
620 |
return writer.toString(); |
| 192 |
|
} |
| 193 |
|
} |
| 194 |
|
|
| 195 |
|
|
| 196 |
|
@param |
| 197 |
|
@throws |
| 198 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 3 |
Complexity Density: 1 |
|
| 199 |
240 |
public void setString(String str) throws IOException... |
| 200 |
|
{ |
| 201 |
240 |
try (Writer writer = getWriter()) { |
| 202 |
240 |
IOUtils.write(str, writer); |
| 203 |
|
} |
| 204 |
|
} |
| 205 |
|
} |
| 206 |
|
|
| 207 |
|
|
| 208 |
|
|
| 209 |
|
|
| 210 |
|
private final String uid = UUID.randomUUID().toString().replace('-', '_'); |
| 211 |
|
|
| 212 |
|
|
| 213 |
|
|
| 214 |
|
|
| 215 |
|
private final AtomicLong counter = new AtomicLong(0); |
| 216 |
|
|
| 217 |
|
@Inject |
| 218 |
|
private Environment environment; |
| 219 |
|
|
| 220 |
|
|
| 221 |
|
@param |
| 222 |
|
@return |
| 223 |
|
@throws |
| 224 |
|
|
| |
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 3 |
Complexity Density: 0.75 |
|
| 225 |
545 |
public File getRepository(String repositoryPath) throws IOException... |
| 226 |
|
{ |
| 227 |
545 |
File repository = new File(this.environment.getTemporaryDirectory(), repositoryPath); |
| 228 |
|
|
| 229 |
545 |
if (!repository.mkdirs() && !repository.exists()) { |
| 230 |
0 |
throw new IOException("Failed to create directory [" + repository + "]"); |
| 231 |
|
} |
| 232 |
|
|
| 233 |
545 |
return repository; |
| 234 |
|
} |
| 235 |
|
|
| 236 |
|
|
| 237 |
|
@param |
| 238 |
|
@return |
| 239 |
|
|
| 240 |
|
@throws |
| 241 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
| 242 |
545 |
public File createTemporaryFile(String repositoryPath) throws IOException... |
| 243 |
|
{ |
| 244 |
545 |
StringBuilder filename = new StringBuilder(); |
| 245 |
545 |
filename.append(this.uid); |
| 246 |
545 |
filename.append('-'); |
| 247 |
545 |
filename.append(this.counter.getAndIncrement()); |
| 248 |
|
|
| 249 |
545 |
return new TemporaryFile(getRepository(repositoryPath), filename.toString()); |
| 250 |
|
} |
| 251 |
|
|
| 252 |
|
|
| 253 |
|
@param |
| 254 |
|
@return@link |
| 255 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 256 |
0 |
public TemporaryDeferredFile createTemporaryDeferredFile(String repositoryPath)... |
| 257 |
|
{ |
| 258 |
0 |
return new TemporaryDeferredFile(repositoryPath); |
| 259 |
|
} |
| 260 |
|
|
| 261 |
|
|
| 262 |
|
@param |
| 263 |
|
@param |
| 264 |
|
@return@link |
| 265 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 266 |
545 |
public TemporaryDeferredStringFile createTemporaryDeferredStringFile(String repositoryPath, Charset charset)... |
| 267 |
|
{ |
| 268 |
545 |
return new TemporaryDeferredStringFile(repositoryPath, charset); |
| 269 |
|
} |
| 270 |
|
} |