use of com.codeforces.commons.io.ByteArrayOutputStream in project codeforces-commons by Codeforces.
the class XmlUtil method removeElementsIfExists.
/**
* Removes all elements Xml by XPath.
*
* @param xmlFile From which will be removed elements.
* @param elementXPath XPath of elements to remove.
* @throws IOException In case of I/O error.
*/
public static void removeElementsIfExists(@Nonnull File xmlFile, @Nonnull String elementXPath) throws IOException {
try {
byte[] inputBytes = FileUtil.getBytes(xmlFile);
ByteArrayInputStream xmlInputStream = new ByteArrayInputStream(inputBytes);
ByteArrayOutputStream xmlOutputStream = new ByteArrayOutputStream();
removeElementsIfExists(xmlInputStream, xmlOutputStream, elementXPath);
byte[] outputBytes = xmlOutputStream.toByteArray();
if (!Arrays.equals(inputBytes, outputBytes)) {
FileUtil.writeFile(xmlFile, outputBytes);
}
} catch (IOException e) {
throw new IOException(String.format("Can't find, read or update file '%s' while evaluating XPath '%s'.", xmlFile.getName(), elementXPath), e);
}
}
use of com.codeforces.commons.io.ByteArrayOutputStream in project codeforces-commons by Codeforces.
the class HttpUtilTest method testPostWithGzippedBinaryEntity_HttpClientUtil.
public void testPostWithGzippedBinaryEntity_HttpClientUtil() throws IOException {
long startTimeMillis = System.currentTimeMillis();
CloseableHttpClient httpClient = null;
HttpPost request = null;
org.apache.http.HttpResponse response = null;
try {
httpClient = HttpClientUtil.newHttpClient(20000, 20000);
request = new HttpPost(BASE_TESTING_URL);
HttpEntity entity;
ByteArrayOutputStream gzippedOutputStream = null;
OutputStream gzipOutputStream = null;
InputStream entityInputStream = null;
try {
byte[] bytes = POST_DATA.getBytes(StandardCharsets.UTF_8);
gzippedOutputStream = new ByteArrayOutputStream();
gzipOutputStream = new GZIPOutputStream(gzippedOutputStream);
gzipOutputStream.write(bytes);
gzipOutputStream.close();
entityInputStream = new ByteArrayInputStream(gzippedOutputStream.toByteArray());
entity = new InputStreamEntity(entityInputStream, gzippedOutputStream.size());
} finally {
IoUtil.closeQuietly(gzippedOutputStream, gzipOutputStream, entityInputStream);
}
request.addHeader(new BasicHeader("Content-Encoding", "gzip"));
request.setEntity(entity);
response = httpClient.execute(request);
assertEquals(String.format("Got unexpected response code %d.", response.getStatusLine().getStatusCode()), HttpCode.OK, response.getStatusLine().getStatusCode());
} finally {
HttpClientUtil.closeQuietly(httpClient, request, response);
}
printf("Done 'HttpUtilTest.testPostWithGzippedBinaryEntity_HttpClientUtil' in %d ms.%n", System.currentTimeMillis() - startTimeMillis);
}
use of com.codeforces.commons.io.ByteArrayOutputStream in project codeforces-commons by Codeforces.
the class ZipUtil method formatZipArchiveContentForView.
/**
* Formats content of the ZIP-archive for view and returns result as UTF-8 bytes. The {@code truncated} flag
* indicates that the length of returned view was restricted by {@code maxLength} parameter.
*
* @param zipFile ZIP-archive to format
* @param maxLength maximal allowed length of result
* @param maxEntryLineCount maximal allowed number of content lines to display for a single ZIP-archive entry
* @param maxEntryLineLength maximal allowed length of ZIP-archive entry content line
* @param entryListHeaderPattern pattern of entry list header; parameters: {@code fileName}, {@code filePath}, {@code entryCount}
* @param entryListItemPattern pattern of entry list item; parameters: {@code entryName}, {@code entrySize}, {@code entryIndex} (1-based)
* @param entryListItemSeparatorPattern pattern of entry list separator
* @param entryListCloserPattern pattern of entry list closer; parameters: {@code fileName}, {@code filePath}
* @param entryContentHeaderPattern pattern of entry content header; parameters: {@code entryName}, {@code entrySize}
* @param entryContentLinePattern pattern of entry content line; parameters: {@code entryLine}
* @param entryContentLineSeparatorPattern pattern of entry content separator
* @param entryContentCloserPattern pattern of entry content closer; parameters: {@code entryName}
* @param binaryEntryContentPlaceholderPattern pattern of binary entry content placeholder; parameters: {@code entrySize}
* @param emptyZipFilePlaceholderPattern pattern of empty (no entries) ZIP-file placeholder; parameters: {@code fileName}, {@code filePath}
* @return formatted view of ZIP-archive
* @throws IOException if {@code zipFile} is not a correct ZIP-archive or any other I/O-error has been occured
* @see String#format(String, Object...)
*/
@SuppressWarnings("OverlyLongMethod")
@Nonnull
public static FileUtil.FirstBytes formatZipArchiveContentForView(File zipFile, int maxLength, int maxEntryLineCount, int maxEntryLineLength, @Nullable String entryListHeaderPattern, @Nullable String entryListItemPattern, @Nullable String entryListItemSeparatorPattern, @Nullable String entryListCloserPattern, @Nullable String entryContentHeaderPattern, @Nullable String entryContentLinePattern, @Nullable String entryContentLineSeparatorPattern, @Nullable String entryContentCloserPattern, @Nullable String binaryEntryContentPlaceholderPattern, @Nullable String emptyZipFilePlaceholderPattern) throws IOException {
entryListHeaderPattern = StringUtil.nullToDefault(entryListHeaderPattern, "ZIP-file entries {\n");
entryListItemPattern = StringUtil.nullToDefault(entryListItemPattern, " %3$03d. %1$s (%2$d B)");
entryListItemSeparatorPattern = StringUtil.nullToDefault(entryListItemSeparatorPattern, "\n");
entryListCloserPattern = StringUtil.nullToDefault(entryListCloserPattern, "\n}\n\n");
entryContentHeaderPattern = StringUtil.nullToDefault(entryContentHeaderPattern, "Entry %1$s (%2$d B) {\n");
entryContentLinePattern = StringUtil.nullToDefault(entryContentLinePattern, " %1$s");
entryContentLineSeparatorPattern = StringUtil.nullToDefault(entryContentLineSeparatorPattern, "\n");
entryContentCloserPattern = StringUtil.nullToDefault(entryContentCloserPattern, "\n} // %1$s\n\n");
binaryEntryContentPlaceholderPattern = StringUtil.nullToDefault(binaryEntryContentPlaceholderPattern, " *** BINARY DATA (%1$d B) ***");
emptyZipFilePlaceholderPattern = StringUtil.nullToDefault(emptyZipFilePlaceholderPattern, "Empty ZIP-file.");
try {
Charset charset = StandardCharsets.UTF_8;
ZipFile internalZipFile = new ZipFile(zipFile);
List<?> fileHeaders = internalZipFile.getFileHeaders();
int headerCount = fileHeaders.size();
if (headerCount <= 0) {
return formatEmptyZipFilePlaceholder(zipFile, maxLength, emptyZipFilePlaceholderPattern, charset);
}
MutableBoolean truncated = new MutableBoolean(Boolean.FALSE);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
CountingOutputStream countingOutputStream = new CountingOutputStream(byteArrayOutputStream);
byte[] entryListHeaderBytes = String.format(entryListHeaderPattern, zipFile.getName(), zipFile.getPath(), headerCount).getBytes(charset);
if (!writeBytesForView(countingOutputStream, entryListHeaderBytes, maxLength, truncated)) {
throw new IllegalArgumentException(String.format("Argument 'maxLength' (%d) is less than the length of entry list header '%s' (%d bytes).", maxLength, new String(entryListHeaderBytes, charset), entryListHeaderBytes.length));
}
fileHeaders.sort(Comparator.comparing(header -> ((FileHeader) header).getFileName()));
for (int headerIndex = 0; headerIndex < headerCount; ++headerIndex) {
FileHeader header = (FileHeader) fileHeaders.get(headerIndex);
String fileName = header.getFileName();
String entryListItemAppendix = headerIndex == headerCount - 1 ? String.format(entryListCloserPattern, zipFile.getName(), zipFile.getPath()) : entryListItemSeparatorPattern;
byte[] entryListItemBytes = (String.format(entryListItemPattern, fileName, header.getUncompressedSize(), headerIndex + 1) + entryListItemAppendix).getBytes(charset);
if (!writeBytesForView(countingOutputStream, entryListItemBytes, maxLength, truncated)) {
break;
}
}
for (int headerIndex = 0; headerIndex < headerCount; ++headerIndex) {
FileHeader header = (FileHeader) fileHeaders.get(headerIndex);
if (header.isDirectory()) {
continue;
}
formatAndAppendEntryContent(countingOutputStream, maxLength, truncated, charset, internalZipFile, header, maxEntryLineCount, maxEntryLineLength, entryContentHeaderPattern, entryContentLinePattern, entryContentLineSeparatorPattern, entryContentCloserPattern, binaryEntryContentPlaceholderPattern);
if (truncated.booleanValue()) {
break;
}
}
return new FileUtil.FirstBytes(truncated.booleanValue(), byteArrayOutputStream.toByteArray());
} catch (ZipException e) {
throw new IOException("Can't format ZIP-file for view.", e);
}
}
use of com.codeforces.commons.io.ByteArrayOutputStream in project codeforces-commons by Codeforces.
the class GridFsByteCache method get.
@Nullable
@Override
public byte[] get(@Nonnull String section, @Nonnull String key) {
GridFSDBFile file = fs.findOne(getFilename(section, key));
if (file == null) {
return null;
}
try {
long deadlineTime = (Long) file.getMetaData().get("deadlineTime");
if (System.currentTimeMillis() <= deadlineTime) {
ByteArrayOutputStream dataOutputStream = new ByteArrayOutputStream(NumberUtil.toInt(file.getLength()));
file.writeTo(dataOutputStream);
return dataOutputStream.toByteArray();
}
} catch (Exception e) {
logger.error("Can't get get().", e);
}
return null;
}
use of com.codeforces.commons.io.ByteArrayOutputStream in project codeforces-commons by Codeforces.
the class ZipUtil method zip.
/**
* Adds a directory to a ZIP-archive and returns its bytes.
*
* @param source directory to compress, will not be added itself;
* source directory child files will be placed in the root of archive
* @param level compression level (0-9)
* @param skipFilter skipped files filter or {@code null} to accept all files
* @return ZIP-archive bytes
* @throws IOException if any I/O-exception occured
*/
public static byte[] zip(File source, int level, @Nullable FileFilter skipFilter) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
zipOutputStream.setLevel(level);
try {
addDirectory("", source, zipOutputStream, skipFilter, false);
} finally {
IoUtil.closeQuietly(zipOutputStream, outputStream);
}
return outputStream.toByteArray();
}
Aggregations