Search in sources :

Example 6 with FormattedText

use of org.cactoos.text.FormattedText in project cactoos by yegor256.

the class NoNulls method next.

@Override
public X next() {
    final X next = this.iterator.next();
    if (next == null) {
        throw new IllegalStateException(new UncheckedText(new FormattedText("Item #%d of %s is NULL", this.pos.get(), this.iterator)).asString());
    }
    this.pos.incrementAndGet();
    return next;
}
Also used : UncheckedText(org.cactoos.text.UncheckedText) FormattedText(org.cactoos.text.FormattedText)

Example 7 with FormattedText

use of org.cactoos.text.FormattedText in project cactoos by yegor256.

the class LoggingInputStream method read.

@Override
public int read(final byte[] buf, final int offset, final int len) throws IOException {
    final Instant start = Instant.now();
    final int byts = this.origin.read(buf, offset, len);
    final Instant end = Instant.now();
    final long millis = Duration.between(start, end).toMillis();
    if (byts > 0) {
        this.bytes.getAndAdd(byts);
        this.time.getAndAdd(millis);
    }
    final UncheckedText msg = new UncheckedText(new FormattedText("Read %d byte(s) from %s in %dms.", this.bytes.get(), this.source, this.time.get()));
    if (byts > 0) {
        if (!this.level.value().equals(Level.INFO)) {
            this.logger.log(this.level.value(), msg.asString());
        }
    } else {
        if (this.level.value().equals(Level.INFO)) {
            this.logger.info(msg.asString());
        }
    }
    return byts;
}
Also used : UncheckedText(org.cactoos.text.UncheckedText) Instant(java.time.Instant) FormattedText(org.cactoos.text.FormattedText)

Example 8 with FormattedText

use of org.cactoos.text.FormattedText in project cactoos by yegor256.

the class LoggingOutputStream method write.

@Override
public void write(final byte[] buf, final int offset, final int len) throws IOException {
    final Instant start = Instant.now();
    this.origin.write(buf, offset, len);
    final Instant end = Instant.now();
    final long millis = Duration.between(start, end).toMillis();
    this.bytes.getAndAdd(len);
    this.time.getAndAdd(millis);
    final Level level = this.logger.getLevel();
    if (!level.equals(Level.INFO)) {
        this.logger.log(level, new UncheckedText(new FormattedText("Written %d byte(s) to %s in %dms.", this.bytes.get(), this.destination, this.time.get())).asString());
    }
}
Also used : UncheckedText(org.cactoos.text.UncheckedText) Instant(java.time.Instant) Level(java.util.logging.Level) FormattedText(org.cactoos.text.FormattedText)

Example 9 with FormattedText

use of org.cactoos.text.FormattedText in project cactoos by yegor256.

the class LoggingOutputStream method close.

@Override
public void close() throws IOException {
    this.origin.close();
    final Level level = this.logger.getLevel();
    if (level.equals(Level.INFO)) {
        this.logger.log(level, new UncheckedText(new FormattedText("Written %d byte(s) to %s in %dms.", this.bytes.get(), this.destination, this.time.get())).asString());
    }
    this.logger.log(level, new UncheckedText(new FormattedText("Closed output stream from %s.", this.destination)).asString());
}
Also used : UncheckedText(org.cactoos.text.UncheckedText) Level(java.util.logging.Level) FormattedText(org.cactoos.text.FormattedText)

Example 10 with FormattedText

use of org.cactoos.text.FormattedText in project cactoos by yegor256.

the class TailOf method stream.

@Override
public InputStream stream() throws Exception {
    if (this.max < this.count) {
        throw new IllegalArgumentException(new FormattedText("Can't tail %d bytes if buffer is set to %d", this.count, this.max).asString());
    }
    final byte[] buffer = new byte[this.max];
    final byte[] response = new byte[this.count];
    int num = 0;
    final InputStream strm = this.input.stream();
    for (int read = strm.read(buffer); read > 0; read = strm.read(buffer)) {
        if (read < this.max && read < this.count) {
            num = this.copyPartial(buffer, response, num, read);
        } else {
            num = this.copy(buffer, response, read);
        }
    }
    return new ByteArrayInputStream(response, 0, num);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FormattedText(org.cactoos.text.FormattedText)

Aggregations

FormattedText (org.cactoos.text.FormattedText)15 UncheckedText (org.cactoos.text.UncheckedText)12 Level (java.util.logging.Level)3 Instant (java.time.Instant)2 Text (org.cactoos.Text)2 IterableOf (org.cactoos.iterable.IterableOf)2 Test (org.junit.jupiter.api.Test)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 TextOf (org.cactoos.text.TextOf)1 HasValues (org.llorllale.cactoos.matchers.HasValues)1 Throws (org.llorllale.cactoos.matchers.Throws)1