Skip to content

Commit 360909c

Browse files
Narosbeikov
authored andcommitted
HHH-14725 Fix reset handling on BlobProxy
1 parent f5ddee4 commit 360909c

File tree

3 files changed

+152
-0
lines changed

3 files changed

+152
-0
lines changed

hibernate-core/src/main/java/org/hibernate/engine/jdbc/BlobProxy.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
public final class BlobProxy implements Blob, BlobImplementer {
3232

3333
private final BinaryStream binaryStream;
34+
private final int markBytes;
35+
private boolean resetAllowed;
3436
private boolean needsReset;
3537

3638
/**
@@ -41,6 +43,8 @@ public final class BlobProxy implements Blob, BlobImplementer {
4143
*/
4244
private BlobProxy(byte[] bytes) {
4345
binaryStream = new BinaryStreamImpl( bytes );
46+
markBytes = bytes.length + 1;
47+
setStreamMark();
4448
}
4549

4650
/**
@@ -52,6 +56,18 @@ private BlobProxy(byte[] bytes) {
5256
*/
5357
private BlobProxy(InputStream stream, long length) {
5458
this.binaryStream = new StreamBackedBinaryStream( stream, length );
59+
this.markBytes = (int) length + 1;
60+
setStreamMark();
61+
}
62+
63+
private void setStreamMark() {
64+
if ( binaryStream.getInputStream().markSupported() ) {
65+
binaryStream.getInputStream().mark( markBytes );
66+
resetAllowed = true;
67+
}
68+
else {
69+
resetAllowed = false;
70+
}
5571
}
5672

5773

@@ -67,7 +83,12 @@ public BinaryStream getUnderlyingStream() throws SQLException {
6783
private void resetIfNeeded() throws SQLException {
6884
try {
6985
if ( needsReset ) {
86+
if ( !resetAllowed ) {
87+
throw new SQLException( "Underlying stream does not allow reset" );
88+
}
89+
7090
binaryStream.getInputStream().reset();
91+
setStreamMark();
7192
}
7293
}
7394
catch ( IOException ioe) {
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.orm.test.envers.integration.blob;
8+
9+
import static org.hamcrest.MatcherAssert.assertThat;
10+
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
11+
import static org.junit.Assert.fail;
12+
13+
import java.io.BufferedInputStream;
14+
import java.io.InputStream;
15+
import java.nio.file.Files;
16+
import java.nio.file.Path;
17+
import java.sql.Blob;
18+
19+
import org.hamcrest.Matchers;
20+
import org.hibernate.engine.jdbc.BlobProxy;
21+
import org.hibernate.envers.Audited;
22+
import org.hibernate.orm.test.envers.BaseEnversJPAFunctionalTestCase;
23+
import org.hibernate.orm.test.envers.Priority;
24+
import org.junit.Test;
25+
26+
import jakarta.persistence.Entity;
27+
import jakarta.persistence.GeneratedValue;
28+
import jakarta.persistence.Id;
29+
30+
/**
31+
* @author Chris Cranford
32+
*/
33+
public class BasicBlobTest extends BaseEnversJPAFunctionalTestCase {
34+
35+
@Override
36+
protected Class<?>[] getAnnotatedClasses() {
37+
return new Class<?>[] { Asset.class };
38+
}
39+
40+
@Test
41+
@Priority(10)
42+
public void initData() {
43+
final Path path = Path.of( getClass().getResource( "./blob.txt" ).getPath() );
44+
doInJPA( this::entityManagerFactory, entityManager -> {
45+
try {
46+
final Asset asset = new Asset();
47+
asset.setFileName( "blob.txt" );
48+
49+
final InputStream stream = new BufferedInputStream( Files.newInputStream( path ) );
50+
assertThat( stream.markSupported(), Matchers.is( true ) );
51+
52+
Blob blob = BlobProxy.generateProxy( stream, Files.size( path ) );
53+
54+
asset.setData( blob );
55+
entityManager.persist( asset );
56+
}
57+
catch (Exception e) {
58+
e.printStackTrace();
59+
fail( "Failed to persist the entity" );
60+
}
61+
} );
62+
}
63+
64+
@Audited
65+
@Entity(name = "Asset")
66+
public static class Asset {
67+
@Id
68+
@GeneratedValue
69+
private Integer id;
70+
private String fileName;
71+
private Blob data;
72+
73+
public Integer getId() {
74+
return id;
75+
}
76+
77+
public void setId(Integer id) {
78+
this.id = id;
79+
}
80+
81+
public String getFileName() {
82+
return fileName;
83+
}
84+
85+
public void setFileName(String fileName) {
86+
this.fileName = fileName;
87+
}
88+
89+
public Blob getData() {
90+
return data;
91+
}
92+
93+
public void setData(Blob data) {
94+
this.data = data;
95+
}
96+
}
97+
98+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version='1.0' encoding='utf-8'?>
2+
<!--
3+
~ Hibernate, Relational Persistence for Idiomatic Java
4+
~
5+
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
6+
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
7+
-->
8+
<!DOCTYPE hibernate-configuration PUBLIC
9+
"-//Hibernate/Hibernate Configuration DTD//EN"
10+
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
11+
12+
<hibernate-configuration>
13+
<session-factory>
14+
<property name="hbm2ddl.auto">create-drop</property>
15+
16+
<property name="show_sql">false</property>
17+
<property name="format_sql">false</property>
18+
19+
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
20+
<property name="connection.url">jdbc:h2:mem:envers</property>
21+
<property name="connection.driver_class">org.h2.Driver</property>
22+
<property name="connection.username">sa</property>
23+
<property name="connection.password"></property>
24+
25+
<!--<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>-->
26+
<!--<property name="connection.url">jdbc:mysql:///hibernate_tests?useUnicode=true&amp;characterEncoding=UTF-8</property>-->
27+
<!--<property name="connection.driver_class">com.mysql.jdbc.Driver</property>-->
28+
<!--<property name="connection.username">root</property>-->
29+
<!--<property name="connection.password"></property>-->
30+
31+
<!--<property name="hibernate.jdbc.batch_size">100</property>-->
32+
</session-factory>
33+
</hibernate-configuration>

0 commit comments

Comments
 (0)