protected final URI getNamedURI(String name, byte[] bytes, int byteIndex, int length) { String digest; if (bytes != null) { digest = digest(bytes, byteIndex, length); } else { digest = Integer.toString(System.identityHashCode(this), 16); } if (name != null) { digest += '/' + name; } try { return new URI(URI_SCHEME, digest, null); } catch (URISyntaxException ex) { throw new Error(ex); // Should not happen } }
@Override public final boolean equals(Object obj) { if (obj == null || getClass() != obj.getClass()) { return false; } Content other = (Content) obj; return Objects.equals(getCharacters(), other.getCharacters()); }
@Override public int hashCode() { return getHashKey().hashCode(); }
/** * MIME type that is associated with this source. By default file extensions known to the system * are used to determine the MIME type (via registered {@link FileTypeDetector} classes), yet * one can directly {@link Builder#mimeType(java.lang.String) provide a MIME type} to each * source. * * @return MIME type of this source or <code>null</code>, if unknown * @since 0.8 or earlier */ public String getMimeType() { if (mimeType == null) { try { mimeType = content().findMimeType(); } catch (IOException ex) { // swallow and return null } } return mimeType; }
protected final URI getNamedURI(String name, byte[] bytes) { return getNamedURI(name, bytes, 0, bytes.length); }
/** * Returns the name of this resource holding a guest language program. An example would be the * name of a guest language source code file. Name is supposed to be shorter than * {@link #getPath()}. * * @return the name of the guest language program * @since 0.8 or earlier */ public String getName() { return name == null ? content().getName() : name; }
/** * The fully qualified name of the source. In case this source originates from a {@link File}, * then the default path is the normalized, {@link File#getCanonicalPath() canonical path}. * * @since 0.8 or earlier */ public String getPath() { return content().getPath(); }
/** * Get URI of the source. Every source has an associated {@link URI}, which can be used as a * persistent identification of the source. For example one can * {@link com.oracle.truffle.api.debug.DebuggerSession#install(com.oracle.truffle.api.debug.Breakpoint) * register a breakpoint using a URI} to a source that isn't loaded yet and it will be activated * when the source is * {@link com.oracle.truffle.api.vm.PolyglotEngine#eval(com.oracle.truffle.api.source.Source) * evaluated}. The {@link URI} returned by this method should be as unique as possible, yet it * can happen that different {@link Source sources} return the same {@link #getURI} - for * example when content of a {@link Source#newBuilder(java.io.File) file on a disk} changes and * is re-loaded. * * @return a URI, it's never <code>null</code> * @since 0.14 */ public URI getURI() { return uri == null ? content().getURI() : uri; }
/** * Access to the source contents. * * @since 0.8 or earlier */ public Reader getReader() { try { return content().getReader(); } catch (final IOException ex) { return new Reader() { @Override public int read(char[] cbuf, int off, int len) throws IOException { throw ex; } @Override public void close() throws IOException { } }; } }
@Override public boolean equals(Object obj) { SourceAccessor.neverPartOfCompilation("do not call Source.equals from compiled code"); if (obj instanceof Source) { Source other = (Source) obj; return content().equals(other.content()) && equalAttributes(other); } return false; }
holder = buildString(); String type = this.mime == null ? holder.findMimeType() : this.mime; if (type == null) { throw raise(RuntimeException.class, new MissingMIMETypeException());
/** * Returns the code sequence as {@link CharSequence}. Causes the contents of this source to be * loaded if they are loaded lazily. * * @since 0.28 */ public CharSequence getCharacters() { return content().getCharacters(); }
/** * Returns the complete text of the code. Causes the contents of this source to be loaded if * they are loaded lazily. * * @since 0.8 or earlier * @deprecated use {@link #getCharacters()} instead. */ @Deprecated public String getCode() { return content().getCharacters().toString(); }