ImageLayerSource.java 3.62 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.scene7.is.provider.LayerSourceEnum
 *  com.scene7.is.util.serializers.Serializer
 *  org.jetbrains.annotations.NotNull
 */
package com.scene7.is.ps.provider;

import com.scene7.is.provider.LayerSourceEnum;
import com.scene7.is.ps.provider.LayerSource;
import com.scene7.is.util.serializers.Serializer;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jetbrains.annotations.NotNull;

public class ImageLayerSource
implements LayerSource {
    private static final Pattern DEVICE_NAME = Pattern.compile("^[A-z]+:");
    private static final Pattern PARENT_DIR = Pattern.compile("(^|/)\\.\\.($|/)");
    private static final Serializer<ImageLayerSource> SERIALIZER = new Serializer<ImageLayerSource>(){

        public ImageLayerSource load(@NotNull DataInput in) throws IOException {
            String path = in.readUTF();
            return ImageLayerSource.imageLayerSource(path);
        }

        public void store(ImageLayerSource value, @NotNull DataOutput out) throws IOException {
            out.writeUTF(value.path);
        }

        public int dataLength() throws UnsupportedOperationException {
            throw new UnsupportedOperationException("dataLength");
        }
    };
    @NotNull
    private final String path;

    @NotNull
    public static Serializer<ImageLayerSource> imageLayerSourceSerializer() {
        return SERIALIZER;
    }

    @NotNull
    public static ImageLayerSource imageLayerSource(@NotNull String path) {
        return new ImageLayerSource(path);
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || this.getClass() != obj.getClass()) {
            return false;
        }
        ImageLayerSource that = (ImageLayerSource)obj;
        return this.path.equals(that.path);
    }

    public int hashCode() {
        return this.path.hashCode();
    }

    @NotNull
    public String toString() {
        return "path['" + this.path + "']";
    }

    @NotNull
    @Override
    public LayerSourceEnum getType() {
        return LayerSourceEnum.IS;
    }

    @NotNull
    @Override
    public String getValue() {
        return this.path;
    }

    private void readObject(ObjectInputStream in) throws IOException {
        throw new AssertionError();
    }

    private void writeObject(ObjectOutputStream out) throws IOException {
        throw new AssertionError();
    }

    private ImageLayerSource(@NotNull String path) {
        if (DEVICE_NAME.matcher(path).find()) {
            throw new IllegalArgumentException("path may not start with windows device name");
        }
        if (PARENT_DIR.matcher(path).find()) {
            throw new IllegalArgumentException("path may not contain '..' segments");
        }
        this.path = path;
    }

    protected Object writeReplace() {
        return new Serializable(){
            private ImageLayerSource value;

            private Object readResolve() {
                assert (this.value != null);
                return this.value;
            }

            private void readObject(ObjectInputStream in) throws IOException {
                this.value = (ImageLayerSource)SERIALIZER.load((DataInput)in);
            }

            private void writeObject(ObjectOutputStream out) throws IOException {
                SERIALIZER.store((Object)ImageLayerSource.this, (DataOutput)out);
            }
        };
    }

}