Search code examples
javajpegxor

XOR transform jpeg in Folders


This is a bit of code that I found that looks at files in a folder xor transforms the files and outputs them. Based on my searching I believe that it is java but it doesn't run, all I get is errors.

final Path indir = Paths.get("C:\\Temp\\crin");
final Path outdir = Paths.get("C:\\Temp\\crout");
final List<Path> contents = new ArrayList<>();
Files.newDirectoryStream(indir).forEach(contents::add);
for (final Path p : contents) {
    final byte[] arr = Files.readAllBytes(p);
    for (int i = 0; i < arr.length; i++)
        arr[i] ^= 0x42;
    Files.write(outdir.resolve(p.getFileName()), arr);
}

I try this in the cmd and get

javac decode.java

ecode.java:1: error: class, interface, or enum expected
final Path indir = Paths.get("C:\\Temp\\crin");
      ^
decode.java:2: error: class, interface, or enum expected
final Path outdir = Paths.get("C:\\Temp\\crout");
      ^
decode.java:3: error: class, interface, or enum expected
final List<Path> contents = new ArrayList<>();
      ^
decode.java:4: error: class, interface, or enum expected
Files.newDirectoryStream(indir).forEach(contents::add);
^
decode.java:5: error: class, interface, or enum expected
for (final Path p : contents) {
^
decode.java:5: error: class, interface, or enum expected
for (final Path p : contents) {
           ^
decode.java:6: error: class, interface, or enum expected
    final byte[] arr = Files.readAllBytes(p);
          ^
decode.java:7: error: class, interface, or enum expected
    for (int i = 0; i < arr.length; i++)
    ^
decode.java:7: error: class, interface, or enum expected
    for (int i = 0; i < arr.length; i++)
                    ^
decode.java:7: error: class, interface, or enum expected
    for (int i = 0; i < arr.length; i++)
                                    ^
decode.java:9: error: class, interface, or enum expected
    Files.write(outdir.resolve(p.getFileName()), arr);}
    ^
decode.java:9: error: class, interface, or enum expected
    Files.write(outdir.resolve(p.getFileName()), arr);}
                                                      ^
12 errors

This code was described as working, so if I wrong and the code wasn't java what is it, and if it is java what am i doing wrong.

Update

Tried the updated code getting less errors now, I'm now getting this

javac decode.java
decode.java:13: error: ')' expected
            Files.newDirectoryStream(indir).forEach(contents::add);
                                                            ^
decode.java:13: error: illegal start of expression
            Files.newDirectoryStream(indir).forEach(contents::add);
                                                             ^
decode.java:13: error: ';' expected
            Files.newDirectoryStream(indir).forEach(contents::add);
                                                                 ^
3 errors

Solution

  • At a minimum, you need to import the classes you're using. You need to create a class, and it needs to contain a main() method. Something like,

    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.util.ArrayList;
    import java.util.List;
    
    public class decode { // <-- by convention, should be Decode
        public static void main(String[] args) {
            final Path indir = Paths.get("C:\\Temp\\crin");
            final Path outdir = Paths.get("C:\\Temp\\crout");
            final List<Path> contents = new ArrayList<>();
            try {
                Files.newDirectoryStream(indir).forEach(contents::add);
                for (final Path p : contents) {
                    final byte[] arr = Files.readAllBytes(p);
                    for (int i = 0; i < arr.length; i++)
                        arr[i] ^= 0x42;
                    Files.write(outdir.resolve(p.getFileName()), arr);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    note I haven't run your code above and you'll need to add your files to C:\Temp\crin\ for the code to read them.