Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
189 changes: 136 additions & 53 deletions src/main/java/org/duckdb/DuckDBPreparedStatement.java

Large diffs are not rendered by default.

23 changes: 20 additions & 3 deletions src/main/java/org/duckdb/DuckDBTime.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
package org.duckdb;

import java.sql.Time;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.*;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;

public class DuckDBTime extends DuckDBTimestamp {
public DuckDBTime(Time time) {
super(TimeUnit.MILLISECONDS.toMicros(time.getTime()));
super(TimeUnit.MILLISECONDS.toMicros(timeToMillis(time)));
}

public DuckDBTime(LocalTime lt) {
super(TimeUnit.NANOSECONDS.toMicros(lt.toNanoOfDay()));
}

private static long timeToMillis(Time time) {
// Per JDBC spec PreparedStatement#setTime() must NOT use
// default JVM time zone.
// The Time we have is already shifted with the default time zone,
// so we need to shift it back to keep hours/minutes/seconds
// values intact.
Instant instant = Instant.ofEpochMilli(time.getTime());
ZoneId zoneId = TimeZone.getDefault().toZoneId();
LocalDateTime ldt = LocalDateTime.ofInstant(instant, zoneId);
ZonedDateTime utc = ldt.atZone(ZoneId.of("UTC"));
return utc.toInstant().toEpochMilli();
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/duckdb/DuckDBTimestamp.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ public static Object valueOf(Object x) {
x = new DuckDBDate((Date) x);
} else if (x instanceof Time) {
x = new DuckDBTime((Time) x);
} else if (x instanceof LocalTime) {
x = new DuckDBTime((LocalTime) x);
}
return x;
}
Expand Down
15 changes: 0 additions & 15 deletions src/main/java/org/duckdb/JdbcUtils.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package org.duckdb;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;

final class JdbcUtils {
Expand All @@ -15,18 +12,6 @@ static <T> T unwrap(Object obj, Class<T> iface) throws SQLException {
return (T) obj;
}

static byte[] readAllBytes(InputStream x) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] thing = new byte[256];
int length;
int offset = 0;
while ((length = x.read(thing)) != -1) {
out.write(thing, offset, length);
offset += length;
}
return out.toByteArray();
}

private JdbcUtils() {
}
}
52 changes: 52 additions & 0 deletions src/main/java/org/duckdb/io/IOUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.duckdb.io;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.sql.SQLException;

public class IOUtils {

public static byte[] readAllBytes(InputStream x) throws SQLException {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[8192];
int length;
while ((length = x.read(buffer)) != -1) {
out.write(buffer, 0, length);
}
return out.toByteArray();
} catch (IOException e) {
throw new SQLException(e);
}
}

public static String readToString(Reader reader) throws SQLException {
try {
StringBuilder sb = new StringBuilder();
char[] buffer = new char[4096];
int length;
while ((length = reader.read(buffer)) != -1) {
sb.append(buffer, 0, length);
}
return sb.toString();
} catch (IOException e) {
throw new SQLException();
}
}

public static InputStream wrapStreamWithMaxBytes(InputStream is, long maxBytes) {
if (maxBytes < 0) {
return is;
}
return new LimitedInputStream(is, maxBytes);
}

public static Reader wrapReaderWithMaxChars(Reader reader, long maxChars) {
if (maxChars < 0) {
return reader;
}
return new LimitedReader(reader, maxChars);
}
}
54 changes: 54 additions & 0 deletions src/main/java/org/duckdb/io/LimitedInputStream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.duckdb.io;

import java.io.IOException;
import java.io.InputStream;

public class LimitedInputStream extends InputStream {
private final InputStream source;
private final long maxBytesToRead;
private long bytesRead;

public LimitedInputStream(InputStream source, long maxBytesToRead) {
if (source == null) {
throw new IllegalArgumentException("Source input stream cannot be null");
}
if (maxBytesToRead < 0) {
throw new IllegalArgumentException("maxBytesToRead must be non-negative");
}
this.source = source;
this.maxBytesToRead = maxBytesToRead;
this.bytesRead = 0;
}

@Override
public int read() throws IOException {
if (bytesRead >= maxBytesToRead) {
return -1; // EOF
}
int result = source.read();
if (result != -1) {
bytesRead++;
}
return result;
}

@Override
public int read(byte[] b, int off, int len) throws IOException {
if (bytesRead >= maxBytesToRead) {
return -1; // EOF
}
// Calculate the maximum number of bytes we can read
long bytesRemaining = maxBytesToRead - bytesRead;
int bytesToRead = (int) Math.min(len, bytesRemaining);
int result = source.read(b, off, bytesToRead);
if (result != -1) {
bytesRead += result;
}
return result;
}

@Override
public void close() throws IOException {
source.close();
}
}
53 changes: 53 additions & 0 deletions src/main/java/org/duckdb/io/LimitedReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.duckdb.io;

import java.io.IOException;
import java.io.Reader;

public class LimitedReader extends Reader {
private final Reader source;
private final long maxCharsToRead;
private long charsRead;

public LimitedReader(Reader source, long maxCharsToRead) {
if (source == null) {
throw new IllegalArgumentException("Source Reader cannot be null");
}
if (maxCharsToRead < 0) {
throw new IllegalArgumentException("maxCharsToRead must be non-negative");
}
this.source = source;
this.maxCharsToRead = maxCharsToRead;
this.charsRead = 0;
}

@Override
public int read() throws IOException {
if (charsRead >= maxCharsToRead) {
return -1; // EOF
}
int result = source.read();
if (result != -1) {
charsRead++;
}
return result;
}

@Override
public int read(char[] cbuf, int off, int len) throws IOException {
if (charsRead >= maxCharsToRead) {
return -1; // EOF
}
long charsRemaining = maxCharsToRead - charsRead;
int charsToRead = (int) Math.min(len, charsRemaining);
int result = source.read(cbuf, off, charsToRead);
if (result != -1) {
charsRead += result;
}
return result;
}

@Override
public void close() throws IOException {
source.close();
}
}
Loading
Loading