Skip to content

Commit 0add274

Browse files
committed
HHH-19464 Using GZIPInput/OutputStream instead of JarFile
1 parent 04396ad commit 0add274

File tree

3 files changed

+117
-67
lines changed

3 files changed

+117
-67
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.lob;
6+
7+
import jakarta.persistence.Entity;
8+
import jakarta.persistence.Id;
9+
import jakarta.persistence.Lob;
10+
import org.hibernate.engine.jdbc.env.internal.NonContextualLobCreator;
11+
import org.hibernate.testing.orm.junit.DomainModel;
12+
import org.hibernate.testing.orm.junit.JiraKey;
13+
import org.hibernate.testing.orm.junit.SessionFactory;
14+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
15+
import org.junit.jupiter.api.Test;
16+
17+
import java.io.ByteArrayInputStream;
18+
import java.io.ByteArrayOutputStream;
19+
import java.io.IOException;
20+
import java.io.InputStream;
21+
import java.sql.Blob;
22+
import java.sql.SQLException;
23+
import java.util.Random;
24+
import java.util.zip.GZIPInputStream;
25+
import java.util.zip.GZIPOutputStream;
26+
27+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
28+
import static org.junit.jupiter.api.Assertions.assertEquals;
29+
30+
@DomainModel(annotatedClasses = InflaterInputStreamBlobTest.TestEntity.class)
31+
@SessionFactory
32+
@JiraKey("HHH-19464")
33+
class InflaterInputStreamBlobTest {
34+
35+
private static final int RANDOM_SIZE = 32000;
36+
37+
@Test
38+
void hibernate_blob_streaming(SessionFactoryScope scope) throws Exception {
39+
final var randomBytes = getRandomBytes();
40+
final var outputStream = new ByteArrayOutputStream();
41+
try (var zipOutputStream = new GZIPOutputStream( outputStream )) {
42+
zipOutputStream.write( randomBytes );
43+
}
44+
45+
long size = randomBytes.length;
46+
scope.inTransaction( entityManager -> {
47+
try {
48+
InputStream is = new GZIPInputStream( new ByteArrayInputStream( outputStream.toByteArray() ) );
49+
Blob blob = NonContextualLobCreator.INSTANCE.wrap(
50+
NonContextualLobCreator.INSTANCE.createBlob( is, size )
51+
);
52+
TestEntity e = new TestEntity();
53+
e.setId( 1L );
54+
e.setData( blob );
55+
56+
entityManager.persist( e );
57+
}
58+
catch (IOException e) {
59+
throw new RuntimeException( e );
60+
}
61+
}
62+
);
63+
64+
scope.inStatelessSession( session -> {
65+
final var entity = session.get( TestEntity.class, 1L );
66+
try {
67+
final var blob = entity.getData();
68+
assertEquals( size, blob.length() );
69+
assertArrayEquals( randomBytes, blob.getBytes( 1L, (int) blob.length() ) );
70+
}
71+
catch (SQLException e) {
72+
throw new RuntimeException( e );
73+
}
74+
} );
75+
}
76+
77+
private static byte[] getRandomBytes() {
78+
final var bytes = new byte[RANDOM_SIZE];
79+
new Random().nextBytes( bytes );
80+
return bytes;
81+
}
82+
83+
@Entity
84+
public static class TestEntity {
85+
86+
@Id
87+
Long id;
88+
89+
@Lob
90+
Blob data;
91+
92+
public Long getId() {
93+
return id;
94+
}
95+
96+
public void setId(Long id) {
97+
this.id = id;
98+
}
99+
100+
public Blob getData() {
101+
return data;
102+
}
103+
104+
public InputStream getInputStream() {
105+
try {
106+
return data.getBinaryStream();
107+
}
108+
catch (SQLException e) {
109+
throw new IllegalArgumentException( "Could not obtain requested input stream", e );
110+
}
111+
}
112+
113+
public void setData(Blob data) {
114+
this.data = data;
115+
}
116+
}
117+
}

hibernate-core/src/test/java/org/hibernate/orm/test/lob/JarFileEntryBlobTest.java

Lines changed: 0 additions & 67 deletions
This file was deleted.

0 commit comments

Comments
 (0)