if (contents instanceof StringSelection)
{ String data = null;
try
{ data = (String)contents.getTransferData
(DataFlavor.stringFlavor);
}
catch(UnsupportedFlavorException e)
{ return contents; }
catch(IOException e)
{ return contents; }
if (!data.startsWith("Content-type: "))
return contents;
int start = -1;
// skip three newlines
for (int i = 0; i < 3; i++)
{ start = data.indexOf('\n', start + 1);
if (start < 0) return contents;
}
Serializable obj = decode(data.substring(start));
SerializableSelection selection
= new SerializableSelection(obj);
return selection;
}
else return contents;
}
public static String encode(Serializable obj)
{ ByteArrayOutputStream bOut
= new ByteArrayOutputStream();
try
{ Base64OutputStream b64Out
= new Base64OutputStream(bOut);
ObjectOutputStream out
= new ObjectOutputStream(b64Out);
out.writeObject(obj);
out.close();
return bOut.toString("8859_1");
}
catch (IOException exception)
{ return null;
}
}
public static Serializable decode(String s)
{ try
{ byte[] bytes = s.getBytes("8859_1");
ByteArrayInputStream bIn
= new ByteArrayInputStream(bytes);
Base64InputStream b64In
= new Base64InputStream(bIn);
ObjectInputStream in
= new ObjectInputStream(b64In);
Object obj = in.readObject();
in.close();
return (Serializable)obj;
}
catch(Exception e)
{ return null;
}
}
private Clipboard clip;
}
/* BASE64 encoding encodes 3 bytes into 4 characters.
|11111122|22223333|33444444|
Each set of 6 bits is encoded according to the
toBase64 map. If the number of input bytes is not
a multiple of 3, then the last group of 4 characters
is padded with one or two = signs. Each output line
is at most 76 characters.
*/
class Base64OutputStream extends FilterOutputStream
{ public Base64OutputStream(OutputStream out)
{ super(out);
}
public void write(int c) throws IOException
{ inbuf[i] = c;
i++;
if (i == 3)
{ super.write(toBase64[(inbuf[0] & 0xFC) >> 2]);
super.write(toBase64[((inbuf[0] & 0x03) << 4) |
((inbuf[1] & 0xF0) >> 4)]);
super.write(toBase64[((inbuf[1] & 0x0F) << 2) |
((inbuf[2] & 0xC0) >> 6)]);
super.write(toBase64[inbuf[2] & 0x3F]);
col += 4;
i = 0;
if (col >= 76)
{ super.write('\n');
col = 0;
}
}
}
public void flush() throws IOException
{ if (i == 1)
{ super.write(toBase64[(inbuf[0] & 0xFC) >> 2]);
super.write(toBase64[(inbuf[0] & 0x03) << 4]);
super.write('=');
super.write('=');
}
else if (i == 2)
{ super.write(toBase64[(inbuf[0] & 0xFC) >> 2]);
super.write(toBase64[((inbuf[0] & 0x03) << 4) |
((inbuf[1] & 0xF0) >> 4)]);
super.write(toBase64[(inbuf[1] & 0x0F) << 2]);
super.write('=');
}
i = 0;
}
private int col = 0;
private int i = 0;
private int[] inbuf = new int[3];
}
class Base64InputStream extends FilterInputStream
{ public Base64InputStream(InputStream in)
{ super(in);
}
public int read(byte[] b, int off, int len) throws IOException
{ if (len > b.length - off) len = b.length - off;
for (int i = 0; i < len; i++)
{ int ch = read();
if (ch == -1) return i;
b[i + off] = (byte)ch;
}
return len;
}
public int read(byte[] b) throws IOException
{ return read(b, 0, b.length);
}
public int read() throws IOException
{ int r;
if (i == 0)
{ // skip whitespace
do
{ ch[0] = super.read();
if (ch[0] == -1) return -1;
}
while (Character.isWhitespace((char)ch[0]));
ch[1] = super.read();
if (ch[1] == -1) return -1;
i++;
r = (fromBase64[ch[0]] << 2)
| (fromBase64[ch[1]] >> 4);
}
else if (i == 1)
{ ch[2] = super.read();
if (ch[2] == '=' || ch[2] == -1) return -1;
i++;
r = ((fromBase64[ch[1]] & 0x0F) << 4)
| (fromBase64[ch[2]] >> 2);
}
else
{ ch[3] = super.read();
if (ch[3] == '=' || ch[3] == -1) return -1;
i = 0;
r = ((fromBase64[ch[2]] & 0x03) << 6)
| fromBase64[ch[3]];
}
return r;
}