java/sql-dk/src/info/globalcode/sql/dk/batch/BatchDecoder.java
branchv_0
changeset 146 4f4f515df807
child 148 1b2f40cd432b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/batch/BatchDecoder.java	Wed Jan 08 19:18:52 2014 +0100
     1.3 @@ -0,0 +1,110 @@
     1.4 +/**
     1.5 + * SQL-DK
     1.6 + * Copyright © 2014 František Kučera (frantovo.cz)
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, either version 3 of the License, or
    1.11 + * (at your option) any later version.
    1.12 + *
    1.13 + * This program is distributed in the hope that it will be useful,
    1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    1.16 + * GNU General Public License for more details.
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License
    1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +package info.globalcode.sql.dk.batch;
    1.22 +
    1.23 +import info.globalcode.sql.dk.Parameter;
    1.24 +import info.globalcode.sql.dk.SQLCommand;
    1.25 +import info.globalcode.sql.dk.SQLCommandNumbered;
    1.26 +import java.io.DataInputStream;
    1.27 +import java.io.InputStream;
    1.28 +import static info.globalcode.sql.dk.batch.BatchConstants.*;
    1.29 +import static info.globalcode.sql.dk.Functions.toHex;
    1.30 +import info.globalcode.sql.dk.SQLType;
    1.31 +import java.io.IOException;
    1.32 +import java.util.ArrayList;
    1.33 +import java.util.Arrays;
    1.34 +import java.util.List;
    1.35 +
    1.36 +/**
    1.37 + *
    1.38 + * @author Ing. František Kučera (frantovo.cz)
    1.39 + */
    1.40 +public class BatchDecoder {
    1.41 +
    1.42 +	public Batch decode(InputStream in) throws BatchException {
    1.43 +		return new BatchFromStream(new DataInputStream(in));
    1.44 +
    1.45 +	}
    1.46 +
    1.47 +	private class BatchFromStream implements Batch {
    1.48 +
    1.49 +		private DataInputStream in;
    1.50 +		private boolean hasNext;
    1.51 +
    1.52 +		public BatchFromStream(DataInputStream in) throws BatchException {
    1.53 +			this.in = in;
    1.54 +			hasNext = verifyHeader();
    1.55 +		}
    1.56 +
    1.57 +		@Override
    1.58 +		public boolean hasNext() throws BatchException {
    1.59 +			return hasNext;
    1.60 +		}
    1.61 +
    1.62 +		@Override
    1.63 +		public SQLCommand next() throws BatchException {
    1.64 +			try {
    1.65 +				String sql = readNextString();
    1.66 +
    1.67 +				int paramCount = in.readInt();
    1.68 +				List<Parameter> parameters = new ArrayList<>(paramCount);
    1.69 +
    1.70 +				for (int i = 0; i < paramCount; i++) {
    1.71 +					SQLType type = SQLType.valueOf(in.readInt());
    1.72 +					String value = readNextString();
    1.73 +					parameters.add(new Parameter(value, type));
    1.74 +				}
    1.75 +
    1.76 +				hasNext = verifyHeader();
    1.77 +
    1.78 +				SQLCommand sqlCommand = new SQLCommandNumbered(sql, parameters);
    1.79 +				return sqlCommand;
    1.80 +			} catch (IOException e) {
    1.81 +				throw new BatchException("Unable to read batch", e);
    1.82 +			}
    1.83 +		}
    1.84 +
    1.85 +		private String readNextString() throws IOException {
    1.86 +			byte[] buffer = new byte[in.readInt()];
    1.87 +			in.read(buffer);
    1.88 +			return new String(buffer, CHARSET);
    1.89 +		}
    1.90 +
    1.91 +		/**
    1.92 +		 * @return true if correct batch header was found | false if EOF was found
    1.93 +		 * @throws BatchException if unexpected data was found (not batch header nor EOF)
    1.94 +		 */
    1.95 +		private boolean verifyHeader() throws BatchException {
    1.96 +			try {
    1.97 +				byte[] buffer = new byte[BATCH_HEADER.length];
    1.98 +				int bytesRead = in.read(buffer);
    1.99 +
   1.100 +				if (bytesRead == BATCH_HEADER.length && Arrays.equals(buffer, BATCH_HEADER)) {
   1.101 +					return true;
   1.102 +				} else if (bytesRead == -1) {
   1.103 +					return false;
   1.104 +				} else {
   1.105 +					throw new BatchException("This is not SQL-DK batch: " + toHex(buffer));
   1.106 +				}
   1.107 +			} catch (IOException e) {
   1.108 +				throw new BatchException("Unable to read batch header", e);
   1.109 +			}
   1.110 +
   1.111 +		}
   1.112 +	}
   1.113 +}