001package org.openimaj.picslurper.output;
002
003import java.io.DataInput;
004import java.io.DataOutput;
005import java.io.IOException;
006import java.io.PrintWriter;
007import java.net.URL;
008import java.util.Scanner;
009
010import org.openimaj.io.ReadWriteable;
011
012import com.google.gson.Gson;
013import com.google.gson.GsonBuilder;
014
015/**
016 * Can serialise itself as bytes or a json string
017 * @author Sina Samangooei (ss@ecs.soton.ac.uk)
018 *
019 */
020public class WriteableFailedURL implements ReadWriteable{
021        private transient Gson gson = new GsonBuilder().create();
022        private static final String IMAGE_OUTPUT_HEADER = "FAILOUTB";
023        private static final String IMAGE_OUTPUT_HEADER_ASCII = "FAILOUTA";
024        private static final String NEWLINE_STR = "NEWLINE";
025        /**
026         *
027         */
028        public URL url;
029        /**
030         *
031         */
032        public String reason;
033        /**
034         *
035         */
036        public WriteableFailedURL() {
037                // all remain null
038        }
039        /**
040         * @param url
041         * @param reason
042         */
043        public WriteableFailedURL(URL url, String reason) {
044                this.url = url;
045                this.reason = reason;
046        }
047        @Override
048        public void readBinary(DataInput in) throws IOException {
049
050                this.url = new URL(in.readUTF());
051                this.reason = in.readUTF();
052
053
054        }
055        @Override
056        public byte[] binaryHeader() {
057                return IMAGE_OUTPUT_HEADER.getBytes();
058        }
059        @Override
060        public void writeBinary(DataOutput out) throws IOException {
061                out.writeUTF(url.toString());
062                out.writeUTF(this.reason);
063        }
064        @Override
065        public void readASCII(Scanner in) throws IOException {
066                this.url = new URL(in.nextLine());
067                this.reason = in.nextLine().replace(NEWLINE_STR, "\n");
068        }
069        @Override
070        public String asciiHeader() {
071                return IMAGE_OUTPUT_HEADER_ASCII;
072        }
073        @Override
074        public void writeASCII(PrintWriter out) throws IOException {
075                out.println(url.toString());
076                out.println(this.reason.replace("\n", NEWLINE_STR));
077        }
078
079
080}