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;
}
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;
}
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());
}
}
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());
}
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);
}
Aggregations