annotate src/main/java/org/monetdb/mcl/net/MapiSocket.java @ 626:06d69b82d409

Resolve javadoc error and warnings : [javadoc] /home/dinther/dev/javadev/src/main/java/org/monetdb/jdbc/MonetDriver.java:35: error: tag not supported in the generated HTML version: tt [javadoc] * <tt>jdbc:monetdb://&lt;host&gt;[:&lt;port&gt;]/&lt;database&gt;</tt> [javadoc] ^ [javadoc] /home/dinther/dev/javadev/src/main/java/org/monetdb/mcl/io/LineType.java:58: warning: no @param for line [javadoc] public static final LineType classify(String line) { [javadoc] ^ [javadoc] /home/dinther/dev/javadev/src/main/java/org/monetdb/mcl/io/LineType.java:58: warning: no @return [javadoc] public static final LineType classify(String line) { [javadoc] ^ [javadoc] /home/dinther/dev/javadev/src/main/java/org/monetdb/mcl/io/LineType.java:72: warning: no @param for line [javadoc] public static final LineType classify(byte[] line) { [javadoc] ^ [javadoc] /home/dinther/dev/javadev/src/main/java/org/monetdb/mcl/io/LineType.java:72: warning: no @return [javadoc] public static final LineType classify(byte[] line) { [javadoc] ^ [javadoc] /home/dinther/dev/javadev/src/main/java/org/monetdb/jdbc/MonetConnection.java:1700: warning: no @return [javadoc] public UploadHandler getUploadHandler() { [javadoc] ^ [javadoc] /home/dinther/dev/javadev/src/main/java/org/monetdb/jdbc/MonetConnection.java:1716: warning: no @return [javadoc] public DownloadHandler getDownloadHandler() { [javadoc] ^ [javadoc] /home/dinther/dev/javadev/src/main/java/org/monetdb/jdbc/MonetConnection.java:3511: warning: no @param for errorMessage [javadoc] public void sendError(final String errorMessage) throws IOException { [javadoc] ^ [javadoc] /home/dinther/dev/javadev/src/main/java/org/monetdb/jdbc/MonetConnection.java:3511: warning: no @throws for java.io.IOException [javadoc] public void sendError(final String errorMessage) throws IOException { [javadoc] ^ [javadoc] /home/dinther/dev/javadev/src/main/java/org/monetdb/jdbc/MonetConnection.java:3525: warning: no @return [javadoc] public InputStream getStream() throws IOException { [javadoc] ^ [javadoc] /home/dinther/dev/javadev/src/main/java/org/monetdb/jdbc/MonetConnection.java:3525: warning: no @throws for java.io.IOException [javadoc] public InputStream getStream() throws IOException { [javadoc] ^ [javadoc] /home/dinther/dev/javadev/src/main/java/org/monetdb/jdbc/MonetConnection.java:3539: warning: no @param for stream [javadoc] public void downloadTo(final OutputStream stream) throws IOException { [javadoc] ^ [javadoc] /home/dinther/dev/javadev/src/main/java/org/monetdb/jdbc/MonetConnection.java:3539: warning: no @throws for java.io.IOException [javadoc] public void downloadTo(final OutputStream stream) throws IOException { [javadoc] ^ [javadoc] /home/dinther/dev/javadev/src/main/java/org/monetdb/jdbc/MonetConnection.java:3553: warning: no description for @param [javadoc] * @param writer [javadoc] ^ [javadoc] /home/dinther/dev/javadev/src/main/java/org/monetdb/jdbc/MonetConnection.java:3555: warning: no @throws for java.io.IOException [javadoc] public void downloadTo(final Writer writer) throws IOException { [javadoc] ^ [javadoc] /home/dinther/dev/javadev/src/main/java/org/monetdb/jdbc/MonetConnection.java:3336: warning: no @throws for java.io.IOException [javadoc] void handleDownload(Download handle, String name, boolean textMode) throws IOException; [javadoc] ^ [javadoc] /home/dinther/dev/javadev/src/main/java/org/monetdb/jdbc/MonetConnection.java:3367: warning: no @throws for java.io.IOException [javadoc] public void sendError(final String errorMessage) throws IOException { [javadoc] ^ [javadoc] /home/dinther/dev/javadev/src/main/java/org/monetdb/jdbc/MonetConnection.java:3378: warning: no @param for chunkSize [javadoc] public void setChunkSize(final int chunkSize) { [javadoc] ^ [javadoc] /home/dinther/dev/javadev/src/main/java/org/monetdb/jdbc/MonetConnection.java:3387: warning: no @return [javadoc] public PrintStream getStream() throws IOException { [javadoc] ^ [javadoc] /home/dinther/dev/javadev/src/main/java/org/monetdb/jdbc/MonetConnection.java:3387: warning: no @throws for java.io.IOException [javadoc] public PrintStream getStream() throws IOException { [javadoc] ^ [javadoc] /home/dinther/dev/javadev/src/main/java/org/monetdb/jdbc/MonetConnection.java:3423: warning: no @param for inputStream [javadoc] public void uploadFrom(final InputStream inputStream) throws IOException { [javadoc] ^ [javadoc] /home/dinther/dev/javadev/src/main/java/org/monetdb/jdbc/MonetConnection.java:3423: warning: no @throws for java.io.IOException [javadoc] public void uploadFrom(final InputStream inputStream) throws IOException { [javadoc] ^ [javadoc] /home/dinther/dev/javadev/src/main/java/org/monetdb/jdbc/MonetConnection.java:3441: warning: no @throws for java.io.IOException [javadoc] public void uploadFrom(final BufferedReader reader, final long linesToSkip) throws IOException { [javadoc] ^ [javadoc] /home/dinther/dev/javadev/src/main/java/org/monetdb/jdbc/MonetConnection.java:3456: warning: no @throws for java.io.IOException [javadoc] public void uploadFrom(final Reader reader) throws IOException { [javadoc] ^ [javadoc] /home/dinther/dev/javadev/src/main/java/org/monetdb/jdbc/MonetConnection.java:3308: warning: no @throws for java.io.IOException [javadoc] void handleUpload(Upload handle, String name, boolean textMode, long linesToSkip) throws IOException; [javadoc] ^ [javadoc] /home/dinther/dev/javadev/src/main/java/org/monetdb/jdbc/MonetDatabaseMetaData.java:114: warning: @return has already been specified [javadoc] * @return negative of nullsAreSortedHigh() [javadoc] ^ [javadoc] /home/dinther/dev/javadev/src/main/java/org/monetdb/mcl/net/MapiSocket.java:732: warning: no @param for b [javadoc] public boolean setInsertFakePrompts(boolean b) { [javadoc] ^ [javadoc] /home/dinther/dev/javadev/src/main/java/org/monetdb/mcl/net/MapiSocket.java:732: warning: no @return [javadoc] public boolean setInsertFakePrompts(boolean b) { [javadoc] ^ [javadoc] /home/dinther/dev/javadev/src/main/java/org/monetdb/mcl/net/MapiSocket.java:1184: warning: no @return [javadoc] public UploadStream uploadStream(int chunkSize) { [javadoc] ^ [javadoc] /home/dinther/dev/javadev/src/main/java/org/monetdb/mcl/net/MapiSocket.java:1193: warning: no @return [javadoc] public UploadStream uploadStream() { [javadoc] ^ [javadoc] /home/dinther/dev/javadev/src/main/java/org/monetdb/mcl/net/MapiSocket.java:1203: warning: no @return [javadoc] public DownloadStream downloadStream(boolean prependCr) { [javadoc] ^ [javadoc] /home/dinther/dev/javadev/src/main/java/org/monetdb/mcl/net/MapiSocket.java:1259: warning: no @param for cancellationCallback [javadoc] public void setCancellationCallback(final Runnable cancellationCallback) { [javadoc] ^ [javadoc] Building index for all the packages and classes... [javadoc] Building index for all classes... [javadoc] Building index for all classes... [javadoc] Generating /home/dinther/dev/javadev/doc/help-doc.html... [javadoc] 1 error [javadoc] 31 warnings
author Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
date Thu, 20 Jan 2022 19:07:45 +0100 (2022-01-20)
parents 65641a7cea31
children c6fe5dfecafc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1 /*
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
2 * This Source Code Form is subject to the terms of the Mozilla Public
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
5 *
610
6aa38e8c0f2d Updated Copyright year.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 576
diff changeset
6 * Copyright 1997 - July 2008 CWI, August 2008 - 2022 MonetDB B.V.
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
7 */
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
8
391
f523727db392 Moved Java classes from packages starting with nl.cwi.monetdb.* to package org.monetdb.*
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 368
diff changeset
9 package org.monetdb.mcl.net;
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
10
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
11 import java.io.BufferedInputStream;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
12 import java.io.BufferedOutputStream;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
13 import java.io.FileWriter;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
14 import java.io.FilterInputStream;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
15 import java.io.FilterOutputStream;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
16 import java.io.IOException;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
17 import java.io.InputStream;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
18 import java.io.OutputStream;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
19 import java.io.UnsupportedEncodingException;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
20 import java.io.Writer;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
21 import java.net.Socket;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
22 import java.net.SocketException;
225
5ddb1f20d5d5 Socket may also throw an UnknownHostException. Add it to MapiSocket.connect() and catch it in MonetConnection(Properties props).
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 200
diff changeset
23 import java.net.UnknownHostException;
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
24 import java.net.URI;
555
90c346571eb4 Use StandardCharsets.UTF_8 where possible
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 554
diff changeset
25 import java.nio.charset.StandardCharsets;
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
26 import java.security.MessageDigest;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
27 import java.security.NoSuchAlgorithmException;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
28 import java.util.ArrayList;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
29 import java.util.HashSet;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
30 import java.util.List;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
31
391
f523727db392 Moved Java classes from packages starting with nl.cwi.monetdb.* to package org.monetdb.*
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 368
diff changeset
32 import org.monetdb.mcl.MCLException;
f523727db392 Moved Java classes from packages starting with nl.cwi.monetdb.* to package org.monetdb.*
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 368
diff changeset
33 import org.monetdb.mcl.io.BufferedMCLReader;
f523727db392 Moved Java classes from packages starting with nl.cwi.monetdb.* to package org.monetdb.*
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 368
diff changeset
34 import org.monetdb.mcl.io.BufferedMCLWriter;
497
aed7f32e029a Refactor LineType to be an enum
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 495
diff changeset
35 import org.monetdb.mcl.io.LineType;
391
f523727db392 Moved Java classes from packages starting with nl.cwi.monetdb.* to package org.monetdb.*
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 368
diff changeset
36 import org.monetdb.mcl.parser.MCLParseException;
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
37
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
38 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
39 * A Socket for communicating with the MonetDB database in MAPI block
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
40 * mode.
53
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
41 *
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
42 * The MapiSocket implements the protocol specifics of the MAPI block
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
43 * mode protocol, and interfaces it as a socket that delivers a
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
44 * BufferedReader and a BufferedWriter. Because logging in is an
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
45 * integral part of the MAPI protocol, the MapiSocket performs the login
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
46 * procedure. Like the Socket class, various options can be set before
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
47 * calling the connect() method to influence the login process. Only
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
48 * after a successful call to connect() the BufferedReader and
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
49 * BufferedWriter can be retrieved.
194
1296dbcc4958 Resolved javadoc many errors and warnings, such as:
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 180
diff changeset
50 *
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
51 * For each line read, it is determined what type of line it is
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
52 * according to the MonetDB MAPI protocol. This results in a line to be
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
53 * PROMPT, HEADER, RESULT, ERROR or UNKNOWN. Use the getLineType()
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
54 * method on the BufferedMCLReader to retrieve the type of the last
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
55 * line read.
53
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
56 *
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
57 * For debugging purposes a socket level debugging is implemented where
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
58 * each and every interaction to and from the MonetDB server is logged
194
1296dbcc4958 Resolved javadoc many errors and warnings, such as:
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 180
diff changeset
59 * to a file on disk.
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
60 * Incoming messages are prefixed by "RX" (received by the driver),
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
61 * outgoing messages by "TX" (transmitted by the driver). Special
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
62 * decoded non-human readable messages are prefixed with "RD" and "TD"
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
63 * instead. Following this two char prefix, a timestamp follows as the
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
64 * number of milliseconds since the UNIX epoch. The rest of the line is
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
65 * a String representation of the data sent or received.
53
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
66 *
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
67 * The general use of this Socket must be seen only in the full context
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
68 * of a MAPI connection to a server. It has the same ingredients as a
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
69 * normal Socket, allowing for seamless plugging.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
70 * <pre>
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
71 * Socket \ / InputStream ----&gt; (BufferedMCL)Reader
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
72 * &gt; o &lt;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
73 * MapiSocket / \ OutputStream ----&gt; (BufferedMCL)Writer
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
74 * </pre>
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
75 * The MapiSocket allows to retrieve Streams for communicating. They
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
76 * are interfaced, so they can be chained in any way. While the Socket
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
77 * transparently deals with how data is sent over the wire, the actual
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
78 * data read needs to be interpreted, for which a Reader/Writer
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
79 * interface is most sufficient. In particular the BufferedMCL*
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
80 * implementations of those interfaces supply some extra functionality
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
81 * geared towards the format of the data.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
82 *
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
83 * @author Fabian Groffen
576
095e896f9d7a Updated comments. Improved code. Added final keywords
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 573
diff changeset
84 * @version 4.2
391
f523727db392 Moved Java classes from packages starting with nl.cwi.monetdb.* to package org.monetdb.*
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 368
diff changeset
85 * @see org.monetdb.mcl.io.BufferedMCLReader
f523727db392 Moved Java classes from packages starting with nl.cwi.monetdb.* to package org.monetdb.*
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 368
diff changeset
86 * @see org.monetdb.mcl.io.BufferedMCLWriter
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
87 */
391
f523727db392 Moved Java classes from packages starting with nl.cwi.monetdb.* to package org.monetdb.*
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 368
diff changeset
88 public class MapiSocket { /* cannot (yet) be final as nl.cwi.monetdb.mcl.net.MapiSocket extends this class */
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
89 /** The TCP Socket to mserver */
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
90 private Socket con;
53
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
91 /** The TCP Socket timeout in milliseconds. Default is 0 meaning the timeout is disabled (i.e., timeout of infinity) */
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
92 private int soTimeout = 0;
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
93 /** Stream from the Socket for reading */
501
eaad79c3235f Make the fake prompts at block ends optional
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 498
diff changeset
94 private BlockInputStream fromMonet;
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
95 /** Stream from the Socket for writing */
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
96 private OutputStream toMonet;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
97 /** MCLReader on the InputStream */
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
98 private BufferedMCLReader reader;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
99 /** MCLWriter on the OutputStream */
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
100 private BufferedMCLWriter writer;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
101 /** protocol version of the connection */
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
102 private int version;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
103
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
104 /** The database to connect to */
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
105 private String database = null;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
106 /** The language to connect with */
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
107 private String language = "sql";
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
108 /** The hash methods to use (null = default) */
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
109 private String hash = null;
281
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
110
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
111 /** Whether we should follow redirects */
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
112 private boolean followRedirects = true;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
113 /** How many redirections do we follow until we're fed up with it? */
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
114 private int ttl = 10;
281
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
115
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
116 /** Whether we are debugging or not */
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
117 private boolean debug = false;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
118 /** The Writer for the debug log-file */
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
119 private Writer log;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
120
281
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
121 /** The blocksize (hardcoded in compliance with MonetDB common/stream/stream.h) */
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
122 public final static int BLOCK = 8 * 1024 - 2;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
123
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
124 /** A short in two bytes for holding the block size in bytes */
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
125 private final byte[] blklen = new byte[2];
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
126
422
8368cbc670bf Send reply size and time zone during initial handshake
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 406
diff changeset
127 /** Options that can be sent during the auth handshake if the server supports it */
8368cbc670bf Send reply size and time zone during initial handshake
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 406
diff changeset
128 private HandshakeOptions handshakeOptions;
8368cbc670bf Send reply size and time zone during initial handshake
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 406
diff changeset
129
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
130 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
131 * Constructs a new MapiSocket.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
132 */
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
133 public MapiSocket() {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
134 con = null;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
135 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
136
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
137 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
138 * Sets the database to connect to. If database is null, a
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
139 * connection is made to the default database of the server. This
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
140 * is also the default.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
141 *
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
142 * @param db the database
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
143 */
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
144 public void setDatabase(final String db) {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
145 this.database = db;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
146 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
147
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
148 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
149 * Sets the language to use for this connection.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
150 *
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
151 * @param lang the language
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
152 */
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
153 public void setLanguage(final String lang) {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
154 this.language = lang;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
155 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
156
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
157 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
158 * Sets the hash method to use. Note that this method is intended
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
159 * for debugging purposes. Setting a hash method can yield in
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
160 * connection failures. Multiple hash methods can be given by
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
161 * separating the hashes by commas.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
162 * DON'T USE THIS METHOD if you don't know what you're doing.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
163 *
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
164 * @param hash the hash method to use
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
165 */
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
166 public void setHash(final String hash) {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
167 this.hash = hash;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
168 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
169
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
170 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
171 * Sets whether MCL redirections should be followed or not. If set
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
172 * to false, an MCLException will be thrown when a redirect is
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
173 * encountered during connect. The default bahaviour is to
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
174 * automatically follow redirects.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
175 *
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
176 * @param r whether to follow redirects (true) or not (false)
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
177 */
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
178 public void setFollowRedirects(final boolean r) {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
179 this.followRedirects = r;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
180 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
181
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
182 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
183 * Sets the number of redirects that are followed when
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
184 * followRedirects is true. In order to avoid going into an endless
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
185 * loop due to some evil server, or another error, a maximum number
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
186 * of redirects that may be followed can be set here. Note that to
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
187 * disable the following of redirects you should use
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
188 * setFollowRedirects.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
189 *
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
190 * @see #setFollowRedirects(boolean r)
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
191 * @param t the number of redirects before an exception is thrown
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
192 */
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
193 public void setTTL(final int t) {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
194 this.ttl = t;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
195 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
196
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
197 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
198 * Set the SO_TIMEOUT on the underlying Socket. When for some
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
199 * reason the connection to the database hangs, this setting can be
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
200 * useful to break out of this indefinite wait.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
201 * This option must be enabled prior to entering the blocking
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
202 * operation to have effect.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
203 *
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
204 * @param s The specified timeout, in milliseconds. A timeout
281
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
205 * of zero will disable timeout (i.e., timeout of infinity).
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
206 * @throws SocketException Issue with the socket
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
207 */
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
208 public void setSoTimeout(final int s) throws SocketException {
53
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
209 if (s < 0) {
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
210 throw new IllegalArgumentException("timeout can't be negative");
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
211 }
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
212 this.soTimeout = s;
281
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
213 // limit time to wait on blocking operations
53
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
214 if (con != null) {
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
215 con.setSoTimeout(s);
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
216 }
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
217 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
218
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
219 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
220 * Gets the SO_TIMEOUT from the underlying Socket.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
221 *
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
222 * @return the currently in use timeout in milliseconds
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
223 * @throws SocketException Issue with the socket
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
224 */
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
225 public int getSoTimeout() throws SocketException {
53
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
226 if (con != null) {
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
227 this.soTimeout = con.getSoTimeout();
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
228 }
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
229 return this.soTimeout;
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
230 }
75
db46b4ab2b9f Correct the condition for when the log can be closed: only when log instanceof FileWriter
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 53
diff changeset
231
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
232 /**
281
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
233 * Enables/disables debug mode with logging to file
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
234 *
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
235 * @param debug Value to set
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
236 */
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
237 public void setDebug(final boolean debug) {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
238 this.debug = debug;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
239 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
240
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
241 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
242 * Connects to the given host and port, logging in as the given
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
243 * user. If followRedirect is false, a RedirectionException is
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
244 * thrown when a redirect is encountered.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
245 *
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
246 * @param host the hostname, or null for the loopback address
225
5ddb1f20d5d5 Socket may also throw an UnknownHostException. Add it to MapiSocket.connect() and catch it in MonetConnection(Properties props).
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 200
diff changeset
247 * @param port the port number (must be between 0 and 65535, inclusive)
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
248 * @param user the username
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
249 * @param pass the password
53
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
250 * @return A List with informational (warning) messages. If this
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
251 * list is empty; then there are no warnings.
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
252 * @throws IOException if an I/O error occurs when creating the socket
225
5ddb1f20d5d5 Socket may also throw an UnknownHostException. Add it to MapiSocket.connect() and catch it in MonetConnection(Properties props).
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 200
diff changeset
253 * @throws SocketException - if there is an error in the underlying protocol, such as a TCP error.
5ddb1f20d5d5 Socket may also throw an UnknownHostException. Add it to MapiSocket.connect() and catch it in MonetConnection(Properties props).
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 200
diff changeset
254 * @throws UnknownHostException if the IP address of the host could not be determined
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
255 * @throws MCLParseException if bogus data is received
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
256 * @throws MCLException if an MCL related error occurs
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
257 */
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
258 public List<String> connect(final String host, final int port, final String user, final String pass)
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
259 throws IOException, SocketException, UnknownHostException, MCLParseException, MCLException
180
fdf4c888d5b7 Small code and layout improvements
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 90
diff changeset
260 {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
261 // Wrap around the internal connect that needs to know if it
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
262 // should really make a TCP connection or not.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
263 return connect(host, port, user, pass, true);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
264 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
265
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
266 private List<String> connect(final String host, final int port, final String user, final String pass, final boolean makeConnection)
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
267 throws IOException, SocketException, UnknownHostException, MCLParseException, MCLException
180
fdf4c888d5b7 Small code and layout improvements
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 90
diff changeset
268 {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
269 if (ttl-- <= 0)
281
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
270 throw new MCLException("Maximum number of redirects reached, aborting connection attempt.");
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
271
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
272 if (makeConnection) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
273 con = new Socket(host, port);
53
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
274 con.setSoTimeout(this.soTimeout);
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
275 // set nodelay, as it greatly speeds up small messages (like we often do)
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
276 con.setTcpNoDelay(true);
276
fab4e6165be9 use so_keepalive (like the server does)
Niels Nes <niels@cwi.nl>
parents: 261
diff changeset
277 con.setKeepAlive(true);
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
278
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
279 fromMonet = new BlockInputStream(con.getInputStream());
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
280 toMonet = new BlockOutputStream(con.getOutputStream());
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
281 try {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
282 reader = new BufferedMCLReader(fromMonet, "UTF-8");
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
283 writer = new BufferedMCLWriter(toMonet, "UTF-8");
53
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
284 writer.registerReader(reader);
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
285 } catch (UnsupportedEncodingException e) {
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
286 throw new MCLException(e.toString());
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
287 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
288 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
289
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
290 final String c = reader.readLine();
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
291 reader.waitForPrompt();
281
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
292 writer.writeLine(getChallengeResponse(c, user, pass, language, database, hash));
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
293
281
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
294 // read monetdb mserver response till prompt
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
295 final ArrayList<String> redirects = new ArrayList<String>();
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
296 final List<String> warns = new ArrayList<String>();
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
297 String err = "", tmp;
497
aed7f32e029a Refactor LineType to be an enum
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 495
diff changeset
298 LineType lineType;
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
299 do {
281
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
300 tmp = reader.readLine();
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
301 if (tmp == null)
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
302 throw new IOException("Read from " +
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
303 con.getInetAddress().getHostName() + ":" +
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
304 con.getPort() + ": End of stream reached");
281
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
305 lineType = reader.getLineType();
497
aed7f32e029a Refactor LineType to be an enum
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 495
diff changeset
306 if (lineType == LineType.ERROR) {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
307 err += "\n" + tmp.substring(7);
497
aed7f32e029a Refactor LineType to be an enum
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 495
diff changeset
308 } else if (lineType == LineType.INFO) {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
309 warns.add(tmp.substring(1));
497
aed7f32e029a Refactor LineType to be an enum
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 495
diff changeset
310 } else if (lineType == LineType.REDIRECT) {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
311 redirects.add(tmp.substring(1));
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
312 }
497
aed7f32e029a Refactor LineType to be an enum
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 495
diff changeset
313 } while (lineType != LineType.PROMPT);
281
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
314
53
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
315 if (err.length() > 0) {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
316 close();
281
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
317 throw new MCLException(err);
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
318 }
281
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
319
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
320 if (!redirects.isEmpty()) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
321 if (followRedirects) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
322 // Ok, server wants us to go somewhere else. The list
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
323 // might have multiple clues on where to go. For now we
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
324 // don't support anything intelligent but trying the
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
325 // first one. URI should be in form of:
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
326 // "mapi:monetdb://host:port/database?arg=value&..."
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
327 // or
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
328 // "mapi:merovingian://proxy?arg=value&..."
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
329 // note that the extra arguments must be obeyed in both
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
330 // cases
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
331 final String suri = redirects.get(0).toString();
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
332 if (!suri.startsWith("mapi:"))
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
333 throw new MCLException("unsupported redirect: " + suri);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
334
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
335 final URI u;
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
336 try {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
337 u = new URI(suri.substring(5));
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
338 } catch (java.net.URISyntaxException e) {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
339 throw new MCLParseException(e.toString());
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
340 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
341
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
342 tmp = u.getQuery();
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
343 if (tmp != null) {
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
344 final String args[] = tmp.split("&");
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
345 for (int i = 0; i < args.length; i++) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
346 int pos = args[i].indexOf("=");
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
347 if (pos > 0) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
348 tmp = args[i].substring(0, pos);
556
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
349 switch (tmp) {
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
350 case "database":
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
351 tmp = args[i].substring(pos + 1);
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
352 if (!tmp.equals(database)) {
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
353 warns.add("redirect points to different database: " + tmp);
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
354 setDatabase(tmp);
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
355 }
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
356 break;
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
357 case "language":
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
358 tmp = args[i].substring(pos + 1);
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
359 warns.add("redirect specifies use of different language: " + tmp);
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
360 setLanguage(tmp);
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
361 break;
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
362 case "user":
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
363 tmp = args[i].substring(pos + 1);
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
364 if (!tmp.equals(user))
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
365 warns.add("ignoring different username '" + tmp + "' set by " +
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
366 "redirect, what are the security implications?");
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
367 break;
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
368 case "password":
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
369 warns.add("ignoring different password set by redirect, " +
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
370 "what are the security implications?");
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
371 break;
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
372 default:
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
373 warns.add("ignoring unknown argument '" + tmp + "' from redirect");
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
374 break;
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
375 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
376 } else {
53
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
377 warns.add("ignoring illegal argument from redirect: " + args[i]);
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
378 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
379 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
380 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
381
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
382 if (u.getScheme().equals("monetdb")) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
383 // this is a redirect to another (monetdb) server,
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
384 // which means a full reconnect
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
385 // avoid the debug log being closed
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
386 if (debug) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
387 debug = false;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
388 close();
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
389 debug = true;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
390 } else {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
391 close();
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
392 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
393 tmp = u.getPath();
180
fdf4c888d5b7 Small code and layout improvements
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 90
diff changeset
394 if (tmp != null && tmp.length() > 0) {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
395 tmp = tmp.substring(1).trim();
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
396 if (!tmp.isEmpty() && !tmp.equals(database)) {
281
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
397 warns.add("redirect points to different database: " + tmp);
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
398 setDatabase(tmp);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
399 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
400 }
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
401 final int p = u.getPort();
53
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
402 warns.addAll(connect(u.getHost(), p == -1 ? port : p, user, pass, true));
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
403 warns.add("Redirect by " + host + ":" + port + " to " + suri);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
404 } else if (u.getScheme().equals("merovingian")) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
405 // reuse this connection to inline connect to the
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
406 // right database that Merovingian proxies for us
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
407 warns.addAll(connect(host, port, user, pass, false));
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
408 } else {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
409 throw new MCLException("unsupported scheme in redirect: " + suri);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
410 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
411 } else {
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
412 final StringBuilder msg = new StringBuilder("The server sent a redirect for this connection:");
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
413 for (String it : redirects) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
414 msg.append(" [" + it + "]");
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
415 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
416 throw new MCLException(msg.toString());
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
417 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
418 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
419 return warns;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
420 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
421
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
422 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
423 * A little helper function that processes a challenge string, and
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
424 * returns a response string for the server. If the challenge
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
425 * string is null, a challengeless response is returned.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
426 *
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
427 * @param chalstr the challenge string
363
34ce6e1b1be8 Remove support for deprecated MD5 encryption algorithm in MapiSocket.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 350
diff changeset
428 * for example: H8sRMhtevGd:mserver:9:PROT10,RIPEMD160,SHA256,SHA1,COMPRESSION_SNAPPY,COMPRESSION_LZ4:LIT:SHA512:
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
429 * @param username the username to use
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
430 * @param password the password to use
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
431 * @param language the language to use
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
432 * @param database the database to connect to
53
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
433 * @param hash the hash method(s) to use, or NULL for all supported hashes
576
095e896f9d7a Updated comments. Improved code. Added final keywords
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 573
diff changeset
434 * @return the response string for the server
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
435 */
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
436 private String getChallengeResponse(
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
437 final String chalstr,
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
438 String username,
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
439 String password,
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
440 final String language,
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
441 final String database,
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
442 final String hash
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
443 ) throws MCLParseException, MCLException, IOException {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
444 // parse the challenge string, split it on ':'
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
445 final String[] chaltok = chalstr.split(":");
281
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
446 if (chaltok.length <= 5)
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
447 throw new MCLParseException("Server challenge string unusable! It contains too few (" + chaltok.length + ") tokens: " + chalstr);
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
448
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
449 try {
281
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
450 version = Integer.parseInt(chaltok[2]); // protocol version
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
451 } catch (NumberFormatException e) {
281
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
452 throw new MCLParseException("Protocol version (" + chaltok[2] + ") unparseable as integer.");
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
453 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
454
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
455 // handle the challenge according to the version it is
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
456 switch (version) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
457 case 9:
281
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
458 // proto 9 is like 8, but uses a hash instead of the plain password
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
459 // the server tells us (in 6th token) which hash in the
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
460 // challenge after the byte-order token
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
461
281
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
462 String algo;
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
463 String pwhash = chaltok[5];
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
464 /* NOTE: Java doesn't support RIPEMD160 :( */
368
af6db116238d Add javadoc url where the supported hash algorithms are described.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 363
diff changeset
465 /* see: https://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#MessageDigest */
556
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
466 switch (pwhash) {
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
467 case "SHA512":
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
468 algo = "SHA-512";
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
469 break;
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
470 case "SHA384":
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
471 algo = "SHA-384";
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
472 break;
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
473 case "SHA256":
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
474 algo = "SHA-256";
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
475 /* NOTE: Java 7 doesn't support SHA-224. Java 8 does but we have not tested it. It is also not requested yet. */
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
476 break;
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
477 case "SHA1":
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
478 algo = "SHA-1";
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
479 break;
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
480 default:
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
481 /* Note: MD5 has been deprecated by security experts and support is removed from Oct 2020 release */
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
482 throw new MCLException("Unsupported password hash: " + pwhash);
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
483 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
484 try {
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
485 final MessageDigest md = MessageDigest.getInstance(algo);
555
90c346571eb4 Use StandardCharsets.UTF_8 where possible
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 554
diff changeset
486 md.update(password.getBytes(StandardCharsets.UTF_8));
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
487 password = toHex(md.digest());
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
488 } catch (NoSuchAlgorithmException e) {
555
90c346571eb4 Use StandardCharsets.UTF_8 where possible
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 554
diff changeset
489 throw new MCLException("This JVM does not support password hash: " + pwhash + "\n" + e);
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
490 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
491
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
492 // proto 7 (finally) used the challenge and works with a
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
493 // password hash. The supported implementations come
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
494 // from the server challenge. We chose the best hash
281
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
495 // we can find, in the order SHA512, SHA1, MD5, plain.
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
496 // Also the byte-order is reported in the challenge string,
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
497 // which makes sense, since only blockmode is supported.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
498 // proto 8 made this obsolete, but retained the
281
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
499 // byte-order report for future "binary" transports.
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
500 // In proto 8, the byte-order of the blocks is always little
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
501 // endian because most machines today are.
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
502 final String hashes = (hash == null || hash.isEmpty()) ? chaltok[3] : hash;
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
503 final HashSet<String> hashesSet = new HashSet<String>(java.util.Arrays.asList(hashes.toUpperCase().split("[, ]"))); // split on comma or space
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
504
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
505 // if we deal with merovingian, mask our credentials
281
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
506 if (chaltok[1].equals("merovingian") && !language.equals("control")) {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
507 username = "merovingian";
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
508 password = "merovingian";
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
509 }
281
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
510
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
511 // reuse variables algo and pwhash
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
512 algo = null;
281
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
513 pwhash = null;
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
514 if (hashesSet.contains("SHA512")) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
515 algo = "SHA-512";
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
516 pwhash = "{SHA512}";
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
517 } else if (hashesSet.contains("SHA384")) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
518 algo = "SHA-384";
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
519 pwhash = "{SHA384}";
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
520 } else if (hashesSet.contains("SHA256")) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
521 algo = "SHA-256";
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
522 pwhash = "{SHA256}";
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
523 } else if (hashesSet.contains("SHA1")) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
524 algo = "SHA-1";
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
525 pwhash = "{SHA1}";
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
526 } else {
363
34ce6e1b1be8 Remove support for deprecated MD5 encryption algorithm in MapiSocket.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 350
diff changeset
527 /* Note: MD5 has been deprecated by security experts and support is removed from Oct 2020 release */
281
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
528 throw new MCLException("no supported hash algorithms found in " + hashes);
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
529 }
281
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
530 try {
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
531 final MessageDigest md = MessageDigest.getInstance(algo);
555
90c346571eb4 Use StandardCharsets.UTF_8 where possible
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 554
diff changeset
532 md.update(password.getBytes(StandardCharsets.UTF_8));
90c346571eb4 Use StandardCharsets.UTF_8 where possible
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 554
diff changeset
533 md.update(chaltok[0].getBytes(StandardCharsets.UTF_8)); // salt/key
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
534 pwhash += toHex(md.digest());
281
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
535 } catch (NoSuchAlgorithmException e) {
555
90c346571eb4 Use StandardCharsets.UTF_8 where possible
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 554
diff changeset
536 throw new MCLException("This JVM does not support password hash: " + pwhash + "\n" + e);
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
537 }
281
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
538
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
539 // TODO: some day when we need this, we should store this
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
540 if (chaltok[4].equals("BIG")) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
541 // byte-order of server is big-endian
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
542 } else if (chaltok[4].equals("LIT")) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
543 // byte-order of server is little-endian
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
544 } else {
281
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
545 throw new MCLParseException("Invalid byte-order: " + chaltok[4]);
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
546 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
547
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
548 // compose and return response
422
8368cbc670bf Send reply size and time zone during initial handshake
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 406
diff changeset
549 String response = "BIG:" // JVM byte-order is big-endian
8368cbc670bf Send reply size and time zone during initial handshake
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 406
diff changeset
550 + username + ":"
8368cbc670bf Send reply size and time zone during initial handshake
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 406
diff changeset
551 + pwhash + ":"
8368cbc670bf Send reply size and time zone during initial handshake
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 406
diff changeset
552 + language + ":"
576
095e896f9d7a Updated comments. Improved code. Added final keywords
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 573
diff changeset
553 + (database == null ? "" : database) + ":"
095e896f9d7a Updated comments. Improved code. Added final keywords
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 573
diff changeset
554 + "FILETRANS:"; // this capability is added in monetdb-jdbc-3.2.jre8.jar
422
8368cbc670bf Send reply size and time zone during initial handshake
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 406
diff changeset
555 if (chaltok.length > 6) {
8368cbc670bf Send reply size and time zone during initial handshake
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 406
diff changeset
556 // if supported, send handshake options
8368cbc670bf Send reply size and time zone during initial handshake
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 406
diff changeset
557 for (String part : chaltok[6].split(",")) {
8368cbc670bf Send reply size and time zone during initial handshake
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 406
diff changeset
558 if (part.startsWith("sql=") && handshakeOptions != null) {
8368cbc670bf Send reply size and time zone during initial handshake
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 406
diff changeset
559 int level;
8368cbc670bf Send reply size and time zone during initial handshake
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 406
diff changeset
560 try {
8368cbc670bf Send reply size and time zone during initial handshake
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 406
diff changeset
561 level = Integer.parseInt(chaltok[6].substring(4));
8368cbc670bf Send reply size and time zone during initial handshake
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 406
diff changeset
562 } catch (NumberFormatException e) {
8368cbc670bf Send reply size and time zone during initial handshake
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 406
diff changeset
563 throw new MCLParseException("Invalid handshake level: " + chaltok[6]);
8368cbc670bf Send reply size and time zone during initial handshake
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 406
diff changeset
564 }
8368cbc670bf Send reply size and time zone during initial handshake
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 406
diff changeset
565 response += handshakeOptions.formatResponse(level);
8368cbc670bf Send reply size and time zone during initial handshake
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 406
diff changeset
566 break;
8368cbc670bf Send reply size and time zone during initial handshake
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 406
diff changeset
567 }
8368cbc670bf Send reply size and time zone during initial handshake
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 406
diff changeset
568 }
8368cbc670bf Send reply size and time zone during initial handshake
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 406
diff changeset
569 // this ':' delimits the handshake options field.
8368cbc670bf Send reply size and time zone during initial handshake
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 406
diff changeset
570 response += ":";
8368cbc670bf Send reply size and time zone during initial handshake
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 406
diff changeset
571 }
8368cbc670bf Send reply size and time zone during initial handshake
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 406
diff changeset
572 return response;
281
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
573 default:
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
574 throw new MCLException("Unsupported protocol version: " + version);
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
575 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
576 }
75
db46b4ab2b9f Correct the condition for when the log can be closed: only when log instanceof FileWriter
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 53
diff changeset
577
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
578 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
579 * Small helper method to convert a byte string to a hexadecimal
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
580 * string representation.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
581 *
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
582 * @param digest the byte array to convert
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
583 * @return the byte array as hexadecimal string
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
584 */
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
585 private final static String toHex(final byte[] digest) {
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
586 final char[] result = new char[digest.length * 2];
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
587 int pos = 0;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
588 for (int i = 0; i < digest.length; i++) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
589 result[pos++] = hexChar((digest[i] & 0xf0) >> 4);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
590 result[pos++] = hexChar(digest[i] & 0x0f);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
591 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
592 return new String(result);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
593 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
594
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
595 private final static char hexChar(final int n) {
281
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
596 return (n > 9)
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
597 ? (char) ('a' + (n - 10))
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
598 : (char) ('0' + n);
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
599 }
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
600
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
601 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
602 * Returns an InputStream that reads from this open connection on
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
603 * the MapiSocket.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
604 *
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
605 * @return an input stream that reads from this open connection
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
606 */
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
607 public InputStream getInputStream() {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
608 return fromMonet;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
609 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
610
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
611 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
612 * Returns an output stream for this MapiSocket.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
613 *
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
614 * @return an output stream for writing bytes to this MapiSocket
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
615 */
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
616 public OutputStream getOutputStream() {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
617 return toMonet;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
618 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
619
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
620 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
621 * Returns a Reader for this MapiSocket. The Reader is a
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
622 * BufferedMCLReader which does protocol interpretation of the
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
623 * BlockInputStream produced by this MapiSocket.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
624 *
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
625 * @return a BufferedMCLReader connected to this MapiSocket
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
626 */
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
627 public BufferedMCLReader getReader() {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
628 return reader;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
629 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
630
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
631 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
632 * Returns a Writer for this MapiSocket. The Writer is a
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
633 * BufferedMCLWriter which produces protocol compatible data blocks
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
634 * that the BlockOutputStream can properly translate into blocks.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
635 *
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
636 * @return a BufferedMCLWriter connected to this MapiSocket
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
637 */
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
638 public BufferedMCLWriter getWriter() {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
639 return writer;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
640 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
641
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
642 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
643 * Returns the mapi protocol version used by this socket. The
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
644 * protocol version depends on the server being used. Users of the
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
645 * MapiSocket should check this version to act appropriately.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
646 *
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
647 * @return the mapi protocol version
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
648 */
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
649 public int getProtocolVersion() {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
650 return version;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
651 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
652
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
653 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
654 * Enables logging to a file what is read and written from and to
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
655 * the server. Logging can be enabled at any time. However, it is
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
656 * encouraged to start debugging before actually connecting the
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
657 * socket.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
658 *
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
659 * @param filename the name of the file to write to
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
660 * @throws IOException if the file could not be opened for writing
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
661 */
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
662 public void debug(final String filename) throws IOException {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
663 debug(new FileWriter(filename));
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
664 }
75
db46b4ab2b9f Correct the condition for when the log can be closed: only when log instanceof FileWriter
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 53
diff changeset
665
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
666 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
667 * Enables logging to a stream what is read and written from and to
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
668 * the server. Logging can be enabled at any time. However, it is
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
669 * encouraged to start debugging before actually connecting the
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
670 * socket.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
671 *
281
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
672 * @param out to write the log to a print stream
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
673 * @throws IOException if the file could not be opened for writing
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
674 */
281
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
675 // disabled as it is not used by JDBC driver code
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
676 // public void debug(PrintStream out) throws IOException {
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
677 // debug(new PrintWriter(out));
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
678 // }
75
db46b4ab2b9f Correct the condition for when the log can be closed: only when log instanceof FileWriter
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 53
diff changeset
679
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
680 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
681 * Enables logging to a stream what is read and written from and to
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
682 * the server. Logging can be enabled at any time. However, it is
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
683 * encouraged to start debugging before actually connecting the
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
684 * socket.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
685 *
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
686 * @param out to write the log to
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
687 */
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
688 public void debug(final Writer out) {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
689 log = out;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
690 debug = true;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
691 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
692
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
693 /**
281
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
694 * Get the log Writer.
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
695 *
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
696 * @return the log writer
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
697 */
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
698 public Writer getLogWriter() {
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
699 return log;
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
700 }
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
701
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
702 /**
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
703 * Writes a logline tagged with a timestamp using the given type and message
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
704 * and optionally flushes afterwards.
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
705 *
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
706 * Used for debugging purposes only and represents a message data that is
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
707 * connected to reading (RD or RX) or writing (TD or TX) to the socket.
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
708 * R=Receive, T=Transmit, D=Data, X=??
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
709 *
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
710 * @param type message type: either RD, RX, TD or TX
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
711 * @param message the message to log
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
712 * @param flush whether we need to flush buffered data to the logfile.
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
713 * @throws IOException if an IO error occurs while writing to the logfile
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
714 */
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
715 private final void log(final String type, final String message, final boolean flush) throws IOException {
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
716 log.write(type + System.currentTimeMillis() + ": " + message + "\n");
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
717 if (flush)
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
718 log.flush();
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
719 }
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
720
422
8368cbc670bf Send reply size and time zone during initial handshake
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 406
diff changeset
721 public void setHandshakeOptions(HandshakeOptions handshakeOptions) {
8368cbc670bf Send reply size and time zone during initial handshake
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 406
diff changeset
722 this.handshakeOptions = handshakeOptions;
8368cbc670bf Send reply size and time zone during initial handshake
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 406
diff changeset
723 }
8368cbc670bf Send reply size and time zone during initial handshake
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 406
diff changeset
724
8368cbc670bf Send reply size and time zone during initial handshake
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 406
diff changeset
725 public HandshakeOptions getHandshakeOptions() {
8368cbc670bf Send reply size and time zone during initial handshake
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 406
diff changeset
726 return handshakeOptions;
8368cbc670bf Send reply size and time zone during initial handshake
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 406
diff changeset
727 }
8368cbc670bf Send reply size and time zone during initial handshake
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 406
diff changeset
728
535
c9d88af06d35 Javadoc and some minor changes
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 533
diff changeset
729 /**
c9d88af06d35 Javadoc and some minor changes
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 533
diff changeset
730 * For internal use
626
06d69b82d409 Resolve javadoc error and warnings :
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 616
diff changeset
731 *
06d69b82d409 Resolve javadoc error and warnings :
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 616
diff changeset
732 * @param b to enable/disable insert 'fake' newline and prompt
06d69b82d409 Resolve javadoc error and warnings :
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 616
diff changeset
733 * @return previous setting
535
c9d88af06d35 Javadoc and some minor changes
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 533
diff changeset
734 */
511
bd860e850fe1 rename insertFakeFlushes to insertFakePrompts
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 509
diff changeset
735 public boolean setInsertFakePrompts(boolean b) {
bd860e850fe1 rename insertFakeFlushes to insertFakePrompts
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 509
diff changeset
736 return fromMonet.setInsertFakePrompts(b);
501
eaad79c3235f Make the fake prompts at block ends optional
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 498
diff changeset
737 }
eaad79c3235f Make the fake prompts at block ends optional
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 498
diff changeset
738
576
095e896f9d7a Updated comments. Improved code. Added final keywords
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 573
diff changeset
739
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
740 /**
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
741 * Inner class that is used to write data on a normal stream as a
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
742 * blocked stream. A call to the flush() method will write a
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
743 * "final" block to the underlying stream. Non-final blocks are
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
744 * written as soon as one or more bytes would not fit in the
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
745 * current block any more. This allows to write to a block to it's
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
746 * full size, and then flush it explicitly to have a final block
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
747 * being written to the stream.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
748 */
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
749 final class BlockOutputStream extends FilterOutputStream {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
750 private int writePos = 0;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
751 private int blocksize = 0;
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
752 private final byte[] block = new byte[BLOCK];
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
753
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
754 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
755 * Constructs this BlockOutputStream, backed by the given
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
756 * OutputStream. A BufferedOutputStream is internally used.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
757 */
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
758 public BlockOutputStream(final OutputStream out) {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
759 // always use a buffered stream, even though we know how
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
760 // much bytes to write/read, since this is just faster for
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
761 // some reason
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
762 super(new BufferedOutputStream(out));
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
763 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
764
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
765 @Override
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
766 public void flush() throws IOException {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
767 // write the block (as final) then flush.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
768 writeBlock(true);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
769 out.flush();
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
770
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
771 // it's a bit nasty if an exception is thrown from the log,
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
772 // but ignoring it can be nasty as well, so it is decided to
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
773 // let it go so there is feedback about something going wrong
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
774 // it's a bit nasty if an exception is thrown from the log,
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
775 // but ignoring it can be nasty as well, so it is decided to
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
776 // let it go so there is feedback about something going wrong
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
777 if (debug) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
778 log.flush();
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
779 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
780 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
781
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
782 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
783 * writeBlock puts the data in the block on the stream. The
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
784 * boolean last controls whether the block is sent with an
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
785 * indicator to note it is the last block of a sequence or not.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
786 *
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
787 * @param last whether this is the last block
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
788 * @throws IOException if writing to the stream failed
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
789 */
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
790 public void writeBlock(final boolean last) throws IOException {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
791 if (last) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
792 // always fits, because of BLOCK's size
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
793 blocksize = (short)writePos;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
794 // this is the last block, so encode least
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
795 // significant bit in the first byte (little-endian)
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
796 blklen[0] = (byte)(blocksize << 1 & 0xFF | 1);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
797 blklen[1] = (byte)(blocksize >> 7);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
798 } else {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
799 // always fits, because of BLOCK's size
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
800 blocksize = (short)BLOCK;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
801 // another block will follow, encode least
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
802 // significant bit in the first byte (little-endian)
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
803 blklen[0] = (byte)(blocksize << 1 & 0xFF);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
804 blklen[1] = (byte)(blocksize >> 7);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
805 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
806
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
807 out.write(blklen);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
808 // write the actual block
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
809 out.write(block, 0, writePos);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
810
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
811 if (debug) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
812 if (last) {
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
813 log("TD ", "write final block: " + writePos + " bytes", false);
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
814 } else {
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
815 log("TD ", "write block: " + writePos + " bytes", false);
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
816 }
555
90c346571eb4 Use StandardCharsets.UTF_8 where possible
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 554
diff changeset
817 log("TX ", new String(block, 0, writePos, StandardCharsets.UTF_8), true);
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
818 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
819
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
820 writePos = 0;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
821 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
822
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
823 @Override
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
824 public void write(final int b) throws IOException {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
825 if (writePos == BLOCK) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
826 writeBlock(false);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
827 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
828 block[writePos++] = (byte)b;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
829 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
830
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
831 @Override
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
832 public void write(final byte[] b) throws IOException {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
833 write(b, 0, b.length);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
834 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
835
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
836 @Override
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
837 public void write(final byte[] b, int off, int len) throws IOException {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
838 while (len > 0) {
556
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
839 int t = BLOCK - writePos;
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
840 if (len > t) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
841 System.arraycopy(b, off, block, writePos, t);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
842 off += t;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
843 len -= t;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
844 writePos += t;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
845 writeBlock(false);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
846 } else {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
847 System.arraycopy(b, off, block, writePos, len);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
848 writePos += len;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
849 break;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
850 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
851 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
852 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
853
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
854 @Override
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
855 public void close() throws IOException {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
856 // we don't want the flush() method to be called (default of
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
857 // the FilterOutputStream), so we close manually here
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
858 out.close();
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
859 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
860 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
861
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
862
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
863 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
864 * Inner class that is used to make the data on the blocked stream
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
865 * available as a normal stream.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
866 */
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
867 final class BlockInputStream extends FilterInputStream {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
868 private int readPos = 0;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
869 private int blockLen = 0;
508
7730d65dfd55 Add MapiSocket.BlockInputStream.Raw
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 507
diff changeset
870 private boolean wasEndBlock = false;
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
871 private final byte[] block = new byte[BLOCK + 3]; // \n.\n
511
bd860e850fe1 rename insertFakeFlushes to insertFakePrompts
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 509
diff changeset
872 private boolean insertFakePrompts = true;
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
873
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
874 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
875 * Constructs this BlockInputStream, backed by the given
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
876 * InputStream. A BufferedInputStream is internally used.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
877 */
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
878 public BlockInputStream(final InputStream in) {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
879 // always use a buffered stream, even though we know how
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
880 // much bytes to write/read, since this is just faster for
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
881 // some reason
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
882 super(new BufferedInputStream(in));
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
883 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
884
511
bd860e850fe1 rename insertFakeFlushes to insertFakePrompts
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 509
diff changeset
885 public boolean setInsertFakePrompts(boolean doFake) {
bd860e850fe1 rename insertFakeFlushes to insertFakePrompts
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 509
diff changeset
886 boolean old = insertFakePrompts;
bd860e850fe1 rename insertFakeFlushes to insertFakePrompts
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 509
diff changeset
887 insertFakePrompts = doFake;
501
eaad79c3235f Make the fake prompts at block ends optional
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 498
diff changeset
888 return old;
eaad79c3235f Make the fake prompts at block ends optional
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 498
diff changeset
889 }
eaad79c3235f Make the fake prompts at block ends optional
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 498
diff changeset
890
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
891 @Override
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
892 public int available() {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
893 return blockLen - readPos;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
894 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
895
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
896 @Override
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
897 public boolean markSupported() {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
898 return false;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
899 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
900
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
901 @Override
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
902 public void mark(final int readlimit) {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
903 throw new AssertionError("Not implemented!");
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
904 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
905
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
906 @Override
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
907 public void reset() {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
908 throw new AssertionError("Not implemented!");
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
909 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
910
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
911 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
912 * Small wrapper to get a blocking variant of the read() method
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
913 * on the BufferedInputStream. We want to benefit from the
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
914 * Buffered pre-fetching, but not dealing with half blocks.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
915 * Changing this class to be able to use the partially received
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
916 * data will greatly complicate matters, while a performance
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
917 * improvement is debatable given the relatively small size of
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
918 * our blocks. Maybe it does speed up on slower links, then
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
919 * consider this method a quick bug fix/workaround.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
920 *
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
921 * @return false if reading the block failed due to EOF
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
922 */
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
923 private boolean _read(final byte[] b, int len) throws IOException {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
924 int s;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
925 int off = 0;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
926 while (len > 0) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
927 s = in.read(b, off, len);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
928 if (s == -1) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
929 // if we have read something before, we should have been
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
930 // able to read the whole, so make this fatal
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
931 if (off > 0) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
932 if (debug) {
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
933 log("RD ", "the following incomplete block was received:", false);
555
90c346571eb4 Use StandardCharsets.UTF_8 where possible
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 554
diff changeset
934 log("RX ", new String(b, 0, off, StandardCharsets.UTF_8), true);
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
935 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
936 throw new IOException("Read from " +
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
937 con.getInetAddress().getHostName() + ":" +
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
938 con.getPort() + ": Incomplete block read from stream");
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
939 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
940 if (debug)
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
941 log("RD ", "server closed the connection (EOF)", true);
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
942 return false;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
943 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
944 len -= s;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
945 off += s;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
946 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
947
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
948 return true;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
949 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
950
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
951 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
952 * Reads the next block on the stream into the internal buffer,
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
953 * or writes the prompt in the buffer.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
954 *
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
955 * The blocked stream protocol consists of first a two byte
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
956 * integer indicating the length of the block, then the
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
957 * block, followed by another length + block. The end of
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
958 * such sequence is put in the last bit of the length, and
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
959 * hence this length should be shifted to the right to
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
960 * obtain the real length value first. We simply fetch
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
961 * blocks here as soon as they are needed for the stream's
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
962 * read methods.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
963 *
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
964 * The user-flush, which is an implicit effect of the end of
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
965 * a block sequence, is communicated beyond the stream by
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
966 * inserting a prompt sequence on the stream after the last
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
967 * block. This method makes sure that a final block ends with a
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
968 * newline, if it doesn't already, in order to facilitate a
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
969 * Reader that is possibly chained to this InputStream.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
970 *
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
971 * If the stream is not positioned correctly, hell will break
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
972 * loose.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
973 */
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
974 private int readBlock() throws IOException {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
975 // read next two bytes (short)
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
976 if (!_read(blklen, 2))
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
977 return(-1);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
978
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
979 // Get the short-value and store its value in blockLen.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
980 blockLen = (short)(
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
981 (blklen[0] & 0xFF) >> 1 |
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
982 (blklen[1] & 0xFF) << 7
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
983 );
508
7730d65dfd55 Add MapiSocket.BlockInputStream.Raw
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 507
diff changeset
984 wasEndBlock = (blklen[0] & 0x1) == 1;
7730d65dfd55 Add MapiSocket.BlockInputStream.Raw
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 507
diff changeset
985
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
986 readPos = 0;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
987
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
988 if (debug) {
508
7730d65dfd55 Add MapiSocket.BlockInputStream.Raw
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 507
diff changeset
989 if (wasEndBlock) {
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
990 log("RD ", "read final block: " + blockLen + " bytes", false);
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
991 } else {
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
992 log("RD ", "read new block: " + blockLen + " bytes", false);
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
993 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
994 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
995
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
996 // sanity check to avoid bad servers make us do an ugly
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
997 // stack trace
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
998 if (blockLen > block.length)
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
999 throw new IOException("Server sent a block larger than BLOCKsize: " +
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1000 blockLen + " > " + block.length);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1001 if (!_read(block, blockLen))
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
1002 return -1;
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1003
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1004 if (debug)
555
90c346571eb4 Use StandardCharsets.UTF_8 where possible
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 554
diff changeset
1005 log("RX ", new String(block, 0, blockLen, StandardCharsets.UTF_8), true);
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1006
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
1007 // if this is the last block, make it end with a newline and prompt
508
7730d65dfd55 Add MapiSocket.BlockInputStream.Raw
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 507
diff changeset
1008 if (wasEndBlock) {
537
242debd7e866 Fix mistake in comment
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 535
diff changeset
1009 // insert 'fake' newline and prompt
511
bd860e850fe1 rename insertFakeFlushes to insertFakePrompts
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 509
diff changeset
1010 if (insertFakePrompts) {
507
1db3398b78f7 If we don't want a fake prompt we probably also don't want a fake newline
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 502
diff changeset
1011 if (blockLen > 0 && block[blockLen - 1] != '\n') {
1db3398b78f7 If we don't want a fake prompt we probably also don't want a fake newline
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 502
diff changeset
1012 // to terminate the block in a Reader
1db3398b78f7 If we don't want a fake prompt we probably also don't want a fake newline
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 502
diff changeset
1013 block[blockLen++] = '\n';
1db3398b78f7 If we don't want a fake prompt we probably also don't want a fake newline
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 502
diff changeset
1014 }
501
eaad79c3235f Make the fake prompts at block ends optional
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 498
diff changeset
1015 for (byte b : LineType.PROMPT.bytes()) {
eaad79c3235f Make the fake prompts at block ends optional
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 498
diff changeset
1016 block[blockLen++] = b;
eaad79c3235f Make the fake prompts at block ends optional
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 498
diff changeset
1017 }
eaad79c3235f Make the fake prompts at block ends optional
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 498
diff changeset
1018 block[blockLen++] = '\n';
511
bd860e850fe1 rename insertFakeFlushes to insertFakePrompts
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 509
diff changeset
1019 if (debug) {
bd860e850fe1 rename insertFakeFlushes to insertFakePrompts
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 509
diff changeset
1020 log("RD ", "inserting prompt", true);
bd860e850fe1 rename insertFakeFlushes to insertFakePrompts
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 509
diff changeset
1021 }
498
4cfe4991ee63 Use the prompt characters from mapi_prompt.h
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 497
diff changeset
1022 }
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1023 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1024
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
1025 return blockLen;
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1026 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1027
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1028 @Override
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1029 public int read() throws IOException {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1030 if (available() == 0) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1031 if (readBlock() == -1)
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
1032 return -1;
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1033 }
75
db46b4ab2b9f Correct the condition for when the log can be closed: only when log instanceof FileWriter
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 53
diff changeset
1034
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1035 if (debug)
555
90c346571eb4 Use StandardCharsets.UTF_8 where possible
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 554
diff changeset
1036 log("RX ", new String(block, readPos, 1, StandardCharsets.UTF_8), true);
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
1037
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1038 return (int)block[readPos++];
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1039 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1040
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1041 @Override
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
1042 public int read(final byte[] b) throws IOException {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1043 return read(b, 0, b.length);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1044 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1045
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1046 @Override
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
1047 public int read(final byte[] b, int off, int len) throws IOException {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1048 int t;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1049 int size = 0;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1050 while (size < len) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1051 t = available();
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1052 if (t == 0) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1053 if (size != 0)
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1054 break;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1055 if (readBlock() == -1) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1056 if (size == 0)
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1057 size = -1;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1058 break;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1059 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1060 t = available();
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1061 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1062 if (len > t) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1063 System.arraycopy(block, readPos, b, off, t);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1064 off += t;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1065 len -= t;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1066 readPos += t;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1067 size += t;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1068 } else {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1069 System.arraycopy(block, readPos, b, off, len);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1070 readPos += len;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1071 size += len;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1072 break;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1073 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1074 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1075 return size;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1076 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1077
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1078 @Override
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 281
diff changeset
1079 public long skip(final long n) throws IOException {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1080 long skip = n;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1081 while (skip > 0) {
556
87feb93330a6 Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 555
diff changeset
1082 int t = available();
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1083 if (skip > t) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1084 skip -= t;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1085 readPos += t;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1086 readBlock();
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1087 } else {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1088 readPos += skip;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1089 break;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1090 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1091 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1092 return n;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1093 }
508
7730d65dfd55 Add MapiSocket.BlockInputStream.Raw
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 507
diff changeset
1094
535
c9d88af06d35 Javadoc and some minor changes
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 533
diff changeset
1095 /**
c9d88af06d35 Javadoc and some minor changes
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 533
diff changeset
1096 * For internal use
c9d88af06d35 Javadoc and some minor changes
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 533
diff changeset
1097 */
508
7730d65dfd55 Add MapiSocket.BlockInputStream.Raw
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 507
diff changeset
1098 Raw getRaw() {
7730d65dfd55 Add MapiSocket.BlockInputStream.Raw
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 507
diff changeset
1099 return new Raw();
7730d65dfd55 Add MapiSocket.BlockInputStream.Raw
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 507
diff changeset
1100 }
7730d65dfd55 Add MapiSocket.BlockInputStream.Raw
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 507
diff changeset
1101
7730d65dfd55 Add MapiSocket.BlockInputStream.Raw
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 507
diff changeset
1102 /** An alternative I/O interface that exposes the block based nature of the MAPI protocol */
7730d65dfd55 Add MapiSocket.BlockInputStream.Raw
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 507
diff changeset
1103 final class Raw {
7730d65dfd55 Add MapiSocket.BlockInputStream.Raw
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 507
diff changeset
1104 byte[] getBytes() {
7730d65dfd55 Add MapiSocket.BlockInputStream.Raw
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 507
diff changeset
1105 return block;
7730d65dfd55 Add MapiSocket.BlockInputStream.Raw
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 507
diff changeset
1106 }
7730d65dfd55 Add MapiSocket.BlockInputStream.Raw
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 507
diff changeset
1107
7730d65dfd55 Add MapiSocket.BlockInputStream.Raw
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 507
diff changeset
1108 int getLength() {
7730d65dfd55 Add MapiSocket.BlockInputStream.Raw
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 507
diff changeset
1109 return blockLen;
7730d65dfd55 Add MapiSocket.BlockInputStream.Raw
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 507
diff changeset
1110 }
7730d65dfd55 Add MapiSocket.BlockInputStream.Raw
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 507
diff changeset
1111
7730d65dfd55 Add MapiSocket.BlockInputStream.Raw
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 507
diff changeset
1112 int getPosition() {
7730d65dfd55 Add MapiSocket.BlockInputStream.Raw
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 507
diff changeset
1113 return readPos;
7730d65dfd55 Add MapiSocket.BlockInputStream.Raw
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 507
diff changeset
1114 }
7730d65dfd55 Add MapiSocket.BlockInputStream.Raw
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 507
diff changeset
1115
7730d65dfd55 Add MapiSocket.BlockInputStream.Raw
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 507
diff changeset
1116 int consume(int delta) {
7730d65dfd55 Add MapiSocket.BlockInputStream.Raw
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 507
diff changeset
1117 int pos = readPos;
7730d65dfd55 Add MapiSocket.BlockInputStream.Raw
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 507
diff changeset
1118 readPos += delta;
7730d65dfd55 Add MapiSocket.BlockInputStream.Raw
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 507
diff changeset
1119 return pos;
7730d65dfd55 Add MapiSocket.BlockInputStream.Raw
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 507
diff changeset
1120 }
7730d65dfd55 Add MapiSocket.BlockInputStream.Raw
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 507
diff changeset
1121
7730d65dfd55 Add MapiSocket.BlockInputStream.Raw
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 507
diff changeset
1122 int readBlock() throws IOException {
511
bd860e850fe1 rename insertFakeFlushes to insertFakePrompts
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 509
diff changeset
1123 boolean wasFaking = setInsertFakePrompts(false);
509
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1124 try {
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1125 return BlockInputStream.this.readBlock();
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1126 } finally {
511
bd860e850fe1 rename insertFakeFlushes to insertFakePrompts
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 509
diff changeset
1127 setInsertFakePrompts(wasFaking);
509
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1128 }
508
7730d65dfd55 Add MapiSocket.BlockInputStream.Raw
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 507
diff changeset
1129 }
7730d65dfd55 Add MapiSocket.BlockInputStream.Raw
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 507
diff changeset
1130
7730d65dfd55 Add MapiSocket.BlockInputStream.Raw
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 507
diff changeset
1131 boolean wasEndBlock() {
7730d65dfd55 Add MapiSocket.BlockInputStream.Raw
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 507
diff changeset
1132 return wasEndBlock;
7730d65dfd55 Add MapiSocket.BlockInputStream.Raw
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 507
diff changeset
1133 }
7730d65dfd55 Add MapiSocket.BlockInputStream.Raw
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 507
diff changeset
1134 }
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1135 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1136
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1137 /**
281
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
1138 * Closes the streams and socket connected to the server if possible.
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
1139 * If an error occurs at closing a resource, it is ignored so as many
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
1140 * resources as possible are closed.
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1141 */
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1142 public synchronized void close() {
53
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
1143 if (writer != null) {
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
1144 try {
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
1145 writer.close();
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
1146 writer = null;
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
1147 } catch (IOException e) { /* ignore it */ }
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
1148 }
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
1149 if (reader != null) {
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
1150 try {
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
1151 reader.close();
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
1152 reader = null;
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
1153 } catch (IOException e) { /* ignore it */ }
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
1154 }
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
1155 if (toMonet != null) {
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
1156 try {
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
1157 toMonet.close();
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
1158 toMonet = null;
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
1159 } catch (IOException e) { /* ignore it */ }
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
1160 }
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
1161 if (fromMonet != null) {
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
1162 try {
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
1163 fromMonet.close();
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
1164 fromMonet = null;
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
1165 } catch (IOException e) { /* ignore it */ }
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
1166 }
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
1167 if (con != null) {
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
1168 try {
281
b58c1b245ede Correct and improve implementation of getChallengeResponse() for protocol 9
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 276
diff changeset
1169 con.close(); // close the socket
53
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
1170 con = null;
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
1171 } catch (IOException e) { /* ignore it */ }
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
1172 }
75
db46b4ab2b9f Correct the condition for when the log can be closed: only when log instanceof FileWriter
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 53
diff changeset
1173 if (debug && log != null && log instanceof FileWriter) {
53
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
1174 try {
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
1175 log.close();
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
1176 log = null;
6cc63d6cb224 Implemented fix as suggested in Bug 3973
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
1177 } catch (IOException e) { /* ignore it */ }
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1178 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1179 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1180
535
c9d88af06d35 Javadoc and some minor changes
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 533
diff changeset
1181 /**
c9d88af06d35 Javadoc and some minor changes
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 533
diff changeset
1182 * Return an UploadStream for use with for example COPY FROM filename ON CLIENT.
c9d88af06d35 Javadoc and some minor changes
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 533
diff changeset
1183 *
552
7b320303b579 Doc comment improvements
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 547
diff changeset
1184 * Building block for {@link org.monetdb.jdbc.MonetConnection.UploadHandler}.
626
06d69b82d409 Resolve javadoc error and warnings :
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 616
diff changeset
1185 *
535
c9d88af06d35 Javadoc and some minor changes
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 533
diff changeset
1186 * @param chunkSize chunk size for the upload stream
626
06d69b82d409 Resolve javadoc error and warnings :
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 616
diff changeset
1187 * @return UploadStream new upload stream with the given chunk size
535
c9d88af06d35 Javadoc and some minor changes
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 533
diff changeset
1188 */
521
72007c4f8f8a Allow MonetUploadHandler to configure the chunk size
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 512
diff changeset
1189 public UploadStream uploadStream(int chunkSize) {
72007c4f8f8a Allow MonetUploadHandler to configure the chunk size
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 512
diff changeset
1190 return new UploadStream(chunkSize);
72007c4f8f8a Allow MonetUploadHandler to configure the chunk size
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 512
diff changeset
1191 }
502
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1192
535
c9d88af06d35 Javadoc and some minor changes
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 533
diff changeset
1193 /**
c9d88af06d35 Javadoc and some minor changes
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 533
diff changeset
1194 * Return an UploadStream for use with for example COPY FROM filename ON CLIENT.
c9d88af06d35 Javadoc and some minor changes
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 533
diff changeset
1195 *
552
7b320303b579 Doc comment improvements
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 547
diff changeset
1196 * Building block for {@link org.monetdb.jdbc.MonetConnection.UploadHandler}.
626
06d69b82d409 Resolve javadoc error and warnings :
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 616
diff changeset
1197 *
06d69b82d409 Resolve javadoc error and warnings :
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 616
diff changeset
1198 * @return UploadStream new upload stream
535
c9d88af06d35 Javadoc and some minor changes
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 533
diff changeset
1199 */
502
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1200 public UploadStream uploadStream() {
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1201 return new UploadStream();
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1202 }
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1203
535
c9d88af06d35 Javadoc and some minor changes
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 533
diff changeset
1204 /**
c9d88af06d35 Javadoc and some minor changes
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 533
diff changeset
1205 * Return a DownloadStream for use with for example COPY INTO filename ON CLIENT
c9d88af06d35 Javadoc and some minor changes
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 533
diff changeset
1206 *
552
7b320303b579 Doc comment improvements
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 547
diff changeset
1207 * Building block for {@link org.monetdb.jdbc.MonetConnection.DownloadHandler}.
626
06d69b82d409 Resolve javadoc error and warnings :
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 616
diff changeset
1208 *
616
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1209 * @param prependCr convert \n to \r\n
626
06d69b82d409 Resolve javadoc error and warnings :
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 616
diff changeset
1210 * @return DownloadStream new download stream
535
c9d88af06d35 Javadoc and some minor changes
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 533
diff changeset
1211 */
616
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1212 public DownloadStream downloadStream(boolean prependCr) {
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1213 return new DownloadStream(fromMonet.getRaw(), toMonet, prependCr);
509
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1214 }
502
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1215
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1216 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1217 * Destructor called by garbage collector before destroying this
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1218 * object tries to disconnect the MonetDB connection if it has not
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1219 * been disconnected already.
368
af6db116238d Add javadoc url where the supported hash algorithms are described.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 363
diff changeset
1220 *
301
59bc8bebbfe9 Resolve javac -Xlint warnings when compiled on Java 9 or higher
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 297
diff changeset
1221 * @deprecated (since="9")
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1222 */
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1223 @Override
301
59bc8bebbfe9 Resolve javac -Xlint warnings when compiled on Java 9 or higher
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 297
diff changeset
1224 @Deprecated
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1225 protected void finalize() throws Throwable {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1226 close();
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1227 super.finalize();
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1228 }
502
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1229
576
095e896f9d7a Updated comments. Improved code. Added final keywords
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 573
diff changeset
1230
535
c9d88af06d35 Javadoc and some minor changes
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 533
diff changeset
1231 /**
c9d88af06d35 Javadoc and some minor changes
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 533
diff changeset
1232 * Stream of data sent to the server
576
095e896f9d7a Updated comments. Improved code. Added final keywords
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 573
diff changeset
1233 *
095e896f9d7a Updated comments. Improved code. Added final keywords
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 573
diff changeset
1234 * Building block for {@link org.monetdb.jdbc.MonetConnection.UploadHandler}.
535
c9d88af06d35 Javadoc and some minor changes
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 533
diff changeset
1235 *
c9d88af06d35 Javadoc and some minor changes
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 533
diff changeset
1236 * An UploadStream has a chunk size. Every chunk size bytes, the server gets
c9d88af06d35 Javadoc and some minor changes
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 533
diff changeset
1237 * the opportunity to abort the upload.
c9d88af06d35 Javadoc and some minor changes
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 533
diff changeset
1238 */
502
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1239 public class UploadStream extends FilterOutputStream {
521
72007c4f8f8a Allow MonetUploadHandler to configure the chunk size
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 512
diff changeset
1240 public final static int DEFAULT_CHUNK_SIZE = 1024 * 1024;
72007c4f8f8a Allow MonetUploadHandler to configure the chunk size
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 512
diff changeset
1241 private final int chunkSize;
502
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1242 private boolean closed = false;
533
b75464874130 Keep better track of whether the server has cancelled the upload
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 521
diff changeset
1243 private boolean serverCancelled = false;
521
72007c4f8f8a Allow MonetUploadHandler to configure the chunk size
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 512
diff changeset
1244 private int chunkLeft;
502
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1245 private byte[] promptBuffer;
557
ce2b616ed22e Add a cancellation callback to UploadHandler
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
1246 private Runnable cancellationCallback = null;
502
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1247
535
c9d88af06d35 Javadoc and some minor changes
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 533
diff changeset
1248 /** Create an UploadStream with the given chunk size */
576
095e896f9d7a Updated comments. Improved code. Added final keywords
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 573
diff changeset
1249 UploadStream(final int chunkSize) {
502
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1250 super(toMonet);
521
72007c4f8f8a Allow MonetUploadHandler to configure the chunk size
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 512
diff changeset
1251 if (chunkSize <= 0) {
72007c4f8f8a Allow MonetUploadHandler to configure the chunk size
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 512
diff changeset
1252 throw new IllegalArgumentException("chunk size must be positive");
72007c4f8f8a Allow MonetUploadHandler to configure the chunk size
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 512
diff changeset
1253 }
72007c4f8f8a Allow MonetUploadHandler to configure the chunk size
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 512
diff changeset
1254 this.chunkSize = chunkSize;
502
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1255 assert LineType.MORE.bytes().length == LineType.FILETRANSFER.bytes().length;
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1256 int promptLen = LineType.MORE.bytes().length;
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1257 promptBuffer = new byte[promptLen + 1];
521
72007c4f8f8a Allow MonetUploadHandler to configure the chunk size
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 512
diff changeset
1258 chunkLeft = this.chunkSize;
72007c4f8f8a Allow MonetUploadHandler to configure the chunk size
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 512
diff changeset
1259 }
72007c4f8f8a Allow MonetUploadHandler to configure the chunk size
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 512
diff changeset
1260
535
c9d88af06d35 Javadoc and some minor changes
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 533
diff changeset
1261 /** Create an UploadStream with the default chunk size */
521
72007c4f8f8a Allow MonetUploadHandler to configure the chunk size
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 512
diff changeset
1262 UploadStream() {
72007c4f8f8a Allow MonetUploadHandler to configure the chunk size
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 512
diff changeset
1263 this(DEFAULT_CHUNK_SIZE);
502
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1264 }
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1265
557
ce2b616ed22e Add a cancellation callback to UploadHandler
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
1266 /** Set a callback to be invoked if the server cancels the upload
626
06d69b82d409 Resolve javadoc error and warnings :
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 616
diff changeset
1267 *
06d69b82d409 Resolve javadoc error and warnings :
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 616
diff changeset
1268 * @param cancellationCallback callback to call
557
ce2b616ed22e Add a cancellation callback to UploadHandler
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
1269 */
576
095e896f9d7a Updated comments. Improved code. Added final keywords
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 573
diff changeset
1270 public void setCancellationCallback(final Runnable cancellationCallback) {
557
ce2b616ed22e Add a cancellation callback to UploadHandler
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
1271 this.cancellationCallback = cancellationCallback;
ce2b616ed22e Add a cancellation callback to UploadHandler
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
1272 }
ce2b616ed22e Add a cancellation callback to UploadHandler
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
1273
502
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1274 @Override
576
095e896f9d7a Updated comments. Improved code. Added final keywords
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 573
diff changeset
1275 public void write(final int b) throws IOException {
533
b75464874130 Keep better track of whether the server has cancelled the upload
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 521
diff changeset
1276 if (serverCancelled) {
b75464874130 Keep better track of whether the server has cancelled the upload
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 521
diff changeset
1277 // We have already thrown an exception and apparently that has been ignored.
b75464874130 Keep better track of whether the server has cancelled the upload
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 521
diff changeset
1278 // Probably because they're calling print methods instead of write.
b75464874130 Keep better track of whether the server has cancelled the upload
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 521
diff changeset
1279 // Throw another one, maybe they'll catch this one.
b75464874130 Keep better track of whether the server has cancelled the upload
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 521
diff changeset
1280 throw new IOException("Server aborted the upload");
b75464874130 Keep better track of whether the server has cancelled the upload
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 521
diff changeset
1281 }
502
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1282 handleChunking();
612
1d44b8a577ca write to out, not to super when implementing FilterOutputStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 610
diff changeset
1283 out.write(b);
502
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1284 wrote(1);
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1285 }
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1286
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1287 @Override
576
095e896f9d7a Updated comments. Improved code. Added final keywords
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 573
diff changeset
1288 public void write(final byte[] b) throws IOException {
612
1d44b8a577ca write to out, not to super when implementing FilterOutputStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 610
diff changeset
1289 this.write(b, 0, b.length);
502
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1290 }
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1291
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1292 @Override
576
095e896f9d7a Updated comments. Improved code. Added final keywords
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 573
diff changeset
1293 public void write(final byte[] b, int off, int len) throws IOException {
533
b75464874130 Keep better track of whether the server has cancelled the upload
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 521
diff changeset
1294 if (serverCancelled) {
b75464874130 Keep better track of whether the server has cancelled the upload
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 521
diff changeset
1295 // We have already thrown an exception and apparently that has been ignored.
b75464874130 Keep better track of whether the server has cancelled the upload
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 521
diff changeset
1296 // Probably because they're calling print methods instead of write.
b75464874130 Keep better track of whether the server has cancelled the upload
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 521
diff changeset
1297 // Throw another one, maybe they'll catch this one.
b75464874130 Keep better track of whether the server has cancelled the upload
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 521
diff changeset
1298 throw new IOException("Server aborted the upload");
b75464874130 Keep better track of whether the server has cancelled the upload
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 521
diff changeset
1299 }
502
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1300 while (len > 0) {
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1301 handleChunking();
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1302 int toWrite = Integer.min(len, chunkLeft);
612
1d44b8a577ca write to out, not to super when implementing FilterOutputStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 610
diff changeset
1303 out.write(b, off, toWrite);
502
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1304 off += toWrite;
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1305 len -= toWrite;
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1306 wrote(toWrite);
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1307 }
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1308 }
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1309
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1310 @Override
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1311 public void flush() throws IOException {
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1312 // suppress flushes
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1313 }
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1314
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1315 @Override
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1316 public void close() throws IOException {
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1317 if (closed) {
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1318 return;
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1319 }
533
b75464874130 Keep better track of whether the server has cancelled the upload
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 521
diff changeset
1320 closed = true;
b75464874130 Keep better track of whether the server has cancelled the upload
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 521
diff changeset
1321
b75464874130 Keep better track of whether the server has cancelled the upload
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 521
diff changeset
1322 if (serverCancelled)
b75464874130 Keep better track of whether the server has cancelled the upload
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 521
diff changeset
1323 closeAfterServerCancelled();
b75464874130 Keep better track of whether the server has cancelled the upload
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 521
diff changeset
1324 else
b75464874130 Keep better track of whether the server has cancelled the upload
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 521
diff changeset
1325 closeAfterSuccesfulUpload();
b75464874130 Keep better track of whether the server has cancelled the upload
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 521
diff changeset
1326 }
b75464874130 Keep better track of whether the server has cancelled the upload
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 521
diff changeset
1327
b75464874130 Keep better track of whether the server has cancelled the upload
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 521
diff changeset
1328 private void closeAfterSuccesfulUpload() throws IOException {
521
72007c4f8f8a Allow MonetUploadHandler to configure the chunk size
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 512
diff changeset
1329 if (chunkLeft != chunkSize) {
502
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1330 // flush pending data
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1331 flushAndReadPrompt();
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1332 }
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1333 // send empty block
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1334 out.flush();
576
095e896f9d7a Updated comments. Improved code. Added final keywords
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 573
diff changeset
1335 final LineType acknowledgement = readPrompt();
502
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1336 if (acknowledgement != LineType.FILETRANSFER) {
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1337 throw new IOException("Expected server to acknowledge end of file");
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1338 }
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1339 }
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1340
576
095e896f9d7a Updated comments. Improved code. Added final keywords
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 573
diff changeset
1341 private void closeAfterServerCancelled() {
533
b75464874130 Keep better track of whether the server has cancelled the upload
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 521
diff changeset
1342 // nothing to do here, we have already read the error prompt.
b75464874130 Keep better track of whether the server has cancelled the upload
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 521
diff changeset
1343 }
b75464874130 Keep better track of whether the server has cancelled the upload
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 521
diff changeset
1344
576
095e896f9d7a Updated comments. Improved code. Added final keywords
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 573
diff changeset
1345 private void wrote(final int i) {
502
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1346 chunkLeft -= i;
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1347 }
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1348
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1349 private void handleChunking() throws IOException {
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1350 if (chunkLeft > 0) {
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1351 return;
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1352 }
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1353 flushAndReadPrompt();
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1354 }
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1355
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1356 private void flushAndReadPrompt() throws IOException {
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1357 out.flush();
521
72007c4f8f8a Allow MonetUploadHandler to configure the chunk size
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 512
diff changeset
1358 chunkLeft = chunkSize;
576
095e896f9d7a Updated comments. Improved code. Added final keywords
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 573
diff changeset
1359 final LineType lineType = readPrompt();
502
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1360 switch (lineType) {
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1361 case MORE:
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1362 return;
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1363 case FILETRANSFER:
533
b75464874130 Keep better track of whether the server has cancelled the upload
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 521
diff changeset
1364 // Note, if the caller is calling print methods instead of write, the IO exception gets hidden.
b75464874130 Keep better track of whether the server has cancelled the upload
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 521
diff changeset
1365 // This is unfortunate but there's nothing we can do about it.
b75464874130 Keep better track of whether the server has cancelled the upload
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 521
diff changeset
1366 serverCancelled = true;
557
ce2b616ed22e Add a cancellation callback to UploadHandler
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
1367 if (cancellationCallback != null) {
ce2b616ed22e Add a cancellation callback to UploadHandler
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
1368 cancellationCallback.run();
ce2b616ed22e Add a cancellation callback to UploadHandler
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
1369 }
502
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1370 throw new IOException("Server aborted the upload");
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1371 default:
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1372 throw new IOException("Expected MORE/DONE from server, got " + lineType);
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1373 }
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1374 }
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1375
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1376 private LineType readPrompt() throws IOException {
576
095e896f9d7a Updated comments. Improved code. Added final keywords
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 573
diff changeset
1377 final int nread = fromMonet.read(promptBuffer);
502
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1378 if (nread != promptBuffer.length || promptBuffer[promptBuffer.length - 1] != '\n') {
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1379 throw new IOException("server return incomplete prompt");
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1380 }
576
095e896f9d7a Updated comments. Improved code. Added final keywords
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 573
diff changeset
1381 return LineType.classify(promptBuffer);
502
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1382 }
83354bd21320 Upload fake data when an upload request is received
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 501
diff changeset
1383 }
509
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1384
576
095e896f9d7a Updated comments. Improved code. Added final keywords
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 573
diff changeset
1385
535
c9d88af06d35 Javadoc and some minor changes
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 533
diff changeset
1386 /**
c9d88af06d35 Javadoc and some minor changes
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 533
diff changeset
1387 * Stream of data received from the server
576
095e896f9d7a Updated comments. Improved code. Added final keywords
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 573
diff changeset
1388 *
095e896f9d7a Updated comments. Improved code. Added final keywords
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 573
diff changeset
1389 * Building block for {@link org.monetdb.jdbc.MonetConnection.DownloadHandler}.
535
c9d88af06d35 Javadoc and some minor changes
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 533
diff changeset
1390 */
509
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1391 public static class DownloadStream extends InputStream {
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1392 private final BlockInputStream.Raw rawIn;
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1393 private final OutputStream out;
616
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1394 private final boolean prependCr;
509
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1395 private boolean endBlockSeen = false;
547
8029d7368b5a Make sure the download stream isn't closed twice
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 541
diff changeset
1396 private boolean closed = false;
616
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1397 private boolean newlinePending = false; // used for crlf conversion
509
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1398
616
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1399 DownloadStream(BlockInputStream.Raw rawIn, OutputStream out, boolean prependCr) {
509
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1400 this.rawIn = rawIn;
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1401 this.out = out;
616
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1402 this.prependCr = prependCr;
509
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1403 }
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1404
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1405 void nextBlock() throws IOException {
547
8029d7368b5a Make sure the download stream isn't closed twice
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 541
diff changeset
1406 if (endBlockSeen || closed)
509
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1407 return;
576
095e896f9d7a Updated comments. Improved code. Added final keywords
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 573
diff changeset
1408 final int ret = rawIn.readBlock();
509
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1409 if (ret < 0 || rawIn.wasEndBlock()) {
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1410 endBlockSeen = true;
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1411 }
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1412 }
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1413
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1414 @Override
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1415 public void close() throws IOException {
547
8029d7368b5a Make sure the download stream isn't closed twice
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 541
diff changeset
1416 if (closed)
8029d7368b5a Make sure the download stream isn't closed twice
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 541
diff changeset
1417 return;
8029d7368b5a Make sure the download stream isn't closed twice
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 541
diff changeset
1418 closed = true;
509
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1419 while (!endBlockSeen) {
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1420 nextBlock();
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1421 }
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1422 // Send acknowledgement to server
512
a4000e374a43 Fix silly mistake
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 511
diff changeset
1423 out.write('\n');
509
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1424 out.flush();
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1425 // Do whatever super has to do
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1426 super.close();
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1427 }
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1428
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1429 @Override
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1430 public int read() throws IOException {
576
095e896f9d7a Updated comments. Improved code. Added final keywords
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 573
diff changeset
1431 final byte[] buf = { 0 };
095e896f9d7a Updated comments. Improved code. Added final keywords
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 573
diff changeset
1432 final int nread = read(buf, 0, 1);
509
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1433 if (nread == 1)
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1434 return buf[0];
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1435 else
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1436 return -1;
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1437 }
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1438
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1439 @Override
616
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1440 public int read(final byte[] dest, int off, int len) throws IOException {
576
095e896f9d7a Updated comments. Improved code. Added final keywords
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 573
diff changeset
1441 final int origOff = off;
616
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1442 int end = off + len;
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1443
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1444 while (off < end) {
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1445 // minimum of what's requested and what we have in stock
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1446 int chunk = Integer.min(end - off, rawIn.getLength() - rawIn.getPosition());
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1447 assert chunk >= 0;
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1448 if (chunk == 0) {
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1449 // make progress by fetching more data
509
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1450 if (endBlockSeen)
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1451 break;
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1452 nextBlock();
616
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1453 continue;
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1454 }
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1455 // make progress copying some bytes
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1456 if (!prependCr) {
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1457 // no conversion needed, use arraycopy
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1458 System.arraycopy(rawIn.getBytes(), rawIn.getPosition(), dest, off, chunk);
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1459 off += chunk;
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1460 rawIn.consume(chunk);
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1461 } else {
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1462 int chunkEnd = off + chunk;
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1463 if (newlinePending && off < chunkEnd) {
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1464 // we were in the middle of a line ending conversion
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1465 dest[off++] = '\n';
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1466 newlinePending = false;
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1467 }
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1468 while (off < chunkEnd) {
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1469 byte b = rawIn.getBytes()[rawIn.consume(1)];
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1470 if (b != '\n') {
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1471 dest[off++] = b;
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1472 } else if (chunkEnd - off >= 2) {
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1473 dest[off++] = '\r';
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1474 dest[off++] = '\n';
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1475 } else {
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1476 dest[off++] = '\r';
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1477 newlinePending = true;
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1478 break;
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1479 }
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1480 }
509
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1481 }
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1482 }
616
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1483
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1484 if (off < end && newlinePending) {
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1485 dest[off++] = '\n';
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1486 newlinePending = false;
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1487 }
65641a7cea31 Implement line ending conversion for downloads
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 612
diff changeset
1488
509
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1489 if (off == origOff && endBlockSeen)
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1490 return -1;
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1491 else
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1492 return off - origOff;
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1493 }
455497783026 Add MapiSocket.DownloadStream
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 508
diff changeset
1494 }
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1495 }