Search in sources :

Example 1 with ByteArrayOutputStream

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);
    }
}
Also used : ByteArrayOutputStream(com.codeforces.commons.io.ByteArrayOutputStream)

Example 2 with ByteArrayOutputStream

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);
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayOutputStream(com.codeforces.commons.io.ByteArrayOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(com.codeforces.commons.io.ByteArrayOutputStream) InputStreamEntity(org.apache.http.entity.InputStreamEntity) GZIPOutputStream(java.util.zip.GZIPOutputStream) BasicHeader(org.apache.http.message.BasicHeader)

Example 3 with ByteArrayOutputStream

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);
    }
}
Also used : ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) java.util(java.util) IOCase(org.apache.commons.io.IOCase) java.util.zip(java.util.zip) ByteArrayOutputStream(com.codeforces.commons.io.ByteArrayOutputStream) Charset(java.nio.charset.Charset) StringUtil(com.codeforces.commons.text.StringUtil) NameFileFilter(org.apache.commons.io.filefilter.NameFileFilter) Math.max(com.codeforces.commons.math.Math.max) Nonnull(javax.annotation.Nonnull) FileHeader(net.lingala.zip4j.model.FileHeader) Nullable(javax.annotation.Nullable) ZipFile(net.lingala.zip4j.core.ZipFile) ZipParameters(net.lingala.zip4j.model.ZipParameters) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) com.codeforces.commons.io(com.codeforces.commons.io) StandardCharsets(java.nio.charset.StandardCharsets) ZipException(net.lingala.zip4j.exception.ZipException) IOUtils(org.apache.commons.io.IOUtils) Contract(org.jetbrains.annotations.Contract) FsSyncException(de.schlichtherle.truezip.fs.FsSyncException) java.io(java.io) de.schlichtherle.truezip.file(de.schlichtherle.truezip.file) MutableBoolean(org.apache.commons.lang3.mutable.MutableBoolean) Patterns(com.codeforces.commons.text.Patterns) MutableBoolean(org.apache.commons.lang3.mutable.MutableBoolean) Charset(java.nio.charset.Charset) ZipException(net.lingala.zip4j.exception.ZipException) ByteArrayOutputStream(com.codeforces.commons.io.ByteArrayOutputStream) ZipFile(net.lingala.zip4j.core.ZipFile) FileHeader(net.lingala.zip4j.model.FileHeader) Nonnull(javax.annotation.Nonnull)

Example 4 with ByteArrayOutputStream

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;
}
Also used : ByteArrayOutputStream(com.codeforces.commons.io.ByteArrayOutputStream) UnknownHostException(java.net.UnknownHostException) Nullable(javax.annotation.Nullable)

Example 5 with ByteArrayOutputStream

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();
}
Also used : ByteArrayOutputStream(com.codeforces.commons.io.ByteArrayOutputStream)

Aggregations

ByteArrayOutputStream (com.codeforces.commons.io.ByteArrayOutputStream)10 Nonnull (javax.annotation.Nonnull)2 Nullable (javax.annotation.Nullable)2 com.codeforces.commons.io (com.codeforces.commons.io)1 Math.max (com.codeforces.commons.math.Math.max)1 Patterns (com.codeforces.commons.text.Patterns)1 StringUtil (com.codeforces.commons.text.StringUtil)1 de.schlichtherle.truezip.file (de.schlichtherle.truezip.file)1 FsSyncException (de.schlichtherle.truezip.fs.FsSyncException)1 java.io (java.io)1 UnknownHostException (java.net.UnknownHostException)1 Charset (java.nio.charset.Charset)1 StandardCharsets (java.nio.charset.StandardCharsets)1 java.util (java.util)1 java.util.zip (java.util.zip)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 Decoder (lzma.sdk.lzma.Decoder)1 Encoder (lzma.sdk.lzma.Encoder)1 ZipFile (net.lingala.zip4j.core.ZipFile)1