-
Notifications
You must be signed in to change notification settings - Fork 426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed a bug where calling length() after obtaining a stream would close the stream for Clobs/NClobs #799
Merged
Merged
Fixed a bug where calling length() after obtaining a stream would close the stream for Clobs/NClobs #799
Changes from 13 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
bb254f2
filling clob if stream is not of type plpinputstream
rene-ye 225c985
fix for simpleinputstream
rene-ye 86a5887
Merge pull request #46 from Microsoft/dev
rene-ye 8f6d197
Added more streaming tests. Changed Clob to use different charset ins…
rene-ye a6ad39a
cleaning up implementation
rene-ye 9619543
removing unused imports
rene-ye 1ebc48c
added length check to tests and updated length() for clobs/nclobs
rene-ye 72b002c
enabled streaming for getAsciiStream
rene-ye 6b7956d
cleaning up ascii stream implementation.
rene-ye 0785932
length() for NClob
rene-ye d272502
reverting exposing private methods as protected methods
rene-ye 0cc0b90
update test
rene-ye 26df3df
upped clob/nclob max length, added test for calling length() after re…
rene-ye 4ffee2e
changes addressing reviews
rene-ye 557b4a1
returning bufferedinputstream instead of inputstream
rene-ye 9a0fd0c
formatting
rene-ye 1844970
normalize nclob getAsciiStream behavior
rene-ye e613240
test fix
rene-ye c692c68
use getCharacterStream for nclobs
rene-ye c3fcc9d
removing redundant code
rene-ye 7ebaa44
removing unused imports
rene-ye 1702730
Merge branch 'dev' of https://github.com/Microsoft/mssql-jdbc into is…
rene-ye df7e79b
changing enum name
rene-ye 9ac2149
refractor while loop
rene-ye 8260ce9
corrected spelling of 'recieved' to 'received'
rene-ye d3179aa
place rs in try-catch
rene-ye 8746b95
free lobs when not needed
rene-ye File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,11 +5,8 @@ | |
|
||
package com.microsoft.sqlserver.jdbc; | ||
|
||
import static java.nio.charset.StandardCharsets.US_ASCII; | ||
import static java.nio.charset.StandardCharsets.UTF_16LE; | ||
|
||
import java.io.BufferedInputStream; | ||
import java.io.BufferedReader; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.Closeable; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
@@ -20,6 +17,7 @@ | |
import java.io.StringReader; | ||
import java.io.UnsupportedEncodingException; | ||
import java.io.Writer; | ||
import java.nio.charset.Charset; | ||
import java.sql.Clob; | ||
import java.sql.SQLException; | ||
import java.text.MessageFormat; | ||
|
@@ -152,7 +150,7 @@ abstract class SQLServerClobBase extends SQLServerLob implements Serializable { | |
|
||
// The value of the CLOB that this Clob object represents. | ||
// This value is never null unless/until the free() method is called. | ||
private String value; | ||
protected String value; | ||
|
||
private final SQLCollation sqlCollation; | ||
|
||
|
@@ -181,8 +179,9 @@ final public String toString() { | |
// Unique id generator for each instance (used for logging). | ||
static private final AtomicInteger baseID = new AtomicInteger(0); | ||
|
||
private Charset defaultCharset = null; | ||
|
||
// Returns unique id for each instance. | ||
|
||
private static int nextInstanceID() { | ||
return baseID.incrementAndGet(); | ||
} | ||
|
@@ -281,9 +280,20 @@ public InputStream getAsciiStream() throws SQLException { | |
if (null != sqlCollation && !sqlCollation.supportsAsciiConversion()) | ||
DataTypes.throwConversionError(getDisplayClassName(), "AsciiStream"); | ||
|
||
getStringFromStream(); | ||
InputStream getterStream = new BufferedInputStream( | ||
new ReaderInputStream(new StringReader(value), US_ASCII, value.length())); | ||
// Need to use a BufferedInputStream since the stream returned by this method is assumed to support mark/reset | ||
InputStream getterStream; | ||
if (null == value && !activeStreams.isEmpty()) { | ||
InputStream inputStream = (InputStream) activeStreams.get(0); | ||
try { | ||
inputStream.reset(); | ||
getterStream = inputStream; | ||
} catch (IOException e) { | ||
throw new SQLServerException(e.getMessage(), null, 0, e); | ||
} | ||
} else { | ||
getStringFromStream(); | ||
getterStream = new ByteArrayInputStream(value.getBytes(java.nio.charset.StandardCharsets.US_ASCII)); | ||
} | ||
activeStreams.add(getterStream); | ||
return getterStream; | ||
} | ||
|
@@ -301,11 +311,18 @@ public Reader getCharacterStream() throws SQLException { | |
Reader getterStream = null; | ||
if (null == value && !activeStreams.isEmpty()) { | ||
InputStream inputStream = (InputStream) activeStreams.get(0); | ||
getterStream = new BufferedReader(new InputStreamReader(inputStream, UTF_16LE)); | ||
try { | ||
inputStream.reset(); | ||
} catch (IOException e) { | ||
throw new SQLServerException(e.getMessage(), null, 0, e); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use "SQLServerException.makeFromDriverError" instead. Also applies to other locations in the changes made. |
||
} | ||
Charset cs = (defaultCharset == null) ? typeInfo.getCharset() : defaultCharset; | ||
getterStream = new BufferedReader(new InputStreamReader(inputStream, cs)); | ||
} else { | ||
getStringFromStream(); | ||
getterStream = new StringReader(value); | ||
activeStreams.add(getterStream); | ||
} | ||
activeStreams.add(getterStream); | ||
return getterStream; | ||
} | ||
|
||
|
@@ -381,9 +398,8 @@ public String getSubString(long pos, int length) throws SQLException { | |
*/ | ||
public long length() throws SQLException { | ||
checkClosed(); | ||
|
||
if (value == null && activeStreams.get(0) instanceof PLPInputStream) { | ||
return (long) ((PLPInputStream) activeStreams.get(0)).payloadLength / 2; | ||
if (value == null && activeStreams.get(0) instanceof BaseInputStream) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. null == value |
||
return (long) ((BaseInputStream) activeStreams.get(0)).payloadLength; | ||
} | ||
return value.length(); | ||
rene-ye marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
@@ -412,7 +428,8 @@ private void getStringFromStream() throws SQLServerException { | |
} catch (IOException e) { | ||
throw new SQLServerException(e.getMessage(), null, 0, e); | ||
} | ||
value = new String((stream).getBytes(), typeInfo.getCharset()); | ||
Charset cs = (defaultCharset == null) ? typeInfo.getCharset() : defaultCharset; | ||
value = new String((stream).getBytes(), cs); | ||
} | ||
} | ||
|
||
|
@@ -661,6 +678,10 @@ public int setString(long pos, String str, int offset, int len) throws SQLExcept | |
|
||
return len; | ||
} | ||
|
||
protected void setDefaultCharset(Charset c) { | ||
this.defaultCharset = c; | ||
} | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we close 'inputStream' here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's the same input stream that the user gets, if we close it, the stream returned to the user gets closed.