Energy harvesting has emerged as a powerful technology for complementing current battery-powered communication systems in order to extend their lifetime. In this paper a general framework is introduced for the optimization of communication systems in which the transmitter is able to harvest energy from its environment. Assuming that the energy arrival process is known non-causally at the transmitter, the structure of the optimal transmission scheme, which maximizes the amount of transmitted data by a given deadline, is identified. Our framework includes models with continuous energy arrival as well as battery constraints. A battery that suffers from energy leakage is studied further, and the optimal transmission scheme is characterized for ...