use of org.graalvm.polyglot.io.ByteSequence in project graal by oracle.
the class Xar method create.
public static Xar create(ByteSequence bytes) {
ObjectFileReader data = new ObjectFileReader(bytes, false);
// magic
data.getInt();
XarHeader header = XarHeader.create(data);
List<XarFile> files = TocParser.parse(data, header);
ByteSequence heap = data.slice();
return new Xar(header, files, heap);
}
use of org.graalvm.polyglot.io.ByteSequence in project graal by oracle.
the class SourceBuilderTest method testBinarySourcesUnavailableSourceSection.
@Test
public void testBinarySourcesUnavailableSourceSection() {
ByteSequence sequence = ByteSequence.create(new byte[] { 1, 2, 3, 4 });
Source source = Source.newBuilder("", sequence, null).build();
SourceSection section = source.createUnavailableSection();
assertEquals("", section.getCharacters().toString());
assertEquals(0, section.getCharEndIndex());
assertEquals(0, section.getCharIndex());
assertEquals(0, section.getCharLength());
assertEquals(1, section.getEndColumn());
assertEquals(1, section.getEndLine());
assertEquals(1, section.getStartColumn());
assertEquals(1, section.getStartLine());
assertSame(source, section.getSource());
}
use of org.graalvm.polyglot.io.ByteSequence in project graal by oracle.
the class SourceBuilderTest method testMimeTypes.
@Test
public void testMimeTypes() {
setupEnv();
ByteSequence bytes = ByteSequence.create(new byte[8]);
assertNotNull(Source.newBuilder("", "", "").mimeType(null).build());
assertFails(() -> Source.newBuilder("", "", "").mimeType(""), IllegalArgumentException.class);
assertFails(() -> Source.newBuilder("", "", "").mimeType("/"), IllegalArgumentException.class);
assertFails(() -> Source.newBuilder("", "", "").mimeType("a/"), IllegalArgumentException.class);
assertFails(() -> Source.newBuilder("", "", "").mimeType("/a"), IllegalArgumentException.class);
assertEquals("text/a", Source.newBuilder("", "", "").mimeType("text/a").build().getMimeType());
assertEquals("application/a", Source.newBuilder("", bytes, "").mimeType("application/a").build().getMimeType());
}
use of org.graalvm.polyglot.io.ByteSequence in project graal by oracle.
the class SourceBuilderTest method testBinarySources.
@Test
public void testBinarySources() {
ByteSequence sequence = ByteSequence.create(new byte[] { 1, 2, 3, 4 });
Source source = Source.newBuilder("", sequence, null).build();
assertTrue(source.hasBytes());
assertFalse(source.hasCharacters());
assertEquals(4, source.getLength());
assertFails(() -> source.createSection(0), UnsupportedOperationException.class);
assertFails(() -> source.createSection(0, 0), UnsupportedOperationException.class);
assertFails(() -> source.createSection(0, 0, 0), UnsupportedOperationException.class);
SourceSection section = source.createUnavailableSection();
assertFalse(section.isAvailable());
assertTrue(section.getCharacters().length() == 0);
assertFails(() -> source.getCharacters(), UnsupportedOperationException.class);
assertFails(() -> source.getCharacters(0), UnsupportedOperationException.class);
assertFails(() -> source.getColumnNumber(0), UnsupportedOperationException.class);
assertFails(() -> source.getLineCount(), UnsupportedOperationException.class);
assertFails(() -> source.getLineLength(0), UnsupportedOperationException.class);
assertFails(() -> source.getLineNumber(0), UnsupportedOperationException.class);
assertFails(() -> source.getLineStartOffset(0), UnsupportedOperationException.class);
assertFails(() -> source.getReader(), UnsupportedOperationException.class);
assertNull(source.getMimeType());
assertEquals("", source.getLanguage());
assertEquals(sequence, source.getBytes());
assertEquals("Unnamed", source.getName());
assertNull(source.getURL());
assertEquals("truffle:9f64a747e1b97f131fabb6b447296c9b6f0201e79fb3c5356e6c77e89b6a806a/Unnamed", source.getURI().toString());
}
use of org.graalvm.polyglot.io.ByteSequence in project graal by oracle.
the class SourceBuilderTest method testBuildBinarySources.
@Test
public void testBuildBinarySources() throws IOException {
setupEnv();
ByteSequence bytes = ByteSequence.create(new byte[8]);
Source source = Source.newBuilder("", bytes, null).build();
assertTrue(source.hasBytes());
assertFalse(source.hasCharacters());
source = Source.newBuilder("", "", null).content(bytes).build();
assertTrue(source.hasBytes());
assertFalse(source.hasCharacters());
source = Source.newBuilder("", bytes, null).content("").build();
assertFalse(source.hasBytes());
assertTrue(source.hasCharacters());
File file = File.createTempFile("Hello", ".bin").getCanonicalFile();
file.deleteOnExit();
TruffleFile truffleFile = languageEnv.getPublicTruffleFile(file.getPath());
// mime-type not specified + invalid langauge -> characters
source = Source.newBuilder("", truffleFile).build();
assertFalse(source.hasBytes());
assertTrue(source.hasCharacters());
// mime-type not specified + invalid langauge -> characters
source = Source.newBuilder("", truffleFile).content(bytes).build();
assertTrue(source.hasBytes());
assertFalse(source.hasCharacters());
source = Source.newBuilder("", truffleFile).content("").build();
assertFalse(source.hasBytes());
assertTrue(source.hasCharacters());
}
Aggregations