Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit03ab5f0

Browse files
committed
This patch updates the ImageViewer example to use Multiple Threading.
When importing an image into the database, the example now fires off anewThread, which imports the image in the background. This also means thatthe application doesn't freeze on the user, and they can still browsetheimages in the database, while the upload is running.This now makes the ImageViewer a true example on how to use Threads (thethreadtest class is just that - a test).Peter
1 parentd32c89b commit03ab5f0

File tree

1 file changed

+120
-49
lines changed

1 file changed

+120
-49
lines changed

‎src/interfaces/jdbc/example/ImageViewer.java

Lines changed: 120 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
* It's only if you use the non jdbc facilities, do you have to take this into
2727
* account.
2828
*
29+
* Note: For PostgreSQL 6.4, the driver is now Thread safe, so this example
30+
* application has been updated to use multiple threads, especially in the
31+
* image storing and retrieving methods.
2932
*/
3033

3134
publicclassImageViewerimplementsItemListener
@@ -42,8 +45,15 @@ public class ImageViewer implements ItemListener
4245
// This is a simple component to display our image
4346
publicclassimageCanvasextendsCanvas
4447
{
48+
// holds the image
4549
privateImageimage;
4650

51+
// holds the background buffer
52+
privateImagebkg;
53+
54+
// the size of the buffer
55+
privateDimensionsize;
56+
4757
publicimageCanvas()
4858
{
4959
image=null;
@@ -71,14 +81,35 @@ public void update(Graphics g)
7181
paint(g);
7282
}
7383

74-
publicvoidpaint(Graphicsg)
84+
/**
85+
* Paints the image, using double buffering to prevent screen flicker
86+
*/
87+
publicvoidpaint(Graphicsgr)
7588
{
89+
Dimensions =getSize();
90+
91+
if(size==null ||bkg==null || !s.equals(size)) {
92+
size =s;
93+
bkg =createImage(size.width,size.height);
94+
}
95+
96+
// now set the background
97+
Graphicsg =bkg.getGraphics();
7698
g.setColor(Color.gray);
77-
g.fillRect(0,0,getSize().width,getSize().height);
99+
g.fillRect(0,0,s.width,s.height);
78100

101+
// now paint the image over the background
79102
if(image!=null)
80103
g.drawImage(image,0,0,this);
104+
105+
// dispose the graphics instance
106+
g.dispose();
107+
108+
// paint the image onto the component
109+
gr.drawImage(bkg,0,0,this);
110+
81111
}
112+
82113
}
83114

84115
publicImageViewer(Framef,Stringurl,Stringuser,Stringpassword)throwsClassNotFoundException,FileNotFoundException,IOException,SQLException
@@ -197,9 +228,8 @@ public void close()
197228
}
198229

199230
/**
200-
* This imports an image into the database.
201-
*
202-
* This is the most efficient method, using the large object extension.
231+
* This imports an image into the database, using a Thread to do this in the
232+
* background.
203233
*/
204234
publicvoidimportImage()
205235
{
@@ -209,51 +239,92 @@ public void importImage()
209239
Stringdir =d.getDirectory();
210240
d.dispose();
211241

212-
// Now the real import stuff
213-
if(name!=null &&dir!=null) {
214-
try {
215-
System.out.println("Importing file");
216-
// A temporary buffer - this can be as large as you like
217-
bytebuf[] =newbyte[2048];
218-
219-
// Open the file
220-
System.out.println("Opening file "+dir+"/"+name);
221-
FileInputStreamfis =newFileInputStream(newFile(dir,name));
222-
223-
// Gain access to large objects
224-
System.out.println("Gaining LOAPI");
225-
226-
// Now create the large object
227-
System.out.println("creating blob");
228-
intoid =lom.create();
229-
230-
System.out.println("Opening "+oid);
231-
LargeObjectblob =lom.open(oid);
242+
// now start the true importer
243+
Threadt =newimporter(db,name,dir);
244+
//t.setPriority(Thread.MAX_PRIORITY);
245+
t.start();
246+
}
247+
248+
/**
249+
* This is an example of using a thread to import a file into a Large Object.
250+
* It uses the Large Object extension, to write blocks of the file to the
251+
* database.
252+
*/
253+
classimporterextendsThread
254+
{
255+
Stringname,dir;
256+
Connectiondb;
257+
258+
publicimporter(Connectiondb,Stringname,Stringdir) {
259+
this.db =db;
260+
this.name =name;
261+
this.dir =dir;
262+
}
263+
264+
publicvoidrun() {
265+
266+
// Now the real import stuff
267+
if(name!=null &&dir!=null) {
268+
Statementstat =null;
232269

233-
// Now copy the file into the object.
234-
//
235-
// Note: we dont use write(buf), as the last block is rarely the same
236-
// size as our buffer, so we have to use the amount read.
237-
System.out.println("Importing file");
238-
ints,t=0;
239-
while((s=fis.read(buf,0,buf.length))>0) {
240-
System.out.println("Block s="+s+" t="+t);t+=s;
241-
blob.write(buf,0,s);
270+
try {
271+
// fetch the large object manager
272+
LargeObjectManagerlom = ((postgresql.Connection)db).getLargeObjectAPI();
273+
274+
System.out.println("Importing file");
275+
// A temporary buffer - this can be as large as you like
276+
bytebuf[] =newbyte[2048];
277+
278+
// Open the file
279+
System.out.println("Opening file "+dir+"/"+name);
280+
FileInputStreamfis =newFileInputStream(newFile(dir,name));
281+
282+
// Gain access to large objects
283+
System.out.println("Gaining LOAPI");
284+
285+
// Now create the large object
286+
System.out.println("creating blob");
287+
intoid =lom.create();
288+
289+
System.out.println("Opening "+oid);
290+
LargeObjectblob =lom.open(oid);
291+
292+
// Now copy the file into the object.
293+
//
294+
// Note: we dont use write(buf), as the last block is rarely the same
295+
// size as our buffer, so we have to use the amount read.
296+
System.out.println("Importing file");
297+
ints,t=0;
298+
while((s=fis.read(buf,0,buf.length))>0) {
299+
System.out.println("Block s="+s+" t="+t);t+=s;
300+
blob.write(buf,0,s);
301+
}
302+
303+
// Close the object
304+
System.out.println("Closing blob");
305+
blob.close();
306+
307+
// Now store the entry into the table
308+
309+
// As we are a different thread to the other window, we must use
310+
// our own thread
311+
stat =db.createStatement();
312+
stat.executeUpdate("insert into images values ('"+name+"',"+oid+")");
313+
314+
// Finally refresh the names list, and display the current image
315+
ImageViewer.this.refreshList();
316+
ImageViewer.this.displayImage(name);
317+
}catch(Exceptionex) {
318+
label.setText(ex.toString());
319+
}finally {
320+
// ensure the statement is closed after us
321+
try {
322+
if(stat !=null)
323+
stat.close();
324+
}catch(SQLExceptionse) {
325+
System.err.println("closing of Statement failed");
326+
}
242327
}
243-
244-
// Close the object
245-
System.out.println("Closing blob");
246-
blob.close();
247-
248-
// Now store the entry into the table
249-
stat.executeUpdate("insert into images values ('"+name+"',"+oid+")");
250-
stat.close();
251-
252-
// Finally refresh the names list, and display the current image
253-
refreshList();
254-
displayImage(name);
255-
}catch(Exceptionex) {
256-
label.setText(ex.toString());
257328
}
258329
}
259330
}
@@ -364,7 +435,7 @@ public static void main(String args[])
364435
}
365436

366437
try {
367-
Frameframe =newFrame("PostgreSQL ImageViewer v6.3 rev 1");
438+
Frameframe =newFrame("PostgreSQL ImageViewer v6.4 rev 1");
368439
frame.setLayout(newBorderLayout());
369440
ImageViewerviewer =newImageViewer(frame,args[0],args[1],args[2]);
370441
frame.pack();

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp