A search for a charged Higgs boson is performed with a data sample corresponding to an integrated luminosity of 19.7 0.5 fb collected with the CMS detector in proton-proton collisions at 8 TeV. The charged Higgs boson is searched for in top quark decays for lower then , and in the direct production for larger than . The and decay modes in the final states +jets, , +jets, and ' ( e, ) are considered in the search. No signal is observed and 95% confidence level upper limits are set on the charged Higgs boson production. A model-independent upper limit on the product branching fraction \mathcal{B}({\rm t\rightarrow H^\pm b}) \, \mathcal{B}({\mathrm H^\pm \rightarrow \tau^\pm \nu_\tau})= 1.2-0.15% is obtained in the mass range 80-160 GeV, while the upper limit on the cross section times branching fraction \sigma({\rm pp \rightarrow t (b) H^\pm}) \, \mathcal{B} ({\mathrm H^\pm \rightarrow \tau^\pm \nu_\tau})= 0.38-0.025 pb is set in the mass range 180-600 GeV. Here, cross section stands for the sum . Assuming 1, an upper limit on of 2.0-0.13 pb is set for 180-600 GeV. The combination of all considered decay modes and final states is used to set exclusion limits in the - parameter space in different MSSM benchmark scenarios.