Home / Class/ LogUpdateEvent Class — spring-boot Architecture

LogUpdateEvent Class — spring-boot Architecture

Architecture documentation for the LogUpdateEvent class in LogUpdateEvent.java from the spring-boot codebase.

Entity Profile

Source Code

buildpack/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/LogUpdateEvent.java lines 36–152

public class LogUpdateEvent extends UpdateEvent {

	private static final Pattern ANSI_PATTERN = Pattern.compile("\u001B\\[[;\\d]*m");

	private static final Pattern TRAILING_NEW_LINE_PATTERN = Pattern.compile("\\n$");

	private final StreamType streamType;

	private final byte[] payload;

	private final String string;

	LogUpdateEvent(StreamType streamType, byte[] payload) {
		this.streamType = streamType;
		this.payload = payload;
		String string = new String(payload, StandardCharsets.UTF_8);
		string = ANSI_PATTERN.matcher(string).replaceAll("");
		string = TRAILING_NEW_LINE_PATTERN.matcher(string).replaceAll("");
		this.string = string;
	}

	public void print() {
		switch (this.streamType) {
			case STD_OUT -> System.out.println(this);
			case STD_ERR -> System.err.println(this);
		}
	}

	public StreamType getStreamType() {
		return this.streamType;
	}

	public byte[] getPayload() {
		return this.payload;
	}

	@Override
	public String toString() {
		return this.string;
	}

	static void readAll(InputStream inputStream, Consumer<LogUpdateEvent> consumer) throws IOException {
		try {
			LogUpdateEvent event;
			while ((event = LogUpdateEvent.read(inputStream)) != null) {
				consumer.accept(event);
			}
		}
		catch (IllegalStateException ex) {
			byte[] message = (ex.getMessage() == null) ? new byte[0] : ex.getMessage().getBytes(StandardCharsets.UTF_8);
			consumer.accept(new LogUpdateEvent(StreamType.STD_ERR, message));
			StreamUtils.drain(inputStream);
		}
		finally {
			inputStream.close();
		}
	}

	private static @Nullable LogUpdateEvent read(InputStream inputStream) throws IOException {
		byte[] header = read(inputStream, 8);
		if (header == null) {
			return null;
		}
		StreamType streamType = StreamType.forId(header[0]);
		long size = 0;
		for (int i = 0; i < 4; i++) {
			size = (size << 8) + (header[i + 4] & 0xff);
		}
		byte[] payload = read(inputStream, size);
		Assert.state(payload != null, "'payload' must not be null");
		return new LogUpdateEvent(streamType, payload);
	}

	private static byte @Nullable [] read(InputStream inputStream, long size) throws IOException {
		byte[] data = new byte[(int) size];
		int offset = 0;
		do {
			int amountRead = inputStream.read(data, offset, data.length - offset);
			if (amountRead == -1) {
				return null;
			}
			offset += amountRead;
		}
		while (offset < data.length);
		return data;
	}

	/**
	 * Stream types supported by the event.
	 */
	public enum StreamType {

		/**
		 * Input from {@code stdin}.
		 */
		STD_IN,

		/**
		 * Output to {@code stdout}.
		 */
		STD_OUT,

		/**
		 * Output to {@code stderr}.
		 */
		STD_ERR;

		static StreamType forId(byte id) {
			int upperBound = values().length;
			Assert.state(id > 0 && id < upperBound,
					() -> "Stream type is out of bounds. Must be >= 0 and < " + upperBound + ", but was " + id);
			return values()[id];
		}

	}

}

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free