<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Frost137's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 17 May 2008 22:20:05 GMT</pubDate>
    <description>DZone Snippets: Frost137's Code Snippets</description>
    <item>
      <title>Java Cons class</title>
      <link>http://snippets.dzone.com/posts/show/5419</link>
      <description>Very simple Cons class for Java&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public class Cons&lt;T,U&gt;&lt;br /&gt;{&lt;br /&gt;    private T car;&lt;br /&gt;    private U cdr;&lt;br /&gt;&lt;br /&gt;    public Cons( T carArg, U cdrArg )&lt;br /&gt;    {&lt;br /&gt;        car = carArg;&lt;br /&gt;        cdr = cdrArg;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public T getCar(){ return car; }&lt;br /&gt;    public U getCdr(){ return cdr; }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 21 Apr 2008 23:43:50 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5419</guid>
      <author>frost137 (Douglas Wyatt)</author>
    </item>
    <item>
      <title>Common keytool commands needed for living with Java</title>
      <link>http://snippets.dzone.com/posts/show/5362</link>
      <description>&lt;code&gt;&lt;br /&gt;// generate a key -- make it long lived so we dont have to do this again&lt;br /&gt;keytool -genkey -alias tomcat -keyalg RSA -validity 3650 -storepass changeit&lt;br /&gt;&lt;br /&gt;// export cert to a file&lt;br /&gt;keytool -export -rfc -v -file tomcatCert.crt -alias tomcat -storepass changeit&lt;br /&gt; &lt;br /&gt;// look at the cert in the file&lt;br /&gt;keytool -printcert -file tomcatCert.crt -storepass changeit&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// delete pre-existing cert&lt;br /&gt;keytool -delete -alias tomcat -keystore c:/apps/jdk/jre/lib/security/cacerts -storepass changeit&lt;br /&gt;&lt;br /&gt;// import cert into a keystore&lt;br /&gt;keytool -import -file tomcatCert.crt -trustcacerts -alias tomcat -keystore c:/apps/jdk/jre/lib/security/cacerts -storepass changeit&lt;br /&gt;&lt;br /&gt;// look at the imported cert&lt;br /&gt;keytool -list -alias tomcat -keystore c:/apps/jdk/jre/lib/security/cacerts -storepass changeit&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 14 Apr 2008 18:06:50 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5362</guid>
      <author>frost137 (Douglas Wyatt)</author>
    </item>
    <item>
      <title>Convert Java date to GMT</title>
      <link>http://snippets.dzone.com/posts/show/5288</link>
      <description>This function converts a local date to GMT.  This version corrects the bug common to this type of conversion where the date is incorrectly converted when the time is close to the DST crossover.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;private static Date cvtToGmt( Date date )&lt;br /&gt;{&lt;br /&gt;   TimeZone tz = TimeZone.getDefault();&lt;br /&gt;   Date ret = new Date( date.getTime() - tz.getRawOffset() );&lt;br /&gt;&lt;br /&gt;   // if we are now in DST, back off by the delta.  Note that we are checking the GMT date, this is the KEY.&lt;br /&gt;   if ( tz.inDaylightTime( ret ))&lt;br /&gt;   {&lt;br /&gt;      Date dstDate = new Date( ret.getTime() - tz.getDSTSavings() );&lt;br /&gt;&lt;br /&gt;      // check to make sure we have not crossed back into standard time&lt;br /&gt;      // this happens when we are on the cusp of DST (7pm the day before the change for PDT)&lt;br /&gt;      if ( tz.inDaylightTime( dstDate ))&lt;br /&gt;      {&lt;br /&gt;         ret = dstDate;&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   return ret;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 28 Mar 2008 20:56:15 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5288</guid>
      <author>frost137 (Douglas Wyatt)</author>
    </item>
    <item>
      <title>Drop an unnamed constraint from SqlServer 2000</title>
      <link>http://snippets.dzone.com/posts/show/5221</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;declare @name nvarchar(32), &lt;br /&gt;    @sql nvarchar(1000)&lt;br /&gt;&lt;br /&gt;-- find constraint name&lt;br /&gt;select @name = O.name &lt;br /&gt;from sysobjects AS O&lt;br /&gt;left join sysobjects AS T&lt;br /&gt;    on O.parent_obj = T.id&lt;br /&gt;where isnull(objectproperty(O.id,'IsMSShipped'),1) = 0&lt;br /&gt;    and O.name not like '%dtproper%'&lt;br /&gt;    and O.name not like 'dt[_]%'&lt;br /&gt;    and T.name = 'MyTable'&lt;br /&gt;    and O.name like 'DF__MyTable__MyColu%'&lt;br /&gt;&lt;br /&gt;-- delete if found&lt;br /&gt;if not @name is null&lt;br /&gt;begin&lt;br /&gt;    select @sql = 'ALTER TABLE [MyTable] DROP CONSTRAINT [' + @name + ']'&lt;br /&gt;    execute sp_executesql @sql&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;-- do your ALTER TABLE here&lt;br /&gt;&lt;br /&gt;-- replace the constraint&lt;br /&gt;select @sql = 'ALTER TABLE [MyTable] ADD CONSTRAINT [' + @name + '] DEFAULT (0) FOR [MyColumn]'&lt;br /&gt;execute sp_executesql @sql&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 12 Mar 2008 22:00:43 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5221</guid>
      <author>frost137 (Douglas Wyatt)</author>
    </item>
    <item>
      <title>Easiest way to do a POST test from Java</title>
      <link>http://snippets.dzone.com/posts/show/5213</link>
      <description>I need to test services with POST transactions.  Using HTML to do it is the easiest way, constructed in Java (that part isn't necessary unless the args are somehow cooked or just too complex to trust hand-crafted HTML).&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;// write the HTML code out to stdout -- leave it to the user to redirect&lt;br /&gt;System.out.print( "&lt;html&gt;&lt;body&gt;&lt;form id='test' name='test' action='" );&lt;br /&gt;System.out.print( url );&lt;br /&gt;System.out.print( "/getStuff' method='POST'&gt;&lt;input type='hidden' id='cid' name='cid' value=\"" );&lt;br /&gt;System.out.print(  compId );&lt;br /&gt;System.out.print( "\"/&gt;&lt;input type='hidden' id='ids' name='ids' value=\"" );&lt;br /&gt;System.out.print( ids );&lt;br /&gt;System.out.print( "\"/&gt;" );&lt;br /&gt;System.out.print( "&lt;input type='submit' name='Submit' value='Submit'&gt;&lt;/form&gt;&lt;/body&gt;&lt;/html&gt;" );&lt;br /&gt;System.out.println();&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 12 Mar 2008 00:05:42 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5213</guid>
      <author>frost137 (Douglas Wyatt)</author>
    </item>
    <item>
      <title>Java filecopy using NIO</title>
      <link>http://snippets.dzone.com/posts/show/4946</link>
      <description>&lt;code&gt;&lt;br /&gt;    public static void fileCopy( File in, File out )&lt;br /&gt;            throws IOException&lt;br /&gt;    {&lt;br /&gt;        FileChannel inChannel = new FileInputStream( in ).getChannel();&lt;br /&gt;        FileChannel outChannel = new FileOutputStream( out ).getChannel();&lt;br /&gt;        try&lt;br /&gt;        {&lt;br /&gt;//          inChannel.transferTo(0, inChannel.size(), outChannel);      // original -- apparently has trouble copying large files on Windows&lt;br /&gt;&lt;br /&gt;            // magic number for Windows, 64Mb - 32Kb)&lt;br /&gt;            int maxCount = (64 * 1024 * 1024) - (32 * 1024);&lt;br /&gt;            long size = inChannel.size();&lt;br /&gt;            long position = 0;&lt;br /&gt;            while ( position &lt; size )&lt;br /&gt;            {&lt;br /&gt;               position += inChannel.transferTo( position, maxCount, outChannel );&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;        finally&lt;br /&gt;        {&lt;br /&gt;            if ( inChannel != null )&lt;br /&gt;            {&lt;br /&gt;               inChannel.close();&lt;br /&gt;            }&lt;br /&gt;            if ( outChannel != null )&lt;br /&gt;            {&lt;br /&gt;                outChannel.close();&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 04 Jan 2008 22:40:01 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4946</guid>
      <author>frost137 (Douglas Wyatt)</author>
    </item>
    <item>
      <title>Example file download servlet</title>
      <link>http://snippets.dzone.com/posts/show/4629</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  /**&lt;br /&gt;     *  Sends a file to the ServletResponse output stream.  Typically&lt;br /&gt;     *  you want the browser to receive a different name than the&lt;br /&gt;     *  name the file has been saved in your local database, since&lt;br /&gt;     *  your local names need to be unique.&lt;br /&gt;     *&lt;br /&gt;     *  @param req The request&lt;br /&gt;     *  @param resp The response&lt;br /&gt;     *  @param filename The name of the file you want to download.&lt;br /&gt;     *  @param original_filename The name the browser should receive.&lt;br /&gt;     */&lt;br /&gt;    private void doDownload( HttpServletRequest req, HttpServletResponse resp,&lt;br /&gt;                             String filename, String original_filename )&lt;br /&gt;        throws IOException&lt;br /&gt;    {&lt;br /&gt;        File                f        = new File(filename);&lt;br /&gt;        int                 length   = 0;&lt;br /&gt;        ServletOutputStream op       = resp.getOutputStream();&lt;br /&gt;        ServletContext      context  = getServletConfig().getServletContext();&lt;br /&gt;        String              mimetype = context.getMimeType( filename );&lt;br /&gt;&lt;br /&gt;        //&lt;br /&gt;        //  Set the response and go!&lt;br /&gt;        //&lt;br /&gt;        //&lt;br /&gt;        resp.setContentType( (mimetype != null) ? mimetype : "application/octet-stream" );&lt;br /&gt;        resp.setContentLength( (int)f.length() );&lt;br /&gt;        resp.setHeader( "Content-Disposition", "attachment; filename=\"" + original_filename + "\"" );&lt;br /&gt;&lt;br /&gt;        //&lt;br /&gt;        //  Stream to the requester.&lt;br /&gt;        //&lt;br /&gt;        byte[] bbuf = new byte[BUFSIZE];&lt;br /&gt;        DataInputStream in = new DataInputStream(new FileInputStream(f));&lt;br /&gt;&lt;br /&gt;        while ((in != null) &amp;&amp; ((length = in.read(bbuf)) != -1))&lt;br /&gt;        {&lt;br /&gt;            op.write(bbuf,0,length);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        in.close();&lt;br /&gt;        op.flush();&lt;br /&gt;        op.close();&lt;br /&gt;    }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 09 Oct 2007 23:12:04 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4629</guid>
      <author>frost137 (Douglas Wyatt)</author>
    </item>
    <item>
      <title>Get a String version of a stacktrace</title>
      <link>http://snippets.dzone.com/posts/show/4562</link>
      <description>// Get a String version of a stacktrace&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    Throwable t = new Throwable();  // test code&lt;br /&gt;&lt;br /&gt;    final Writer result = new StringWriter();&lt;br /&gt;    final PrintWriter printWriter = new PrintWriter(result);&lt;br /&gt;    t.printStackTrace(printWriter);&lt;br /&gt;    result.toString();&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 20 Sep 2007 21:00:14 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4562</guid>
      <author>frost137 (Douglas Wyatt)</author>
    </item>
  </channel>
</rss>
