Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
2138 connectés 

  FORUM HardWare.fr
  Programmation
  Java

  Encoder en qoted printable

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Encoder en qoted printable

n°225503
krosso
j'suis à la bourre
Posté le 09-10-2002 à 11:32:36  profilanswer
 

Salut!
 
Connaissez-vous une methode pour encoder une String en quoted printable (RFC 2045) ?
 
J'ai bien trouvé kekchose dans javax.mail.internet.MimeUtility, mais ça n'a pas l'air de convenir (je cherche encore).
 
merci à tous.
 
K.

mood
Publicité
Posté le 09-10-2002 à 11:32:36  profilanswer
 

n°225610
krosso
j'suis à la bourre
Posté le 09-10-2002 à 14:28:29  profilanswer
 

Bon, bin je me lance dans l'écriture d'une méthode quotedPrintableEncoder(), ça devrait pas être trop compliqué.
 
M'enfin si vous en avez une qui traîne dans un coin, pensez à moi...
 
K.
 

n°225616
lorill
Posté le 09-10-2002 à 14:38:20  profilanswer
 

Code :
  1. package jwma.webmail.util;
  2. public abstract class QuotedPrintable
  3. {
  4.     /**
  5.      * Convert a string from text canonical form to Quoted Printable form.
  6.      * Line breaks (both before and after conversion) MUST be represented
  7.      * as \r\n - any other combination is treated as binary data.
  8.      *
  9.      * @param s canonical text to be encoded
  10.      * @return text in Quoted Printable form
  11.      */
  12.     public static String encode( String s )
  13.     {
  14.         int len = s.length();
  15.         StringBuffer buffer = new StringBuffer( 2 * len );
  16.         StringBuffer line   = new StringBuffer( 240 );
  17.         StringBuffer white  = new StringBuffer( 80 );
  18.         for (int i=0; i<len; i++)
  19.         {
  20.             int c = s.charAt( i );
  21.             if ((c == '\r') && (i != len-1) && (s.charAt( i+1 ) == '\n'))
  22.             {
  23.                 encodeEndOfLine( buffer, line, white );
  24.                 i++;
  25.                 continue;
  26.             }
  27.             else if ((c == ' ') || (c == '\t'))
  28.             {
  29.                 white.append( (char)c );
  30.             }
  31.             else
  32.             {
  33.                 if (white.length() != 0)
  34.                 {
  35.                     line.append( white );
  36.                     white.setLength( 0 );
  37.                 }
  38.                 if ((c >= '!') && (c <= '~') && (c != '='))
  39.                 {
  40.                     line.append( (char)c );
  41.                 }
  42.                 else
  43.                 {
  44.                     appendHex( line, c );
  45.                 }
  46.             }
  47.         }
  48.         if ((line.length() != 0) || (white.length() != 0))
  49.         {
  50.             encodeEndOfLine( buffer, line, white );
  51.         }
  52.         return buffer.toString();
  53.     }
  54.     private static void encodeEndOfLine(
  55.         StringBuffer buffer, StringBuffer line, StringBuffer white )
  56.     {
  57.         int i,j;
  58.         for (i=0; i<white.length(); i++)
  59.         {
  60.             appendHex( line, white.charAt( i ));
  61.         }
  62.         String s = line.toString();
  63.         int len = s.length();
  64.         boolean split = false;
  65.         for (i=0; i<len; i=j)
  66.         {
  67.             j = i + 75;
  68.             if ((j == len-1) || (j > len)) j = len;
  69.             if (j > 2)
  70.             {
  71.                 if (s.charAt( j-1 ) == '=') j -= 1;
  72.                 if (s.charAt( j-2 ) == '=') j -= 2;
  73.             }
  74.             buffer.append( s.substring( i, j ));
  75.             if (j != len) buffer.append( '=' );
  76.             buffer.append( "\r\n" );
  77.         }
  78.         line.setLength( 0 );
  79.         white.setLength( 0 );
  80.     }
  81.     private static void appendHex( StringBuffer line, int c )
  82.     {
  83.         c &= 0xFF;
  84.         line.append( '=' );
  85.         line.append( "0123456789ABCDEF".charAt( c >> 4 ));
  86.         line.append( "0123456789ABCDEF".charAt( c & 0x0F ));
  87.     }
  88.     /**
  89.      * Convert a string from Quoted Printable form to text canonical form.
  90.      * Unrecognised sequences starting with '=' are passed through unmodified.
  91.      * Note that this method will strip trailing whitespace from each line.
  92.      *
  93.      * @param s canonical text to be encoded
  94.      * @return text in Quoted Printable form
  95.      */
  96.     public static String decode( String s )
  97.     {
  98.         if(s.startsWith("=?iso-8859-1?Q?" ))
  99.     s = s.substring(15);
  100.         StringBuffer buffer = new StringBuffer( s.length() );
  101.         StringBuffer white = new StringBuffer( 80 );
  102.         int len = s.length();
  103.         for (int i=0; i<len; i++)
  104.         {
  105.             char c = s.charAt( i );
  106.             if ((c == '=') && (i+2 < len))
  107.             {
  108.                 char a = s.charAt( ++i );
  109.                 char b = s.charAt( ++i );
  110.                 if ((a == '\r') && (b == '\n')) continue;
  111.                 int ah = "0123456789ABCDEF".indexOf( a );
  112.                 int bh = "0123456789ABCDEF".indexOf( b );
  113.                 if ((ah == -1) || (bh == -1))
  114.                 {
  115.                     buffer.append( '=' );
  116.                     buffer.append( a );
  117.                     buffer.append( b );
  118.                 }
  119.                 else
  120.                 {
  121.                     buffer.append( (char)((ah << 4) | bh) );
  122.                 }
  123.             }
  124.             else if ((c == ' ') || (c == '\t'))
  125.             {
  126.                 white.append( c );
  127.             }
  128.             else
  129.             {
  130.                 if (white.length() != 0) buffer.append( white );
  131.                 buffer.append( c );
  132.                 white.setLength( 0 );
  133.             }
  134.         }
  135.         s = buffer.toString();
  136. int index;
  137. while( (index=s.indexOf("=?iso-8859-1?Q?" )) >= 0)
  138.  s = s.substring(0, index) + s.substring(index+15);
  139. while( (index=s.indexOf("?=" )) >= 0)
  140.  s = s.substring(0, index) + s.substring(index+2);
  141.         return s;
  142.     }
  143.     /**
  144.      * Test whether or not a String could have been generated by encode()
  145.      * or any similar generator which complies with the RFC.
  146.      */
  147.     public static boolean isProbablyEncoded( String s )
  148.     {
  149.         int len = s.length();
  150.         int lineStart = 0;
  151.         boolean white = false;
  152.         for (int i=0; i<len; i++)
  153.         {
  154.             char c = s.charAt( i );
  155.             if (i - lineStart > 76) return false;
  156.             if ((c == '=') && (i+2 < len))
  157.             {
  158.                 char a = s.charAt( ++i );
  159.                 char b = s.charAt( ++i );
  160.                 if ((a == '\r') && (b == '\n'))
  161.                 {
  162.                     white = false;
  163.                     lineStart = i+1;
  164.                     continue;
  165.                 }
  166.                 if ("0123456789ABCDEF".indexOf( a ) == -1) return false;
  167.                 if ("0123456789ABCDEF".indexOf( b ) == -1) return false;
  168.             }
  169.             else if ((c == '\r') && (i+1 < len))
  170.             {
  171.                 if (white) return false;
  172.                 char a = s.charAt( ++i );
  173.                 if (a != '\n') return false;
  174.                 lineStart = i+1;
  175.             }
  176.             else
  177.             {
  178.                 if ((c == ' ') || (c == '\t'))
  179.                     white = true;
  180.                 else if ((c >= '!') && (c <= '~') && (c != '='))
  181.                     white = false;
  182.                 else
  183.                     return false;
  184.             }
  185.         }
  186.         return true;
  187.     }
  188. }


 
mais je sais plus ou j'ai emprunté ca :/

n°225623
krosso
j'suis à la bourre
Posté le 09-10-2002 à 14:45:09  profilanswer
 

lorill a écrit a écrit :

 
mais je sais plus ou j'ai emprunté ca :/




 
Merci  :)  
 
google ne renvoit rien sur:
"package jwma.webmail.util; "
 
Bon c'est pas un algorithme ultra-confidentiel non plus.
 

n°225626
krosso
j'suis à la bourre
Posté le 09-10-2002 à 14:48:22  profilanswer
 

Code :
  1. private static void appendHex( StringBuffer line, int c )


 
Tiens moi j'aurais plutôt fait un Integer.toHexString(c).toUpperCase() mais c'est ptête plus lourd ?

n°225628
lorill
Posté le 09-10-2002 à 14:51:11  profilanswer
 

krosso a écrit a écrit :

 
google ne renvoit rien sur:
"package jwma.webmail.util; "




 
Normal, je l'ai adapté... ca doit venir d'un des projets apache, mais lequel, je sais plus. Bref, c'est légalement réutilisable quand même.

n°225655
--greg--
Posté le 09-10-2002 à 15:37:05  profilanswer
 

http://jwma.sourceforge.net/


---------------
#19b | Mardi 18 Février 2003 - nous fêtons les Bernadette | contre le fleur icq!
n°225657
lorill
Posté le 09-10-2002 à 15:38:21  profilanswer
 

--greg-- a écrit a écrit :

http://jwma.sourceforge.net/




 
Oui mais non. C'est effectivement le soft que j'adapte, mais y'a pas de classe QuotedPrintable la dedans (ou alors je suis un boulay, j'ai maté dans le CVS juste avant).

n°225659
--greg--
Posté le 09-10-2002 à 15:42:29  profilanswer
 

lorill a écrit a écrit :

 
 
Oui mais non. C'est effectivement le soft que j'adapte, mais y'a pas de classe QuotedPrintable la dedans (ou alors je suis un boulay, j'ai maté dans le CVS juste avant).



ha ok :D


---------------
#19b | Mardi 18 Février 2003 - nous fêtons les Bernadette | contre le fleur icq!

Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Java

  Encoder en qoted printable

 

Sujets relatifs
Pour les spécialistes HTML / Streaming / Windows Media EncoderEncoder un & en sql sous oracle
Plus de sujets relatifs à : Encoder en qoted printable


Copyright © 1997-2025 Groupe LDLC (Signaler un contenu illicite / Données personnelles)