1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.apache.http.impl.client.cache.memcached;
28
29 import java.io.ByteArrayInputStream;
30 import java.io.ByteArrayOutputStream;
31 import java.io.IOException;
32 import java.io.ObjectInputStream;
33 import java.io.ObjectOutputStream;
34
35 import org.apache.http.client.cache.HttpCacheEntry;
36
37
38
39
40
41
42 public class MemcachedCacheEntryImpl implements MemcachedCacheEntry {
43
44 private String key;
45 private HttpCacheEntry httpCacheEntry;
46
47 public MemcachedCacheEntryImpl(final String key, final HttpCacheEntry httpCacheEntry) {
48 this.key = key;
49 this.httpCacheEntry = httpCacheEntry;
50 }
51
52 public MemcachedCacheEntryImpl() {
53 }
54
55
56
57
58 @Override
59 synchronized public byte[] toByteArray() {
60 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
61 final ObjectOutputStream oos;
62 try {
63 oos = new ObjectOutputStream(bos);
64 oos.writeObject(this.key);
65 oos.writeObject(this.httpCacheEntry);
66 oos.close();
67 } catch (final IOException ioe) {
68 throw new MemcachedSerializationException(ioe);
69 }
70 return bos.toByteArray();
71 }
72
73
74
75
76 @Override
77 public synchronized String getStorageKey() {
78 return key;
79 }
80
81
82
83
84 @Override
85 public synchronized HttpCacheEntry getHttpCacheEntry() {
86 return httpCacheEntry;
87 }
88
89
90
91
92 @Override
93 synchronized public void set(final byte[] bytes) {
94 final ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
95 final ObjectInputStream ois;
96 final String s;
97 final HttpCacheEntry entry;
98 try {
99 ois = new ObjectInputStream(bis);
100 s = (String)ois.readObject();
101 entry = (HttpCacheEntry)ois.readObject();
102 ois.close();
103 bis.close();
104 } catch (final IOException ioe) {
105 throw new MemcachedSerializationException(ioe);
106 } catch (final ClassNotFoundException cnfe) {
107 throw new MemcachedSerializationException(cnfe);
108 }
109 this.key = s;
110 this.httpCacheEntry = entry;
111 }
112
113 }