Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into chore/remove-commons-…
Browse files Browse the repository at this point in the history
…lang-dependency
  • Loading branch information
DavyLandman committed Jul 16, 2024
2 parents 97131ac + cfa4c57 commit 274e623
Show file tree
Hide file tree
Showing 13 changed files with 224 additions and 64 deletions.
23 changes: 14 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>org.rascalmpl</groupId>
<artifactId>rascal</artifactId>
<version>0.40.3-RC3-SNAPSHOT</version>
<version>0.40.7-SNAPSHOT</version>
<packaging>jar</packaging>

<scm>
Expand Down Expand Up @@ -32,7 +32,7 @@
<exec.mainClass>org.rascalmpl.shell.RascalShell</exec.mainClass>
<rascal.test.memory>2</rascal.test.memory>
<maven.compiler.release>11</maven.compiler.release>
<rascal-maven.version>0.27.4-RC1</rascal-maven.version>
<rascal-maven.version>0.28.0-RC1</rascal-maven.version>
</properties>


Expand Down Expand Up @@ -181,7 +181,7 @@
</plugin>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.3.2</version>
<version>3.4.0</version>
<configuration>
<filesets>
<fileset>
Expand All @@ -198,7 +198,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<version>3.3.1</version>
<executions>
<execution>
<id>test</id>
Expand Down Expand Up @@ -246,7 +246,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.6.1</version>
<version>3.7.1</version>
<executions>
<execution>
<goals>
Expand All @@ -262,7 +262,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>3.0.1</version>
<version>3.1.0</version>
<configuration>
<tagNameFormat>v@{project.version}</tagNameFormat>
<arguments>-Drascal.compile.skip -Drascal.tutor.skip -DskipTests</arguments>
Expand All @@ -271,7 +271,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.3</version>
<version>3.6.0</version>
<executions>
<execution>
<phase>package</phase>
Expand Down Expand Up @@ -323,7 +323,7 @@
<plugin><!-- make sure users are using recent versions of maven -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.4.1</version>
<version>3.5.0</version>
<executions>
<execution>
<id>enforce-maven</id>
Expand Down Expand Up @@ -416,13 +416,18 @@
<dependency> <!-- used by the compression uri feature-->
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.26.0</version>
<version>1.26.1</version>
</dependency>
<dependency> <!-- needed by commons-compress for compressed+...://...zst -->
<groupId>com.github.luben</groupId>
<artifactId>zstd-jni</artifactId>
<version>1.5.5-11</version>
</dependency>
<dependency> <!-- used for base32 encoding in IO and String, needed anyway for commons-compress -->
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.17.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
Expand Down
28 changes: 25 additions & 3 deletions src/org/rascalmpl/library/IO.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -563,24 +563,43 @@ Use `readFile(file, inferCharset=false, charset=DEFAULT_CHARSET)` instead.
public str readFileEnc(loc file, str charset) throws PathNotFound, IO
= readFile(file, inferCharset=false, charset=charset);
@synopsis{Read the content of a file and return it as a base-64 encoded string.}
@description {
}
@javaClass{org.rascalmpl.library.Prelude}
public java str readBase64(loc file)
public java str readBase64(loc file, bool includePadding=true)
throws PathNotFound, IO;
@deprecated{
Use readBase64 instead. Uuencode was a misnomer.
}
public str uuencode(loc file) = readBase64(file);
@synopsis{Decode a base-64 encoded string and write the resulting bytes to a file.}
@description {
}
@javaClass{org.rascalmpl.library.Prelude}
public java void writeBase64(loc file, str content)
throws PathNotFound, IO;
@deprecated{
Use writeBase65 instead. Uudecode was a misnomer.
Use writeBase64 instead. Uudecode was a misnomer.
}
public void uudecode(loc file, str content) = writeBase64(file, content);
@synopsis{Read the content of a file and return it as a base-32 encoded string.}
@description {
}
@javaClass{org.rascalmpl.library.Prelude}
public java str readBase32(loc file, bool includePadding=true)
throws PathNotFound, IO;
@synopsis{Decode a base-32 encoded string and write the resulting bytes to a file.}
@description {
}
@javaClass{org.rascalmpl.library.Prelude}
public java void writeBase32(loc file, str content)
throws PathNotFound, IO;
@synopsis{Read the contents of a file and return it as a list of bytes.}
@javaClass{org.rascalmpl.library.Prelude}
Expand Down Expand Up @@ -692,7 +711,10 @@ public java str createLink(str title, str target);
@javaClass{org.rascalmpl.library.Prelude}
public java str toBase64(loc file)
@deprecated{
use `readBase64` instead.
}
public java str toBase64(loc file, bool includePadding=true)
throws PathNotFound, IO;
@javaClass{org.rascalmpl.library.Prelude}
Expand Down
104 changes: 74 additions & 30 deletions src/org/rascalmpl/library/Prelude.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import java.text.ParseException;
import java.util.Arrays;
import java.util.Base64;
import java.util.Base64.Encoder;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
Expand All @@ -69,6 +70,8 @@
import java.util.function.Consumer;
import java.util.regex.Pattern;

import org.apache.commons.codec.CodecPolicy;
import org.apache.commons.codec.binary.Base32;
import org.rascalmpl.debug.IRascalMonitor;
import org.rascalmpl.exceptions.JavaCompilation;
import org.rascalmpl.exceptions.RuntimeExceptionFactory;
Expand Down Expand Up @@ -132,7 +135,7 @@

public class Prelude {
private static final int FILE_BUFFER_SIZE = 8 * 1024;

protected final URIResolverRegistry REGISTRY = URIResolverRegistry.getInstance();
protected final IValueFactory values;
protected final IRascalValueFactory rascalValues;
Expand Down Expand Up @@ -1431,12 +1434,11 @@ private IList consumeInputStreamLines(Reader in) throws IOException {
}

public IList readFileBytes(ISourceLocation sloc) {

if(trackIO) System.err.println("readFileBytes: " + sloc);
IListWriter w = values.listWriter();

try (InputStream in = REGISTRY.getInputStream(sloc)) {
byte bytes[] = new byte[FILE_BUFFER_SIZE];
byte[] bytes = new byte[FILE_BUFFER_SIZE];
int read;

while ((read = in.read(bytes, 0, bytes.length)) != -1) {
Expand All @@ -1455,9 +1457,12 @@ public IList readFileBytes(ISourceLocation sloc) {
return w.done();
}

public IString readBase64(ISourceLocation sloc) {
int BUFFER_SIZE = 3 * 512;
public IString readBase64(ISourceLocation sloc, IBool includePadding) {
final int BUFFER_SIZE = 3 * 512;
Base64.Encoder encoder = Base64.getEncoder();
if (!includePadding.getValue()) {
encoder = encoder.withoutPadding();
}

try (BufferedInputStream in = new BufferedInputStream(REGISTRY.getInputStream(sloc), BUFFER_SIZE); ) {
StringBuilder result = new StringBuilder();
Expand All @@ -1466,13 +1471,13 @@ public IString readBase64(ISourceLocation sloc) {

// read multiples of 3 until not possible anymore
while ( (len = in.read(chunk)) == BUFFER_SIZE ) {
result.append( encoder.encodeToString(chunk) );
result.append(new String(encoder.encode(chunk), StandardCharsets.ISO_8859_1));
}

// read final chunk which is not a multiple of 3
if ( len > 0 ) {
chunk = Arrays.copyOf(chunk,len);
result.append( encoder.encodeToString(chunk) );
result.append(new String(encoder.encode(chunk), StandardCharsets.ISO_8859_1));
}

return values.string(result.toString());
Expand All @@ -1482,18 +1487,42 @@ public IString readBase64(ISourceLocation sloc) {
}
}

public void writeBase64(ISourceLocation sloc, IString contents) {
int BUFFER_SIZE = 3 * 512;
public void writeBase64(ISourceLocation sloc, IString base64content) {
final int BUFFER_SIZE = 3 * 512;
Base64.Decoder decoder = Base64.getDecoder();

try (BufferedOutputStream out = new BufferedOutputStream(REGISTRY.getOutputStream(sloc, false), BUFFER_SIZE); ) {
out.write(decoder.decode(contents.getValue()));
try (BufferedOutputStream output = new BufferedOutputStream(REGISTRY.getOutputStream(sloc, false), BUFFER_SIZE); ) {
output.write(decoder.decode(base64content.getValue()));
}
catch (IOException e) {
throw RuntimeExceptionFactory.io(values.string(e.getMessage()));
}
}

public IString readBase32(ISourceLocation sloc, IBool includePadding) {
try(BufferedInputStream input = new BufferedInputStream(REGISTRY.getInputStream(sloc))) {
Base32 encoder = new Base32();
String encoded = encoder.encodeToString(input.readAllBytes());
if (!includePadding.getValue()) {
encoded = encoded.replace("=", "");
}
return values.string(encoded);
} catch (IOException e) {
throw RuntimeExceptionFactory.io(values.string(e.getMessage()));
}
}

public void writeBase32(ISourceLocation sloc, IString base32Content) {
try (BufferedOutputStream output = new BufferedOutputStream(REGISTRY.getOutputStream(sloc, false))) {
// The only relevant field we want to change set is the policy
Base32 decoder = new Base32(0, new byte[0], false, (byte) '=', CodecPolicy.LENIENT);
output.write(decoder.decode(base32Content.getValue()));
}
catch (IOException e) {
throw RuntimeExceptionFactory.io(values.string(e.getMessage()));
}
}

public IString createLink(IString title, IString target) {
return values.string("\uE007["+title.getValue().replaceAll("\\]", "_")+"]("+target.getValue()+")");
}
Expand Down Expand Up @@ -3360,50 +3389,65 @@ private void copy(Reader from, Writer to) throws IOException {
}
}


private String toBase64(InputStream src, int estimatedSize) throws IOException {
private String toBase64(InputStream src, int estimatedSize, boolean includePadding) throws IOException {
ByteArrayOutputStream result = new ByteArrayOutputStream(estimatedSize);
OutputStream encoder = Base64.getEncoder().wrap(result);
copy(src, encoder);
encoder.close();
Encoder encoder = Base64.getEncoder();
if (!includePadding) {
encoder = encoder.withoutPadding();
}
OutputStream dest = encoder.wrap(result);
copy(src, dest);
dest.close();
return result.toString(StandardCharsets.ISO_8859_1.name());
}

public IString toBase64(IString in) {
public IString toBase64(IString in, IString charsetName, IBool includePadding) {
try {
InputStream bytes = new ByteBufferBackedInputStream(StandardCharsets.UTF_8.encode(in.getValue()));
return values.string(toBase64(bytes, in.length() * 2));
Charset charset = Charset.forName(charsetName.getValue());
InputStream bytes = new ByteBufferBackedInputStream(charset.encode(in.getValue()));
return values.string(toBase64(bytes, in.length() * 2, includePadding.getValue()));
} catch (IOException e) {
throw RuntimeExceptionFactory.io(values.string(e.getMessage()));
}
}

public IString toBase64(ISourceLocation file) {
try (InputStream in = REGISTRY.getInputStream(file)) {
return values.string(toBase64(in, 1024));
}
catch (IOException e) {
throw RuntimeExceptionFactory.io(values.string(e.getMessage()));
}
public IString toBase64(ISourceLocation file, IBool includePadding) {
return readBase64(file, includePadding);
}



private void fromBase64(String src, OutputStream target) throws IOException {
InputStream bytes = new ByteBufferBackedInputStream(StandardCharsets.ISO_8859_1.encode(src));
copy(Base64.getDecoder().wrap(bytes), target);
}

public IString fromBase64(IString in) {
public IString fromBase64(IString in, IString charset) {
try {
ByteArrayOutputStream result = new ByteArrayOutputStream(in.length());
fromBase64(in.getValue(), result);
return values.string(result.toString(StandardCharsets.UTF_8.name()));
return values.string(result.toString(charset.getValue()));
} catch (IOException e) {
throw RuntimeExceptionFactory.io(values.string(e.getMessage()));
}
}

public IString toBase32(IString in, IString charsetName, IBool includePadding) {
Base32 encoder = new Base32();
Charset charset = Charset.forName(charsetName.getValue());
String encoded = encoder.encodeToString(in.getValue().getBytes(charset));
if (!includePadding.getValue()) {
encoded = encoded.replace("=", "");
}
return values.string(encoded);
}

public IString fromBase32(IString in, IString charsetName) {
// The only relevant field we want to change is the policy
Base32 decoder = new Base32(0, new byte[0], false, (byte) '=', CodecPolicy.LENIENT);
byte[] data = decoder.decode(in.getValue());
Charset charset = Charset.forName(charsetName.getValue());
return values.string(new String(data, charset));
}

public IValue toLowerCase(IString s)
//@doc{toLowerCase -- convert all characters in string s to lowercase.}
{
Expand Down
Loading

0 comments on commit 274e623

Please sign in to comment.