Search in sources :

Example 16 with CountingInputStream

use of org.apache.commons.io.input.CountingInputStream in project pwm by pwm-project.

the class LocalDBUtility method importLocalDB.

private void importLocalDB(final InputStream inputStream, final Appendable out, final long totalBytes) throws PwmOperationalException, IOException {
    this.prepareForImport();
    importLineCounter = 0;
    if (totalBytes > 0) {
        writeStringToOut(out, "total bytes in localdb import source: " + totalBytes);
    }
    writeStringToOut(out, "beginning localdb import...");
    final Instant startTime = Instant.now();
    final TransactionSizeCalculator transactionCalculator = new TransactionSizeCalculator(new TransactionSizeCalculator.SettingsBuilder().setDurationGoal(new TimeDuration(100, TimeUnit.MILLISECONDS)).setMinTransactions(50).setMaxTransactions(5 * 1000).createSettings());
    final Map<LocalDB.DB, Map<String, String>> transactionMap = new HashMap<>();
    for (final LocalDB.DB loopDB : LocalDB.DB.values()) {
        transactionMap.put(loopDB, new TreeMap<>());
    }
    final CountingInputStream countingInputStream = new CountingInputStream(inputStream);
    final EventRateMeter eventRateMeter = new EventRateMeter(TimeDuration.MINUTE);
    final Timer statTimer = new Timer(true);
    statTimer.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            String output = "";
            if (totalBytes > 0) {
                final ProgressInfo progressInfo = new ProgressInfo(startTime, totalBytes, countingInputStream.getByteCount());
                output += progressInfo.debugOutput();
            } else {
                output += "recordsImported=" + importLineCounter;
            }
            output += ", avgTransactionSize=" + transactionCalculator.getTransactionSize() + ", recordsPerMinute=" + eventRateMeter.readEventRate().setScale(2, BigDecimal.ROUND_DOWN);
            writeStringToOut(out, output);
        }
    }, 30 * 1000, 30 * 1000);
    Reader csvReader = null;
    try {
        csvReader = new InputStreamReader(new GZIPInputStream(countingInputStream, GZIP_BUFFER_SIZE), PwmConstants.DEFAULT_CHARSET);
        for (final CSVRecord record : PwmConstants.DEFAULT_CSV_FORMAT.parse(csvReader)) {
            importLineCounter++;
            eventRateMeter.markEvents(1);
            final String dbNameRecordStr = record.get(0);
            final LocalDB.DB db = JavaHelper.readEnumFromString(LocalDB.DB.class, null, dbNameRecordStr);
            final String key = record.get(1);
            final String value = record.get(2);
            if (db == null) {
                writeStringToOut(out, "ignoring localdb import record #" + importLineCounter + ", invalid DB name '" + dbNameRecordStr + "'");
            } else {
                transactionMap.get(db).put(key, value);
                int cachedTransactions = 0;
                for (final LocalDB.DB loopDB : LocalDB.DB.values()) {
                    cachedTransactions += transactionMap.get(loopDB).size();
                }
                if (cachedTransactions >= transactionCalculator.getTransactionSize()) {
                    final long startTxnTime = System.currentTimeMillis();
                    for (final LocalDB.DB loopDB : LocalDB.DB.values()) {
                        localDB.putAll(loopDB, transactionMap.get(loopDB));
                        transactionMap.get(loopDB).clear();
                    }
                    transactionCalculator.recordLastTransactionDuration(TimeDuration.fromCurrent(startTxnTime));
                }
            }
        }
    } finally {
        LOGGER.trace("import process completed");
        statTimer.cancel();
        IOUtils.closeQuietly(csvReader);
        IOUtils.closeQuietly(countingInputStream);
    }
    for (final LocalDB.DB loopDB : LocalDB.DB.values()) {
        localDB.putAll(loopDB, transactionMap.get(loopDB));
        transactionMap.get(loopDB).clear();
    }
    this.markImportComplete();
    writeStringToOut(out, "restore complete, restored " + importLineCounter + " records in " + TimeDuration.fromCurrent(startTime).asLongString());
    statTimer.cancel();
}
Also used : InputStreamReader(java.io.InputStreamReader) TransactionSizeCalculator(password.pwm.util.TransactionSizeCalculator) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Instant(java.time.Instant) CountingInputStream(org.apache.commons.io.input.CountingInputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) EventRateMeter(password.pwm.svc.stats.EventRateMeter) GZIPInputStream(java.util.zip.GZIPInputStream) Timer(java.util.Timer) TimerTask(java.util.TimerTask) ProgressInfo(password.pwm.util.ProgressInfo) TimeDuration(password.pwm.util.java.TimeDuration) CSVRecord(org.apache.commons.csv.CSVRecord) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 17 with CountingInputStream

use of org.apache.commons.io.input.CountingInputStream in project hudson-2.x by hudson.

the class FilePath method installIfNecessaryFrom.

/**
 * Given a tgz/zip file, extracts it to the given target directory, if necessary.
 *
 * <p>
 * This method is a convenience method designed for installing a binary package to a location
 * that supports upgrade and downgrade. Specifically,
 *
 * <ul>
 * <li>If the target directory doesn't exist {@linkplain #mkdirs() it'll be created}.
 * <li>The timestamp of the .tgz file is left in the installation directory upon extraction.
 * <li>If the timestamp left in the directory doesn't match with the timestamp of the current archive file,
 *     the directory contents will be discarded and the archive file will be re-extracted.
 * <li>If the connection is refused but the target directory already exists, it is left alone.
 * </ul>
 *
 * @param archive
 *      The resource that represents the tgz/zip file. This URL must support the "Last-Modified" header.
 *      (Most common usage is to get this from {@link ClassLoader#getResource(String)})
 * @param listener
 *      If non-null, a message will be printed to this listener once this method decides to
 *      extract an archive.
 * @return
 *      true if the archive was extracted. false if the extraction was skipped because the target directory
 *      was considered up to date.
 * @since 1.299
 */
public boolean installIfNecessaryFrom(URL archive, TaskListener listener, String message) throws IOException, InterruptedException {
    try {
        URLConnection con;
        try {
            con = ProxyConfiguration.open(archive);
            con.connect();
        } catch (IOException x) {
            if (this.exists()) {
                // Cannot connect now, so assume whatever was last unpacked is still OK.
                if (listener != null) {
                    listener.getLogger().println("Skipping installation of " + archive + " to " + remote + ": " + x);
                }
                return false;
            } else {
                throw x;
            }
        }
        long sourceTimestamp = con.getLastModified();
        FilePath timestamp = this.child(".timestamp");
        if (this.exists()) {
            if (timestamp.exists() && sourceTimestamp == timestamp.lastModified())
                // already up to date
                return false;
            this.deleteContents();
        } else {
            this.mkdirs();
        }
        if (listener != null)
            listener.getLogger().println(message);
        InputStream in = con.getInputStream();
        CountingInputStream cis = new CountingInputStream(in);
        try {
            if (archive.toExternalForm().endsWith(".zip"))
                unzipFrom(cis);
            else
                untarFrom(cis, GZIP);
        } catch (IOException e) {
            throw new IOException2(String.format("Failed to unpack %s (%d bytes read of total %d)", archive, cis.getByteCount(), con.getContentLength()), e);
        }
        timestamp.touch(sourceTimestamp);
        return true;
    } catch (IOException e) {
        throw new IOException2("Failed to install " + archive + " to " + remote, e);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) RemoteInputStream(hudson.remoting.RemoteInputStream) ObjectInputStream(java.io.ObjectInputStream) ZipInputStream(java.util.zip.ZipInputStream) TarInputStream(hudson.org.apache.tools.tar.TarInputStream) CountingInputStream(org.apache.commons.io.input.CountingInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CountingInputStream(org.apache.commons.io.input.CountingInputStream) IOException(java.io.IOException) URLConnection(java.net.URLConnection) IOException2(hudson.util.IOException2)

Example 18 with CountingInputStream

use of org.apache.commons.io.input.CountingInputStream in project AntennaPod by AntennaPod.

the class ChapterReaderTest method testRealFileAuphonic.

@Test
public void testRealFileAuphonic() throws IOException, ID3ReaderException {
    CountingInputStream inputStream = new CountingInputStream(getClass().getClassLoader().getResource("auphonic.mp3").openStream());
    ChapterReader reader = new ChapterReader(inputStream);
    reader.readInputStream();
    List<Chapter> chapters = reader.getChapters();
    assertEquals(4, chapters.size());
    assertEquals(0, chapters.get(0).getStart());
    assertEquals(3000, chapters.get(1).getStart());
    assertEquals(6000, chapters.get(2).getStart());
    assertEquals(9000, chapters.get(3).getStart());
    assertEquals("Chapter 1 - ❤️😊", chapters.get(0).getTitle());
    assertEquals("Chapter 2 - ßöÄ", chapters.get(1).getTitle());
    assertEquals("Chapter 3 - 爱", chapters.get(2).getTitle());
    assertEquals("Chapter 4", chapters.get(3).getTitle());
    assertEquals("https://example.com", chapters.get(0).getLink());
    assertEquals("https://example.com", chapters.get(1).getLink());
    assertEquals("https://example.com", chapters.get(2).getLink());
    assertEquals("https://example.com", chapters.get(3).getLink());
    assertEquals(EmbeddedChapterImage.makeUrl(765, 308), chapters.get(0).getImageUrl());
    assertEquals(EmbeddedChapterImage.makeUrl(1271, 308), chapters.get(1).getImageUrl());
    assertEquals(EmbeddedChapterImage.makeUrl(1771, 308), chapters.get(2).getImageUrl());
    assertEquals(EmbeddedChapterImage.makeUrl(2259, 308), chapters.get(3).getImageUrl());
}
Also used : CountingInputStream(org.apache.commons.io.input.CountingInputStream) Chapter(de.danoeh.antennapod.model.feed.Chapter) Test(org.junit.Test)

Example 19 with CountingInputStream

use of org.apache.commons.io.input.CountingInputStream in project AntennaPod by AntennaPod.

the class ChapterReaderTest method testReadTitleWithGarbage.

@Test
public void testReadTitleWithGarbage() throws IOException, ID3ReaderException {
    byte[] titleSubframeContent = { ID3Reader.ENCODING_ISO, // Title
    'A', // Null-terminated
    0, // Garbage, should be ignored
    42, // Garbage, should be ignored
    42, // Garbage, should be ignored
    42, // Garbage, should be ignored
    42 };
    FrameHeader header = new FrameHeader(ChapterReader.FRAME_ID_TITLE, titleSubframeContent.length, (short) 0);
    CountingInputStream inputStream = new CountingInputStream(new ByteArrayInputStream(titleSubframeContent));
    ChapterReader reader = new ChapterReader(inputStream);
    Chapter chapter = new ID3Chapter("", 0);
    reader.readChapterSubFrame(header, chapter);
    assertEquals("A", chapter.getTitle());
    // Should skip the garbage and point to the next frame
    assertEquals(titleSubframeContent.length, reader.getPosition());
}
Also used : FrameHeader(de.danoeh.antennapod.parser.media.id3.model.FrameHeader) ByteArrayInputStream(java.io.ByteArrayInputStream) CountingInputStream(org.apache.commons.io.input.CountingInputStream) Chapter(de.danoeh.antennapod.model.feed.Chapter) Test(org.junit.Test)

Example 20 with CountingInputStream

use of org.apache.commons.io.input.CountingInputStream in project AntennaPod by AntennaPod.

the class ChapterReaderTest method testReadChapterWithoutSubframes.

@Test
public void testReadChapterWithoutSubframes() throws IOException, ID3ReaderException {
    FrameHeader header = new FrameHeader(ChapterReader.FRAME_ID_CHAPTER, CHAPTER_WITHOUT_SUBFRAME.length, (short) 0);
    CountingInputStream inputStream = new CountingInputStream(new ByteArrayInputStream(CHAPTER_WITHOUT_SUBFRAME));
    Chapter chapter = new ChapterReader(inputStream).readChapter(header);
    assertEquals(CHAPTER_WITHOUT_SUBFRAME_START_TIME, chapter.getStart());
}
Also used : FrameHeader(de.danoeh.antennapod.parser.media.id3.model.FrameHeader) ByteArrayInputStream(java.io.ByteArrayInputStream) CountingInputStream(org.apache.commons.io.input.CountingInputStream) Chapter(de.danoeh.antennapod.model.feed.Chapter) Test(org.junit.Test)

Aggregations

CountingInputStream (org.apache.commons.io.input.CountingInputStream)31 Test (org.junit.Test)17 ByteArrayInputStream (java.io.ByteArrayInputStream)15 InputStream (java.io.InputStream)8 Chapter (de.danoeh.antennapod.model.feed.Chapter)7 IOException (java.io.IOException)7 FrameHeader (de.danoeh.antennapod.parser.media.id3.model.FrameHeader)4 BufferedInputStream (java.io.BufferedInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 GZIPInputStream (java.util.zip.GZIPInputStream)3 ZipInputStream (java.util.zip.ZipInputStream)3 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 RsrcSection (org.phoenicis.win32.pe.rsrc.RsrcSection)2 Logger (org.slf4j.Logger)2 NodeStatisticsMessage (com.sedmelluq.discord.lavaplayer.remote.message.NodeStatisticsMessage)1 RemoteMessage (com.sedmelluq.discord.lavaplayer.remote.message.RemoteMessage)1 TrackExceptionMessage (com.sedmelluq.discord.lavaplayer.remote.message.TrackExceptionMessage)1 TrackFrameDataMessage (com.sedmelluq.discord.lavaplayer.remote.message.TrackFrameDataMessage)1 TrackStartResponseMessage (com.sedmelluq.discord.lavaplayer.remote.message.TrackStartResponseMessage)1