comp.lang.java.programmer http://groups-beta.google.com/group/comp.lang.java.programmer [EMAIL PROTECTED]
Today's topics: * store whole InputStream in a String - 16 messages, 3 authors http://groups-beta.google.com/group/comp.lang.java.programmer/browse_thread/thread/d510835287103e9 * Print help - 1 messages, 1 author http://groups-beta.google.com/group/comp.lang.java.programmer/browse_thread/thread/d8e2a798b4dcd983 * Calling Overridden Methods without super - 1 messages, 1 author http://groups-beta.google.com/group/comp.lang.java.programmer/browse_thread/thread/b323ca46ffc0fc37 * Problem with IE and Applets. - 1 messages, 1 author http://groups-beta.google.com/group/comp.lang.java.programmer/browse_thread/thread/85712e86df721a37 * Thumbnail creation problem. really weird. - 1 messages, 1 author http://groups-beta.google.com/group/comp.lang.java.programmer/browse_thread/thread/b8449e560e72a644 ========================================================================== TOPIC: store whole InputStream in a String http://groups-beta.google.com/group/comp.lang.java.programmer/browse_thread/thread/d510835287103e9 ========================================================================== == 1 of 16 == Date: Sat, Sep 11 2004 9:52 pm From: Paul Lutus <[EMAIL PROTECTED]> Mike Schilling wrote: > > "Paul Lutus" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] >> Mike Schilling wrote: >> >>> >>> "Paul Lutus" <[EMAIL PROTECTED]> wrote in message >>> news:[EMAIL PROTECTED] >>>> ak wrote: >>>> >>>>>> Can I write all contents of an InputStream to a String at once? >>>>>> >>>>>> something like >>>>>> >>>>>> String a = inputStream.readAll(); >>>>>> >>>>> sure: >>>>> >>>>> public String readAll(InputStream inputStream) { >>>>> ByteArrayOutputStream bout = new ByteArrayOutputStream(); >>>>> byte [] buffer = new byte[1024]; >>>>> while(true) { >>>>> int len = inputStream.read[buffer]; >>>>> if(len < 0) { >>>>> break; >>>>> } >>>>> bout.write(buffer, 0, len); >>>>> } >>>> >>>> ??? Please consider the following, and avoid constructions like >>>> "while(true)" and "break" when possible: >>>> >>>> int len; >>>> >>>> while((len = inputStream.read[buffer]) > 0) { >>>> bout.write(buffer, 0, len); >>>> } >>>> >>> >>> I find the other version clearer, myself. >> >> You can't be serious. The original arrangement has two control structures >> where only one is needed. Also, both "while(true)" and "break" are >> frowned upon in any case where they are not required. They are not >> particularly good examples of structure programming. > > I am entirely serious. Then I don't know what to say. I posted the appropriate construction, the one that confers the greatest reliability and maintainability, the one that is most consistent with structured prorgamming principles. Then (see below, as as you point out) I managed to post a bronken version of the same idea. > >> >> A while() clause should actually perform a test, not be added as a patch >> to >> create a loop that must later be broken out of, by force, using break. >> >>> The nesting of method call and >>> test in the while statement is too much information in one line, >> >> 1. A for-loop typically contains one additional piece of information >> (three >> instead of two items as in my example). Want to object to that also? > > They're nicely punctuated.. :) Not terribly relevant. > >> >> 2. Compared to the original, this version is simpler to read and >> interpret. >> It is also better coding practice -- it doesn't misuse while(). >> >>> and it's >>> unfortunate to declare len outside the loop that it's only used inside >>> of. >> >> I could instead have said: >> >> for (int len = inputStream.read[buffer]); len > 0;) { >> } >> >> But this replaces one arbitrary choice with another, and uses a for >> clause in a place where it is not the ideal choice. > > And it's wrong :-) It reads from the stream only once, rather than each > time through the loop. I should have tested it and posted more carefully. I meant to say: for (int len;(len = inputStream.read[buffer])) > 0;) { } In other words, the same construction I have been advocating with a minor variation to change the scope of the variable. > You need > > len = inputStream.read[buffer] > > as the iteration clause. > >> >> In any case, writing "while(true)" simply builds a perpetual loop that >> requires "break" to terminate, creating the illusion of appropriate >> usage. > > > You make a lot of absolute assertions. I disagree with many of them. This is a programming principle, not a debate about something vague, something about which there is any dispute. Reliable, maintainable code doesn't arise by accident. > I'm > not sure what that leaves us to discuss. I agree. This is not a matter for any kind of debate. -- Paul Lutus http://www.arachnoid.com == 2 of 16 == Date: Sat, Sep 11 2004 9:54 pm From: Paul Lutus <[EMAIL PROTECTED]> Tony Morris wrote: > I'm waiting for someone to notice in this thread, as I thought it was > inevitable, but to no avail. > > Take a closer look at the following line of code that I keep seeing > posted: > > int len = inputStream.read[buffer]; Yes, it should be: int len = inputStream.read(buffer); But I haven't been compiling anything (as should be obvious from my other error). -- Paul Lutus http://www.arachnoid.com == 3 of 16 == Date: Sat, Sep 11 2004 9:58 pm From: Paul Lutus <[EMAIL PROTECTED]> Mike Schilling wrote: > > "Tony Morris" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > >> >> Side Note: I'm with Paul Lutus all the way so far, but then, I'm more of >> a "purist" than most. > > > You like infinite for loops? :-) It would not have been infinite had I been paying attention to what I was posting. The correct version looks so much like the original while clause that there is scarcely any point in posting it as a variation -- the only difference is that the "len" variable is local to the loop. For the record, to correct two errors, one mine, one belonging to the OP: // using while int len; while((len = inputStream.read(buffer)) > 0) { bout.write(buffer, 0, len); } // using for for(int len;(len = inputStream.read(buffer)) > 0;) { bout.write(buffer, 0, len); } Apologies to all for my laziness. -- Paul Lutus http://www.arachnoid.com == 4 of 16 == Date: Sat, Sep 11 2004 10:04 pm From: Paul Lutus <[EMAIL PROTECTED]> Vincent Lascaux wrote: / ... > If I understand your code (I am not sure I do), and if it is equivalent to > for(int len = inputStream.read(buffer); len>0; ) { /* Body */ } > then it is equivalent to > { int len = inputStream.read(buffer); while(len>0) { /* Body */ } } > Since /*Body*/ is not supposed to change len, you have an infinite loop > (for a non empty inputStream) I got lazy, and I thank you for your correction. The correct for-loop looks very much like the while-loop, differing only in that the "len" variable is made local to the loop. I only offered the for-loop version because the objection was raised that "len" was not local. // using while int len; while((len = inputStream.read(buffer)) > 0) { bout.write(buffer, 0, len); } // using for for(int len;(len = inputStream.read(buffer)) > 0;) { bout.write(buffer, 0, len); } In any case, the problem with those versions that duplicate one line of code is that there is a duplicate line of code, something professional code administrators try to avoid at all costs. The problem with "while(true)" should be obvious to all -- it is a hack, as is the use of "break" to get around the original hack. -- Paul Lutus http://www.arachnoid.com == 5 of 16 == Date: Sat, Sep 11 2004 10:07 pm From: Paul Lutus <[EMAIL PROTECTED]> Mike Schilling wrote: > > "Paul Lutus" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] >> >>> then it should be >>> for(int len = inputStream.read(buffer); len>0; len = >>> inputStream.read(buffer)) { >> >> What? No wonder you think there's something wrong. Your example is broken >> (it discards the first block of input), mine is not. > > No, try it. That loop is entirely correct. Yours only reads from the > stream once. Yes, thank you, my mistake. Here are the correct versions: // using while int len; while((len = inputStream.read(buffer)) > 0) { bout.write(buffer, 0, len); } // using for for(int len;(len = inputStream.read(buffer)) > 0;) { bout.write(buffer, 0, len); } Again, it is very desirable from a code maintenance standpoint to avoid duplicate lines of code. The alternatives all seem to duplicate one line, or use "while(true)", or some other hack. -- Paul Lutus http://www.arachnoid.com == 6 of 16 == Date: Sat, Sep 11 2004 10:11 pm From: Paul Lutus <[EMAIL PROTECTED]> ak wrote: / ... >> Summary: Never write two identical lines of code when you can instead > write >> one. >> > > ok, what is with folowing, also nightmare? > > int len; > do { > len = inputStream.read[buffer]; > if(len > 0) { > bout.write(buffer, 0, len); > } > }while(len > 0); Well, it has to test a value twice. This is a bad idea. Avoid duplicate code lines. > > or better: > > int len; > do { > len = inputStream.read[buffer]; > writeBuffer(buffer, len, bout); > } No, this never tests "len" for zero, so it runs on foerever after the end of the file. > > private static void writeBuffer(byte [] buffer, int len, OutputStream out) > { > if(len > 0) { > out.write(buffer, 0, len); > } > } This doesn't solve the problem that the original do-loop doesn't have a provision to terminate. All apart from an error I have also been ignoring: len = inputStream.read[buffer]; -> len = inputStream.read(buffer); -- Paul Lutus http://www.arachnoid.com == 7 of 16 == Date: Sat, Sep 11 2004 10:13 pm From: Paul Lutus <[EMAIL PROTECTED]> ak wrote: >> > int len = inputStream.read[buffer]; >> > while(len > 0) { >> > bout.write(buffer, 0, len); >> > len = inputStream.read[buffer]; >> > } >> >> A maintenance nightmare. This is an example of the kind of coding >> practice that drives software administrators up the wall. Always avoid >> this silly construction, the kind that uses two identical lines of code >> to avoid a little thought. > > there is nothing wrong if len initialized with same code as incremented. You have two identical lines in your code. This is a bad idea. Code administrators try to keep this from happening. It invites errors any time the code is changed. Also, it makes programs physically longer for no reason. >> Summary: Never write two identical lines of code when you can instead > write >> one. > even if this one line looks ugly? It doesn't look ugly after you have seen it ten thousand times. :) -- Paul Lutus http://www.arachnoid.com == 8 of 16 == Date: Sat, Sep 11 2004 10:24 pm From: Ben O'Brien <[EMAIL PROTECTED]> ak wrote: >>Can I write all contents of an InputStream to a String at once? >> >>something like >> >>String a = inputStream.readAll(); >> > > sure: > > public String readAll(InputStream inputStream) { > ByteArrayOutputStream bout = new ByteArrayOutputStream(); > byte [] buffer = new byte[1024]; > while(true) { > int len = inputStream.read[buffer]; > if(len < 0) { > break; > } > bout.write(buffer, 0, len); > } > byte [] data = bout.toByteArray(); > return new String(data); > } > > String a = readAll(in); > Good answer this one, except I must agree with Paul so my answer would be: public String readAll(InputStream inputStream) { ByteArrayOutputStream bout = new ByteArrayOutputStream(); byte [] buffer = new byte[1024]; int len; while(len = inputStream.read(buffer) > 0) { bout.write(buffer, 0, len); } byte [] data = bout.toByteArray(); return new String(data); } String a = readAll(in); IMHO break, continue and goto all make for spaghettified code (aaaah Fortran) that is a bit hard to read sometimes. My/Paul's version doesn't declare len multiple times either, and it doesn't have the chance writing zero bytes either. Honestly though, if we were worried about these minimal performance improvements we wouldn't be using Java would we? Although every little bit helps. Also the buffer size might be tuned a bit, depending on the application I suppose. Ben. == 9 of 16 == Date: Sat, Sep 11 2004 10:36 pm From: "Mike Schilling" <[EMAIL PROTECTED]> "Paul Lutus" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Mike Schilling wrote: >> >> >> You make a lot of absolute assertions. I disagree with many of them. > > This is a programming principle, not a debate about something vague, > something about which there is any dispute. Reliable, maintainable code > doesn't arise by accident. When making working, readable code more "reliable and maintainable" results in breaking it, I become skeptical, particularly since it had to be pointed out to you several times that it was broken. Perhaps it's not as maintainable as you maintain. == 10 of 16 == Date: Sat, Sep 11 2004 10:38 pm From: "Mike Schilling" <[EMAIL PROTECTED]> "Paul Lutus" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > > In any case, writing "while(true)" simply builds a perpetual loop that > requires "break" to terminate, creating the illusion of appropriate usage. It builds a loop which will be exited from the middle rather than the top or bottom, which is precisely the case. == 11 of 16 == Date: Sat, Sep 11 2004 10:43 pm From: Paul Lutus <[EMAIL PROTECTED]> Mike Schilling wrote: > > "Paul Lutus" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] >> >> In any case, writing "while(true)" simply builds a perpetual loop that >> requires "break" to terminate, creating the illusion of appropriate >> usage. > > > It builds a loop which will be exited from the middle rather than the top > or bottom, which is precisely the case. Precisely what case is that? It is a hack. It uses "while(true)", hack one, then to repair hack one, it uses "break". Neither is required, and neither is desirable in a maintainable program that meets structured programming principles. -- Paul Lutus http://www.arachnoid.com == 12 of 16 == Date: Sat, Sep 11 2004 10:45 pm From: Paul Lutus <[EMAIL PROTECTED]> Mike Schilling wrote: > > "Paul Lutus" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] >> Mike Schilling wrote: > >>> >>> >>> You make a lot of absolute assertions. I disagree with many of them. >> >> This is a programming principle, not a debate about something vague, >> something about which there is any dispute. Reliable, maintainable code >> doesn't arise by accident. > > When making working, readable code more "reliable and maintainable" > results in breaking it, Post the evidence, not the assertion. I thought I was the one making "absolute assertions." Here is the current code: // using while int len; while((len = inputStream.read(buffer)) > 0) { bout.write(buffer, 0, len); } // using for for(int len;(len = inputStream.read(buffer)) > 0;) { bout.write(buffer, 0, len); } > I become skeptical, particularly since it had to > be pointed > out to you several times that it was broken. Perhaps it's not as > maintainable as you maintain. Try to focus on the topic, not an irrelevant digression. -- Paul Lutus http://www.arachnoid.com == 13 of 16 == Date: Sun, Sep 12 2004 12:01 am From: "Mike Schilling" <[EMAIL PROTECTED]> "Paul Lutus" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Mike Schilling wrote: > >> >> "Paul Lutus" <[EMAIL PROTECTED]> wrote in message >> news:[EMAIL PROTECTED] >>> Mike Schilling wrote: >> >>>> >>>> >>>> You make a lot of absolute assertions. I disagree with many of them. >>> >>> This is a programming principle, not a debate about something vague, >>> something about which there is any dispute. Reliable, maintainable code >>> doesn't arise by accident. >> >> When making working, readable code more "reliable and maintainable" >> results in breaking it, > > Post the evidence, not the assertion. I thought I was the one making > "absolute assertions." Have you forgottne so soon? You improved working code with an infinite loop. >> I become skeptical, particularly since it had to >> be pointed >> out to you several times that it was broken. Perhaps it's not as >> maintainable as you maintain. > > Try to focus on the topic, not an irrelevant digression. Whether code works is irrelevant to its reliability? == 14 of 16 == Date: Sun, Sep 12 2004 12:13 am From: "Mike Schilling" <[EMAIL PROTECTED]> "Paul Lutus" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Mike Schilling wrote: > >> >> "Paul Lutus" <[EMAIL PROTECTED]> wrote in message >> news:[EMAIL PROTECTED] >>> >>> In any case, writing "while(true)" simply builds a perpetual loop that >>> requires "break" to terminate, creating the illusion of appropriate >>> usage. >> >> >> It builds a loop which will be exited from the middle rather than the top >> or bottom, which is precisely the case. > > Precisely what case is that? The case of a loop which naturally exists in the middle of its bod. It does something (read from the stream) and, depending on the status of that, either terminates or does more work and goes around again. Unless you torture the first part of the body into a single statement, a while(true) followed by a conditional break is the only way to express that. > It is a hack. Arguement by assetion 1. > It uses "while(true)", hack one, > then to repair hack one, it uses "break". Neither is required, and neither > is desirable in a maintainable program that meets structured programming > principles. Argument by assertion 2 (a particularly silly one, as it calls the use of "break" to exit a loop a "hack".) "This is the sort of nonsense up with which I will not put" -- Winston Churchill == 15 of 16 == Date: Sun, Sep 12 2004 1:10 am From: Paul Lutus <[EMAIL PROTECTED]> Mike Schilling wrote: > > "Paul Lutus" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] >> Mike Schilling wrote: >> >>> >>> "Paul Lutus" <[EMAIL PROTECTED]> wrote in message >>> news:[EMAIL PROTECTED] >>>> >>>> In any case, writing "while(true)" simply builds a perpetual loop that >>>> requires "break" to terminate, creating the illusion of appropriate >>>> usage. >>> >>> >>> It builds a loop which will be exited from the middle rather than the >>> top or bottom, which is precisely the case. >> >> Precisely what case is that? > The case of a loop which naturally exists in the middle of its bod. You meant "exits", I presume. > It > does something (read from the stream) and, depending on the status of > that, > either terminates or does more work and goes around again. Unless you > torture ... And "torture" is not argument by assertion? The construction you are going on about is very commonly seen in programming practice, in all current languages. The algorithm I posted is shorter, clearer, more maintainable and more consistent with programming standards than the alternatives. > the first part of the body into a single statement, e.g. do it properly, in the manner recommended by the principles of structured programming, buy using "while" as it was intended to be used. And as seen in any number of programs whose designers didn't want the opprobrium that attends either multiple identical code lines or misuses of the language. > a while(true) > followed by a conditional break is the only way to express that. A while(true) followed by break is the sure sign of someone who needs remedial programming instruction. >> It is a hack. > > Arguement by assetion 1. No, it is a hack, not an argument by assertion. "while(true)" is a way to get around the requirement to test something. It is a misuse of "while". That is a hack. > >> It uses "while(true)", hack one, >> then to repair hack one, it uses "break". Neither is required, and >> neither is desirable in a maintainable program that meets structured >> programming principles. > > Argument by assertion 2 (a particularly silly one, as it calls the use of > "break" to exit a loop a "hack".) That is in fact a hack, as are 99% of the uses of "break", "continue" et. al.. > > "This is the sort of nonsense up with which I will not put" -- Winston > Churchill A revealing argument by someone intent on arguing the substance of an issue. -- Paul Lutus http://www.arachnoid.com == 16 of 16 == Date: Sun, Sep 12 2004 1:13 am From: Paul Lutus <[EMAIL PROTECTED]> Mike Schilling wrote: / ... >> Post the evidence, not the assertion. I thought I was the one making >> "absolute assertions." > Have you forgottne so soon? You improved working code with an infinite > loop. Have you forgotten so soon? This has been corrected, see below, and in any case it doesn't affect the substance of the discussion. Reading your posts, one wonders whether you really want to discuss the issue. > >>> I become skeptical, particularly since it had to >>> be pointed >>> out to you several times that it was broken. Perhaps it's not as >>> maintainable as you maintain. >> >> Try to focus on the topic, not an irrelevant digression. > > Whether code works is irrelevant to its reliability? For the fifth time: // using while int len; while((len = inputStream.read(buffer)) > 0) { bout.write(buffer, 0, len); } // using for for(int len;(len = inputStream.read(buffer)) > 0;) { bout.write(buffer, 0, len); } You just ran out of excuses for avoiding the errors in your position. -- Paul Lutus http://www.arachnoid.com ========================================================================== TOPIC: Print help http://groups-beta.google.com/group/comp.lang.java.programmer/browse_thread/thread/d8e2a798b4dcd983 ========================================================================== == 1 of 1 == Date: Sat, Sep 11 2004 11:03 pm From: David Segall <[EMAIL PROTECTED]> "neo" <[EMAIL PROTECTED]> wrote: >Hi, > >I'm new in Java technology and I'm using Eclipse which is helping me a lot, >by far the best IDE available. I make GUIs just that easy like Visual Basic, >I wonder if there is a tool to make reports for print documents, a graphic >tool where I can define page borders, headers and footers, etc. iReport (http://ireport.sourceforge.net/)is a good report designer. It is a visual front end for JasperReports (http://jasperreports.sourceforge.net/) and JFreeChart (http://www.jfree.org/jfreechart/index.html). >If not, please tell me what Classes must I use and give me the easiest way >to make visual reports > >Thank you very much in advance > ========================================================================== TOPIC: Calling Overridden Methods without super http://groups-beta.google.com/group/comp.lang.java.programmer/browse_thread/thread/b323ca46ffc0fc37 ========================================================================== == 1 of 1 == Date: Sat, Sep 11 2004 11:59 pm From: Joona I Palaste <[EMAIL PROTECTED]> Frank <[EMAIL PROTECTED]> scribbled the following: > On 10 Sep 2004 08:45:33 -0700, Marco <[EMAIL PROTECTED]> wrote: >> Can I call the overridden version of an object's foo() method >> outside that object's class, without using super? EG: >> >> class MainProgram { >> public static void main(String[] args) { >> Beta b = new Beta(); >> >> // I want to invoke Alpha.foo() but on the Beta instance. >> // I want to cancel the dynamic binding that makes Beta.foo() >> // get called instead >> // b.super.foo() <---- obviously doesn't compile >> } >> } >> >> class Alpha { >> public void foo() { System.out.println("inside Alpha.foo()"); } >> } >> >> class Beta extends Alpha { >> public void foo() { System.out.println("inside Beta.foo()"); } >> } >> >> Is this possible? >> Marco > There are some other responses here, but I've noticed most people have > said no, but the answer is actually yes (sorta): > change > class Beta extends Alpha { > public void foo() { System.out.println("inside Beta.foo()"); } > public void origionalFoo() { super.foo(); } > } > then your main method reads: > Beta b=new Beta(); > b.origionalFoo(); The point is that this, like the other reply with a workaround, requires a change to Beta's code. If Beta includes only one definition of foo(), and that does not explicitly include a call to super.foo(), then it is impossible to make foo() in Beta call foo() in Alpha from other code, without modifying the code in Beta. >From the OP's original question I got the idea that he was trying to circumvent the overriding without modifying the code in Beta. If this were possible, it would even present a security risk in some applications, when the behaviour of code in a class could be changed without the class being able to do anything about it. -- /-- Joona Palaste ([EMAIL PROTECTED]) ------------- Finland --------\ \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/ "He said: 'I'm not Elvis'. Who else but Elvis could have said that?" - ALF ========================================================================== TOPIC: Problem with IE and Applets. http://groups-beta.google.com/group/comp.lang.java.programmer/browse_thread/thread/85712e86df721a37 ========================================================================== == 1 of 1 == Date: Sun, Sep 12 2004 12:25 am From: [EMAIL PROTECTED] (Vardan) Hello When i make html file, and include there this code: <applet codebase="." archive="myapplet.jar" code="myapplet.MainClass" width="512" height="384" alt="jChatBox Client Applet"> </applet> and run it in Internet Explorer it write that Applet myapplet.MainClass.class not found. But when i open http://www.javazoom.net/services/jchatbox/jchatbox.html and run there applet it's work. Why? Thanks for help. P.S. Sorry for my bad english:) ========================================================================== TOPIC: Thumbnail creation problem. really weird. http://groups-beta.google.com/group/comp.lang.java.programmer/browse_thread/thread/b8449e560e72a644 ========================================================================== == 1 of 1 == Date: Sun, Sep 12 2004 12:32 am From: "hilz" <[EMAIL PROTECTED]> Hi all, After doing a search on how to create thumbnails from jpg's, i found the code below. If i run this code from a standalone java program, it works fine and it creates the thumbnail, but if i run it from inside a jsp page, it creates a black thumbnail with nothing in it!!!! does anyone know what could be the problem? it is driving me crazy. thanks hilz //usage: java Thumbnail <file.jpg> <newfile.jpg> <MAXWIDTH> import java.awt.Image; import java.awt.Graphics2D; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.OutputStream; import java.io.FileOutputStream; import javax.swing.ImageIcon; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; class ThumbnailCreator { public static void main(String[] args) { createThumbnail(args[0], args[1], Integer.parseInt(args[2])); } /** * Reads an image in a file and creates * a thumbnail in another file. * @param orig The name of image file. * @param thumb The name of thumbnail file. * Will be created if necessary. * @param maxDim The width and height of * the thumbnail must * be maxDim pixels or less. */ public static void createThumbnail( String orig, String thumb, int maxDim) { try { // Get the image from a file. java.awt.Image inImage = new javax.swing.ImageIcon(orig).getImage(); // Determine the scale. double scale = (double)maxDim/(double)inImage.getHeight(null); if (inImage.getWidth(null) > inImage.getHeight(null)) { scale = (double)maxDim/(double)inImage.getWidth(null); } // Determine size of new image. //One of them // should equal maxDim. int scaledW = (int)(scale*inImage.getWidth(null)); int scaledH = (int)(scale*inImage.getHeight(null)); // Create an image buffer in //which to paint on. java.awt.image.BufferedImage outImage = new java.awt.image.BufferedImage(scaledW, scaledH,java.awt.image.BufferedImage.TYPE_INT_RGB); // Set the scale. java.awt.geom.AffineTransform tx = new java.awt.geom.AffineTransform(); // If the image is smaller than //the desired image size, // don't bother scaling. if (scale < 1.0d) { tx.scale(scale, scale); } // Paint image. java.awt.Graphics2D g2d = outImage.createGraphics(); g2d.drawImage(inImage, tx, null); g2d.dispose(); // JPEG-encode the image //and write to file. java.io.OutputStream os = new java.io.FileOutputStream(thumb); com.sun.image.codec.jpeg.JPEGImageEncoder encoder = com.sun.image.codec.jpeg.JPEGCodec.createJPEGEncoder(os); encoder.encode(outImage); os.close(); } catch (java.io.IOException e) { e.printStackTrace(); } System.exit(0); } } ======================================================================= You received this message because you are subscribed to the Google Groups "comp.lang.java.programmer". comp.lang.java.programmer [EMAIL PROTECTED] Change your subscription type & other preferences: * click http://groups-beta.google.com/group/comp.lang.java.programmer/subscribe Report abuse: * send email explaining the problem to [EMAIL PROTECTED] Unsubscribe: * click http://groups-beta.google.com/group/comp.lang.java.programmer/subscribe ======================================================================= Google Groups: http://groups-beta.google.com ------------------------ Yahoo! Groups Sponsor --------------------~--> Make a clean sweep of pop-up ads. Yahoo! Companion Toolbar. Now with Pop-Up Blocker. Get it for free! http://us.click.yahoo.com/L5YrjA/eSIIAA/yQLSAA/BCfwlB/TM --------------------------------------------------------------------~-> <a href=http://English-12948197573.SpamPoison.com>Fight Spam! Click Here!</a> Yahoo! Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/kumpulan/ <*> To unsubscribe from this group, send an email to: [EMAIL PROTECTED] <*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/
