1 29 30 package com.caucho.server.connection; 31 32 import com.caucho.log.Log; 33 import com.caucho.util.FreeList; 34 35 import javax.servlet.http.HttpServletResponse ; 36 import java.io.IOException ; 37 import java.util.logging.Logger ; 38 39 public class ToCharResponseAdapter extends ResponseAdapter { 40 private static final Logger log = Log.open(ToCharResponseAdapter.class); 41 42 private static final FreeList<ToCharResponseAdapter> _freeList = 43 new FreeList<ToCharResponseAdapter>(32); 44 45 private ToCharResponseStreamWrapper _responseStream; 46 47 private ToCharResponseAdapter(HttpServletResponse response) 48 { 49 super(response); 50 } 51 52 55 public static ToCharResponseAdapter create(HttpServletResponse response) 56 { 57 ToCharResponseAdapter resAdapt = _freeList.allocate(); 58 59 if (resAdapt == null) 60 resAdapt = new ToCharResponseAdapter(response); 61 else 62 resAdapt.setResponse(response); 63 64 resAdapt.init(response); 65 66 return resAdapt; 67 } 68 69 protected AbstractResponseStream createWrapperResponseStream() 70 { 71 if (_responseStream == null) 72 _responseStream = new ToCharResponseStreamWrapper(); 73 74 return _responseStream; 75 } 76 77 public void init(HttpServletResponse response) 78 { 79 _responseStream.start(); 80 81 super.init(response); 82 } 83 84 public void resetBuffer() 85 { 86 _responseStream.clearBuffer(); 87 88 super.resetBuffer(); 89 90 94 } 95 96 public static void free(ToCharResponseAdapter resAdapt) 97 { 98 resAdapt.free(); 99 100 _freeList.free(resAdapt); 101 } 102 103 class ToCharResponseStreamWrapper extends ToCharResponseStream { 104 protected String getEncoding() 105 { 106 return getResponse().getCharacterEncoding(); 107 } 108 109 112 public void flushChar() 113 throws IOException 114 { 115 flushBuffer(); 116 117 getResponse().getWriter().flush(); 118 } 119 120 123 public void close() 124 throws IOException 125 { 126 flushBuffer(); 128 129 } 132 133 protected void writeNext(char []buffer, int offset, int length) 134 throws IOException 135 { 136 getResponse().getWriter().write(buffer, offset, length); 137 } 138 } 139 } 140 | Popular Tags |