Skip to content

Commit

Permalink
feat(objectionary#37): simplify the solution in order to close all fi…
Browse files Browse the repository at this point in the history
…les properly
  • Loading branch information
volodya-lombrozo committed Sep 7, 2023
1 parent c1040f6 commit f091824
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 34 deletions.
13 changes: 1 addition & 12 deletions src/main/java/org/eolang/jeo/Base64Bytecode.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@

import java.util.Arrays;
import java.util.Base64;
import org.cactoos.Input;
import org.cactoos.bytes.BytesOf;
import org.cactoos.bytes.UncheckedBytes;

/**
* Base64 bytecode.
Expand All @@ -46,19 +43,11 @@ public class Base64Bytecode {
*/
private final byte[] bytes;

/**
* Constructor.
* @param input Input.
*/
Base64Bytecode(final Input input) {
this(new UncheckedBytes(new BytesOf(input)).asBytes());
}

/**
* Constructor.
* @param bytes Bytecode.
*/
private Base64Bytecode(final byte[] bytes) {
Base64Bytecode(final byte[] bytes) {
this.bytes = Arrays.copyOf(bytes, bytes.length);
}

Expand Down
38 changes: 16 additions & 22 deletions src/main/java/org/eolang/jeo/BytecodeIr.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,18 @@

import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;
import lombok.ToString;
import org.cactoos.Input;
import org.cactoos.bytes.BytesOf;
import org.cactoos.bytes.UncheckedBytes;
import org.cactoos.io.InputOf;
import org.cactoos.io.UncheckedInput;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.Opcodes;
Expand Down Expand Up @@ -68,7 +66,7 @@ final class BytecodeIr implements IR {
/**
* Input source.
*/
private final Input input;
private final byte[] input;

/**
* Constructor.
Expand All @@ -83,36 +81,32 @@ final class BytecodeIr implements IR {
* @param input Input source
*/
BytecodeIr(final Input input) {
this.input = input;
this(new UncheckedBytes(new BytesOf(input)).asBytes());
}

/**
* Constructor.
* @param input Input source.
*/
private BytecodeIr(final byte[] input) {
this.input = Arrays.copyOf(input, input.length);
}

@Override
public String name() {
try {
final ClassName name = new ClassName();
new ClassReader(new UncheckedInput(this.input).stream()).accept(name, 0);
return name.asString();
} catch (final IOException ex) {
throw new IllegalStateException(
String.format("Can't parse bytecode '%s'", this.input),
ex
);
}
final ClassName name = new ClassName();
new ClassReader(this.input).accept(name, 0);
return name.asString();
}

@Override
public XML toEO() {
try (InputStream stream = new UncheckedInput(this.input).stream()) {
try {
final ClassPrinter printer = new ClassPrinter();
new ClassReader(stream).accept(printer, 0);
new ClassReader(this.input).accept(printer, 0);
final XMLDocument res = new XMLDocument(new Xembler(printer.directives).xml());
new Schema(res).check();
return res;
} catch (final IOException exception) {
throw new IllegalStateException(
String.format("Can't read input source %s", this.input),
exception
);
} catch (final ImpossibleModificationException exception) {
throw new IllegalStateException(
String.format("Can't build XML from %s", this.input),
Expand Down

0 comments on commit f091824

Please sign in to comment.