commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jeff Varszegi <j...@generalize.net>
Subject [codec] Re: Submit: codec fixes + encoder changes + hex
Date Mon, 02 Dec 2002 18:02:18 GMT
Yeah, I hear you.  Up to now I rolled my own, until I
started using org.apache.xerces.utils.HexBin .  I
agree that most of the time you're looking for a
string (when you're dealing with XML, etc.)-- I will
think on it a bit.  I definitely agree about the
streaming codecs, but I also agree that the previous
objection about things like Soundex being inherently
un-stream-oriented is totally valid.  Also, I think
that the point about encoding and decoding both just
being transformations is totally valid, but I'm not
sure about the cleanest way to represent everything
yet.  

Know what I mean?  There's no Encoder implementation
currently in [codec] that returns a decoded string
from a hex string, and if there were I'm not sure what
it should be called with the current arrangement.  I
don't think that HexDecoder (implements Encoder?)
would be a good term, for instance, but it's the
natural name to give the class...

Another thing that's bothering me is that there may be
some sorts of common situations where the argument
list changes for the encoding methods.  Not only
(without investigation) can we not be sure of knowing
how many arguments may be needed for a particular
encoding method, we can't be sure of their types. 
This is leading me in the direction of suggesting
another version that takes an Object array as an
additional argument (in addition to the stream or byte
array).  This method could be optionally supported.  I
dunno...

One thing that I think should be done to the Hex class
right off the bat is to make the char[] hexDigits
static final, to stop the bleeding if someone creates
a bunch of them instead of properly looking up a
registered instance.

Your friend,

Jeff

--- Tim O'Brien <tobrien@transolutions.net> wrote:
> If I want a hexadecimal representation of a byte
> array, I'm looking for
> a String.  This very same function has popped up in
> a number of projects
> - SOAP, Struts, ... Every project larger than 20,000
> lines of code - and
> I was looking for a way to get a String of hex
> digits out of a byte[]
> without doing one of two things:
> 
> 	1. Implementing a custom Hex in my own proprietary
> project. (
> I've seen "public String toHex(byte[])" written
> almost as frequently as
> "public static void main(String[] args)" ) 
> 	2. Downloading something like Apache SOAP just to
> use a Hex
> encoding function.
> 
> The exception would be if I were looking at a
> ByteArrayInputStream, and
> I wanted to write out 2 characters to a Reader for
> every byte
> encountered.  Codec isn't there yet, it would be
> nice if this codec
> worked with streams.    Codec could be much
> better/cleaner, more well
> thought out.
> 
> A side note: (Hopefully, someone will prove me
> wrong) Maybe I'm missing
> something, but I'm generally disappointed that Sun
> didn't already put
> something in the J2SE API to get a Hex string from a
> byte[].  J2SE is
> like a swiss army knife without a bottle opener.
> 
> --------
> Tim O'Brien 
> Transolutions, Inc.
> W 847-574-2143
> M 847-863-7045
> 
> 
> > -----Original Message-----
> > From: Jeffrey Varszegi
> [mailto:jeff@generalize.net] 
> > Sent: Sunday, December 01, 2002 7:43 PM
> > To: Jakarta Commons Developers List
> > Subject: Re: [codec] Submit: codec fixes + encoder
> changes + hex
> > 
> > 
> > Tim,
> > 
> > That's great, but don't you think that the method
> > should return a byte array instead of a String? 
> Then
> > maybe you could include a method named
> > encodeToString() or something similar, as an
> optional
> > operation.   
> > 
> > I don't think it's a big deal to create a String
> from
> > a byte array, but to assume that all users of some
> > common encoding interface will be dealing with
> strings
> > might be a mistake.  What do you think?  
> > 
> > Jeff
> > 
> > --- Tim O'Brien <tobrien@transolutions.net> wrote:
> > > Here is a patch for codec, which includes the
> > > following:
> > > 
> > > 0. Added a function to Encoder - public String
> > > encode(byte[])
> > > 
> > > 1. Added function to all classes that implement
> > > encoder.
> > > 
> > > 2. Hex, encodes a byte[] to a hex string -
> 0xcbd342
> > > -> "cbd342"
> > > 
> > > 3. TestHex, a junit test for Hex class.
> > > 
> > > 4. Some files in the codec package CRLF
> problems,
> > > patch resolves this.
> > > 
> > > --------
> > > Tim O'Brien
> > > Transolutions, Inc.
> > > 18 N Waukegan Road
> > > Lake Bluff, Il 60044
> > > W 847-574-2143
> > > F 847-234-3471
> > > M 847-863-7045
> > > > Index:
> > > src/java/org/apache/commons/codec/Encoder.java
> > >
> >
>
===================================================================
> > > RCS file:
> > >
> >
>
/home/cvspublic/jakarta-commons-sandbox/codec/src/java/org/apa
> > che/commons/codec/Encoder.java,v
> > > retrieving revision 1.2
> > > diff -u -r1.2 Encoder.java
> > > ---
> src/java/org/apache/commons/codec/Encoder.java
> > > 18 Nov 2002 12:41:24 -0000	1.2
> > > +++
> src/java/org/apache/commons/codec/Encoder.java	1
> > > Dec 2002 22:03:15 -0000
> > > @@ -1,4 +1,16 @@
> > > -/*
> > >
> >
>
====================================================================
> > > * The Apache Software License, Version 1.1
> >  *
> >  *
> > > Copyright (c) 2002 The Apache Software
> Foundation.
> > > All rights
> >  * reserved.
> >  *
> >  * Redistribution and use
> > > in source and binary forms, with or without
> >  *
> > > modification, are permitted provided that the
> > > following conditions
> >  * are met:
> >  *
> >  * 1.
> > > Redistributions of source code must retain the
> above copyright
> >  *    notice, this list of conditions and
> > > the following disclaimer.
> >  *
> >  * 2. Redistributions
> > > in binary form must reproduce the above
> copyright
> > > +/*
> > >
> >
>
====================================================================
> > > + * The Apache Software License, Version 1.1 *
> > > + * Copyright (c) 2002 The Apache Software
> > > Foundation.  All rights
> > > + * reserved.
> > > + *
> > > + * Redistribution and use in source and binary
> > > forms, with or without
> > > + * modification, are permitted provided that
> the
> > > following conditions
> > > + * are met:
> > > + *
> > > + * 1. Redistributions of source code must
> retain
> > > the above copyright
> > > + *    notice, this list of conditions and the
> > > following disclaimer.
> > > + *
> > > + * 2. Redistributions in binary form must
> reproduce
> > > the above copyright
> > >   *    notice, this list of conditions and the
> > > following disclaimer in
> > >   *    the documentation and/or other materials
> > > provided with the
> > >   *    distribution.
> > > @@ -31,4 +43,25 @@
> > >   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
> > > ARISING IN ANY WAY OUT
> > >   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
> OF
> > > THE POSSIBILITY OF
> > >   * SUCH DAMAGE.
> > > - *
> > >
> >
>
====================================================================
> > > *
> >  * This software consists of voluntary
> > > contributions made by many
> >  * individuals on behalf
> > > of the Apache Software Foundation.  For more
> >  *
> > > information on the Apache Software Foundation,
> > > please see
> >  * <http://www.apache.org/>.
> >  */
> > package
> > > org.apache.commons.codec;
> > 
> > /**
> >  * Encoder is an
> > > interface, which is implemented by Soundex,
> >  *
> > > Metaphone, Soundex2, etc.
> >  *
> >  * @author
> > > tobrien@transolutions.net
> >  * @version $Revision: 1.2
> > > $ $Date: 2002/11/18 12:41:24 $
> >  */
> > public interface
> > > Encoder {
> >     String encode(String str);
> > }  
> > > + *
> > >
> >
>
====================================================================
> > > + *
> > > + * This software consists of voluntary
> > > contributions made by many
> > > + * individuals on behalf of the Apache Software
> > > Foundation.  For more
> > > + * information on the Apache Software
> Foundation,
> > > please see
> > > + * <http://www.apache.org/>.
> > > + */
> > > +package org.apache.commons.codec;
> > > +
> > > +/**
> > > + * Encoder is an interface, which is
> implemented by
> > > Soundex,
> > > + * Metaphone, Soundex2, etc.
> > > + * 
> > > + * @author tobrien@transolutions.net 
> > > + * @version $Revision: 1.2 $ $Date: 2002/11/18
> > > 12:41:24 $
> > > + */
> > > +public interface Encoder {
> > > +
> > > +  String encode(String str);
> > > +  String encode(byte[] bytes);
> > > +
> > > +}
> > > Index:
> > >
> >
>
src/java/org/apache/commons/codec/EncoderComparator.java
> > >
> >
>
===================================================================
> > > RCS file:
> > >
> >
>
/home/cvspublic/jakarta-commons-sandbox/codec/src/java/org/apa
> > che/commons/codec/EncoderComparator.java,v
> > > retrieving revision 1.2
> > > diff -u -r1.2 EncoderComparator.java
> > > ---
> > >
> >
>
src/java/org/apache/commons/codec/EncoderComparator.java
> > > 18 Nov 2002 12:41:24 -0000	1.2
> > > +++
> > >
> >
>
src/java/org/apache/commons/codec/EncoderComparator.java
> > > 1 Dec 2002 22:03:16 -0000
> > > @@ -1,2 +1,86 @@
> > > -/*
> > >
> >
>
====================================================================
> > > * The Apache Software License, Version 1.1
> >  *
> >  *
> > > Copyright (c) 2002 The Apache Software
> Foundation.
> > > All rights
> >  * reserved.
> >  *
> >  * Redistribution and use
> > > in source and binary forms, with or without
> >  *
> > > modification, are permitted provided that the
> > > following conditions
> >  * are met:
> >  *
> >  * 1.
> > > Redistributions of source code must retain the
> above copyright
> >  *    notice, this list of conditions and
> > > the following disclaimer.
> >  *
> >  * 2. Redistributions
> > > in binary form must reproduce the above
> copyright
> >  *
> > >    notice, this list of conditions and the
> following disclaimer in
> >  *    the documentation and/or other
> > > materials provided with the
> >  *    distribution.
> >  *
> > > * 3. The end-user documentation included with
> the redistribution,
> >  *    if any, must include the
> > > following acknowledgment:
> >  *       "This product
> > > includes software developed by the
> >  *        Apache
> > > Software Foundation (http://www.apache.org/)."
> >  *   
> > > Alternately, this acknowledgment may appear in
> the
> > > software itself,
> >  *    if and wherever such
> > > third-party acknowledgments normally appear.
> >  *
> >  *
> > > 4. The names "Apache" and "Apache Software
> > > Foundation" and
> >  *    "Apache Commons" must not be
> > > used to endorse or promote products
> >  *    derived
> > > from this software without prior written
> permission.
> > > For
> >  *    written permission, please contact
> > > apache@apache.org.
> >  *
> >  * 5. Products derived from
> > > this software may not be called "Apache",
> >  *   
> > > "Apache Turbine", nor may "Apache" appear in
> their
> > > name, without
> >  *    prior written permission of the
> > > Apache Software Foundation.
> >  *
> >  * THIS SOFTWARE IS
> > > PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
> >  *
> > > WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
> > > IMPLIED WARRANTIES
> >  * OF MERCHANTABILITY AND FITNESS
> > > FOR A PARTICULAR PURPOSE ARE
> >  * DISCLAIMED.  IN NO
> > > EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
> >  * ITS
> > > CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
> > > INCIDENTAL,
> >  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
> > > DAMAGES (INCLUDING, BUT NOT
> >  * LIMITED TO,
> > > PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
> LOSS
> > > OF
> >  * USE, DATA, OR PROFITS; OR BUSINESS
> > > INTERRUPTION) HOWEVER CAUSED AND
> >  * ON ANY THEORY OF
> > > LIABILITY, WHETHER IN CONTRACT, STRICT
> LIABILITY,
> >  *
> > > OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
> ARISING
> > > IN ANY WAY OUT
> >  * OF THE USE OF THIS SOFTWARE, EVEN
> > > IF ADVISED OF THE POSSIBILITY OF
> >  * SUCH DAMAGE.
> >  *
> > >
> >
>
====================================================================
> > > *
> >  * This software consists of voluntary
> > > contributions made by many
> >  * individuals on behalf
> > > of the Apache Software Foundation.  For more
> >  *
> > > information on the Apache Software Foundation,
> > > please see
> >  * <http://www.apache.org/>.
> >  */
> > package
> > > org.apache.commons.codec;
> > > -import java.util.Comparator;
> > 
> > /**
> >  * Compare using
> > > an Encoder.
> >  *
> >  * @author tobrien@transolutions.net
> > > * @version $Revision: 1.2 $ $Date: 2002/11/18
> > > 12:41:24 $
> >  */
> > public class EncoderComparator
> > > implements Comparator {
> > 
> >     private Encoder
> > > encoder;
> > 
> >     /**
> >      * Use the default soundex
> > > algorithm, US_ENGLISH.
> >      */
> >     public
> > > EncoderComparator() {
> >        
> > > this(RefinedSoundex.US_ENGLISH);
> >     }
> > 
> >     /**
> >     
> > > * Use the provided soundex algorithm.
> >      */
> >    
> > > public EncoderComparator(Encoder en) {
> >        
> > > this.encoder = en;
> >     }
> > 
> >     public int
> > > compare(Object o1, Object o2) {
> >         String s1 =
> > > encoder.encode(o1.toString());
> >         String s2 =
> > > encoder.encode(o2.toString());
> >         return
> > > s1.compareTo(s2);
> >     }
> > 
> > }
> > > +/*
> > >
> >
>
====================================================================
> > > + * The Apache Software License, Version 1.1 *
> > > + * Copyright (c) 2002 The Apache Software
> > > Foundation.  All rights
> > > + * reserved.
> > > + *
> > > + * Redistribution and use in source and binary
> > > forms, with or without
> > > + * modification, are permitted provided that
> the
> > > following conditions
> > > + * are met:
> > > + *
> > > + * 1. Redistributions of source code must
> retain
> > > the above copyright
> > > + *    notice, this list of conditions and the
> > > following disclaimer.
> > > + *
> > > + * 2. Redistributions in binary form must
> reproduce
> > > the above copyright
> > > 
> > === message truncated ===> --
> > > To unsubscribe, e-mail:
> > >
> <mailto:commons-dev-unsubscribe@jakarta.apache.org>
> > > For additional commands, e-mail:
> > <mailto:commons-dev-help@jakarta.apache.org>
> > 
> > 
> > --
> > To unsubscribe, e-mail:   
> > <mailto:commons-dev->
> unsubscribe@jakarta.apache.org>
> > For 
> > additional commands, 
> > e-mail:
> <mailto:commons-dev-help@jakarta.apache.org>
> > 
> 
> 
> 
> --
> To unsubscribe, e-mail:  
> <mailto:commons-dev-unsubscribe@jakarta.apache.org>
> For additional commands, e-mail:
> <mailto:commons-dev-help@jakarta.apache.org>
> 


--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message